我正在尝试将字符串中的最后两个字符设置为英寸,将前两个字符设置为英尺。有没有我没有看到的东西,因为我已经尝试了很多东西。
高度应该在两组数字之间有一个空格,比如5 10或12 02,所以这就是我试图获得某些角色的原因。所以我可以将它们移动到另一个字符串中以将它们添加到一起,因为我想将两个人的高度加在一起,这就是我到目前为止所得到的......
import javax.swing.JOptionPane;
public class TestProgramming
{ //begin class
public static void main(String[] args)
{ // begin main
// ***** variables *****
int h1;
int h2;
String height1 = "0";
String height2 = "0";
String feet1;
String feet2;
String inches1;
String inches2;
String FinalFoot = "0";
String FinalInch = "12";
// ***** input box 1 *****
height1 = JOptionPane.showInputDialog (null, "Please enter the height of the first person in inches.");
// ***** input box 2 *****
height2 = JOptionPane.showInputDialog (null, "Please enter the height of the second person in inches.");
// ***** parse *****
h1 = Integer.parseInt(height1);
h2 = Integer.parseInt(height2);
// ***** integer to string *****
Integer.toString(h1);
Integer.toString(h2);
// ***** taking the feet and inches from the two heights *****
feet1 = h1.substr(0, 1); // subtract the last 2 numbers
inches1 = h1.substr(2, 4); // subtract the first 2 numbers
feet2 = h2.substr(0, 1); // subtract the last 2 numbers
inches2 = h2.substr(2, 4); // subtract the first 2 numbers
正如你所看到的那样,我遇到的问题是“从两个高度抬起脚和英寸”。
答案 0 :(得分:1)
让我们仔细看看代码......
int h1;
int h2;
String height1 = "0";
String height2 = "0";
//...
// This is good...
height1 = JOptionPane.showInputDialog (null, "Please enter the height of the first person in inches.");
height2 = JOptionPane.showInputDialog (null, "Please enter the height of the second person in inches.");
// Ahh, here is a problem...
// If the String values contain any thing that is not a numerical character,
// these will throw a NumberFormatException, meaning anything that follows won't
// be processed...
h1 = Integer.parseInt(height1);
h2 = Integer.parseInt(height2);
// This does nothing. Integer.toString will RETURN a String representation
// of the int's you pass it, but you're not assigning them to anything...
Integer.toString(h1);
Integer.toString(h2);
// Last time I checked, String don't have a method called `substr`
feet1 = h1.substr(0, 1);
从用户那里获得String
输入后,您可以使用String#split
拆分空格上的输入,例如......
height1 = ...
String[] heightsOf1 = height1.split(" ");
然后,您需要将每个元素转换为int
...
int[] personOneHeights = new int[2];
personOneHeights[0] = Integer.parseInt(heightsOf1[0]);
personOneHeights[1] = Integer.parseInt(heightsOf1[1]);
现在,您真的应该在这里添加一些健全性检查,您可以使用String#contains
来确定原始String
是否包含空格字符,如果不是,则继续询问用户输入(例如)。您还应该检查split
的结果,以确定它是否在阵列中至少有2个元素。
如果您不能使用数组,则不应该依赖幻数,而是尝试使用您手边的信息,例如......
String part1 = height1.substring(0, height1.indexOf(" "));
String part2 = height1.substring(height1.lastIndexOf(" ") + 1);
答案 1 :(得分:0)
我先用空格分割字符串:
How to split a String by space
String[] split1 = height1.split("\\s+");
feet1 = Integer.parseInt( split1[0] );
inches1 = Integer.parseInt( split1[1] );
String[] split2 = height2.split("\\s+");
feet2 = Integer.parseInt( split2[0] );
inches2 = Integer.parseInt( split2[1] );
答案 2 :(得分:0)
这两行:
Integer.toString(h1);
Integer.toString(h2);
不要做你认为他们做的事。
我认为你预计在他们跑完之后,h1和h2现在是Strings。但事实并非如此。变量不能改变类型。 h1和h2保持整数。
Integer.toString
不会产生副作用,而是返回结果。目前你只是放弃了结果。您需要对结果执行某些操作,例如将其存储在新变量中。也许是这样的:
String h1AsString = Integer.toString(h1);
String h2AsString = Integer.toString(h2);
答案 3 :(得分:0)
您正试图在substr()
上使用int
。此外,当您调用未将变量设置为字符串的Integer.toString(h1);
时。要做到这一点,它必须是:
feet1 = Integer.toString(h1)
feet1 = feet1.substr(0, 1);
希望这有帮助
答案 4 :(得分:0)
试试这个:(请注意,我对输入进行了硬编码,以便在控制台应用中轻松进行测试)
public static void main(String []args){
// removed the early declarations here. This is C-style, old, unnecessary and makes
// code harder to read
// ***** input box 1 ***** HARCODED - replace with UI call
String height1 = "6 10";
// ***** input box 2 ***** HARCODED - replace with UI call
String height2 = "3 10";
// ***** parse feet and inches from each input *****
String feet1 = height1.substring(0, 1); // get the first digit
String inches1 = height1.substring(2, 4); // get the last 2 digits
String feet2 = height2.substring(0, 1); // get the first digit
String inches2 = height2.substring(2, 4); // get the last 2 digits
// convert parsed string data to their integer counterparts
int f1 = Integer.parseInt(feet1);
int i1 = Integer.parseInt(inches1);
int f2 = Integer.parseInt(feet2);
int i2 = Integer.parseInt(inches2);
// calculate total feet
int totalFeet = f1 + f2 + (i1 + i2) / 12;
// calculate total inches (using modulus operator)
int totalInches = (i1 + i2) % 12;
// and do the output... assuming this is what you want...
System.out.println(totalFeet + " " + totalInches);
}
此外,它是substring
而不是subtract
或substr
。