int a;
int b;
int c;
Scanner input = new Scanner (System.in);
//how to read three integers with white space delimiter
System.out.print("Enter the 3 edges of the triangle to be calculated: ");
int numbers = input.nextInt();
//then turn 3 integers into boolean form
//this is only the algorithm
Boolean isTriangle = ((a+b>c) && (b+c > a) && (c+a > b));
System.out.print(isTriangle);
else
System.out.print(isTriangle);
因此,而不是在一个单独的行或换行,我想他们都在短短与空格分隔同一行中输入3点的整数。我是否需要将标准输入更改为字符串,然后在布尔部分之后解析它?我很困惑,因为进入整数后我不知道在哪里存储它们以用于布尔部分。如何从仅用空格分隔的一行读取数据?
编辑部分:
public static void main(String[] args){
int x;
int y;
int z;
Scanner input = new Scanner(System.in);
System.out.println("Enter three edges: ");
x = input.nextInt();
y = input.nextInt();
z = input.nextInt();
boolean isTriangle = ((x+y>z) && (y+z > x) && (z+x > y));
if (isTriangle){
System.out.print("Can edges " + x + ", " + y + ", " + z + " form a triangle"+ isTriangle);
}
else {
System.out.print("Can edges " + x + ", " + y + ", " + z + " form a triangle"+ isTriangle);
}
}
那为什么当我打电话XY和Z的System.out他们不显示输入的输入,但是当我只有把isTriangle上的System.out它给了我输出
我首先想到了这一点,但我也认为也许还有另一种阅读输入的方式。谢谢! – Nathan 2014-09-04 08:18:36
你如何使用hasNextInt? – Nathan 2014-09-04 08:41:28
@Nathan查看更新 – BackSlash 2014-09-04 08:50:04