0
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它給了我輸出

回答

0

只需撥打nextInt三次即可。

System.out.print("Enter the 3 edges of the triangle to be calculated: "); 
a = input.nextInt(); 
b = input.nextInt(); 
c = input.nextInt(); 

隨着輸入

5 5 5 

它打印true

你必須請檢查是否有三個數字可用(使用input.hasNextInt()input.nextInt()):

System.out.print("Enter the 3 edges of the triangle to be calculated: "); 
if(input.hasNextInt()) { 
    a = input.nextInt(); 
} else { 
    //handle it, you don't have ints! 
} 
if(input.hasNextInt()) { 
    b = input.nextInt(); 
} else { 
    //handle it, you have just one int! 
} 
if(input.hasNextInt()) { 
    c = input.nextInt(); 
} else { 
    //handle it, you have just two ints! 
} 

DEMO

+0

我首先想到了這一點,但我也認爲也許還有另一種閱讀輸入的方式。謝謝! – Nathan 2014-09-04 08:18:36

+0

你如何使用hasNextInt? – Nathan 2014-09-04 08:41:28

+0

@Nathan查看更新 – BackSlash 2014-09-04 08:50:04

0

你可以這樣來做:

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: "); 
    a = input.nextInt(); 
    b = input.nextInt(); 
    c = 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)); 
    if (isTriangle) 
     System.out.print(isTriangle); 

    else 

     System.out.print(isTriangle); 
0

最好在下面使用。

String values=scanner.next();//if your input 5 5 5 
    String numinString[]=values.split(" "); 
    int a=Integer.parseInt(numinString[0]);//a=5 
    int b=Integer.parseInt(numinString[1]);//b=5 
    int c=Integer.parseInt(numinString[2]);//c=5 
+0

您的示例將不起作用。類Scanner的'next()'方法讀取第一個空間的空白。所以如果輸入是'5 5 5','scanner.next()'將只讀取第一個'5'。 – BackSlash 2014-09-04 08:51:11

+0

使用scanner.nextLine(); – sreenivas 2014-09-05 11:33:55

+0

我知道,我只是指出你的解決方案是錯誤的,你應該更新你的答案 – BackSlash 2014-09-05 11:39:18

相關問題