1

我试图获取字符串中的最后两个字符为英寸,前两个字符为英尺。有什么我没有看到的,因为我尝试了很多东西。

高度应该在两组数字之间有一个空格,例如 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

如您所见,我遇到了“从两个高度取英尺和英寸”的问题。

4

5 回答 5

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);
于 2013-10-18T00:36:11.913 回答
0

您正在尝试substr()int. 此外,当您调用它时Integer.toString(h1);,不会将变量设置为字符串。为此,它必须是:

feet1 = Integer.toString(h1)

feet1 = feet1.substr(0, 1); 

希望这可以帮助

于 2013-10-18T00:32:57.597 回答
0

这两行:

Integer.toString(h1);
Integer.toString(h2);

不要做你认为他们做的事。

我认为您期望在它们运行之后,h1 和 h2 现在是字符串。但事实并非如此。变量不能改变类型。h1 和 h2 仍然是整数。

Integer.toString返回结果而不是产生副作用。目前,您只是丢弃结果。您需要对结果进行一些处理,例如将其存储在新变量中。也许是这样的:

String h1AsString = Integer.toString(h1);
String h2AsString = Integer.toString(h2);
于 2013-10-18T00:31:15.353 回答
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不是subtractor substr

于 2013-10-18T00:45:30.300 回答
0

我先用空格分割字符串:

如何按空格分割字符串

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] );
于 2013-10-18T00:31:07.823 回答