2013-10-06 87 views
0

我想要求用戶輸入任意數量的數字最多5個,每個數字由空格分隔。閱讀用戶輸入

例如

輸入最多5個數字:3 4 5

我將它們添加在整數和,隨後可以通過反分裂他們

到得到這些數字的平均值。

但是,我的循環似乎沒有結束。我的代碼有什麼問題?

int counter = 0 , sum = 0; 

    Scanner scan = new Scanner(System.in); 

    System.out.println("enter up to 5 numbers"); 

    while(scan.hasNextInt());{ 
    counter++; 
    sum += scan.nextInt(); 
    } 
    System.out.println(counter); 
    System.out.println(sum); 

回答

1

你把while{之間的;,所以它循環。去掉它。

+0

它仍然不會結束,除非I/N在控制檯中。 – Flow

+0

@Flow我很抱歉,直到你在控制檯中什麼東西? –

0

Scanner.hasNextInt()不會做你認爲它所做的事情。它沒有告訴你在已經鍵入的輸入中是否有一個整數(它沒有任何「被鍵入」的概念),而是輸入等待是否可以作爲一個整數來讀取。如果沒有任何輸入已經在等待,它將會阻塞直到出現,所以你的循環將永遠坐在那裏,阻塞更多的輸入。

你可能想要做的是讀一整行,然後將它明確地分割成空格分隔的部分,然後纔將它們解析爲整數。例如,像這樣:

String input = scan.nextLine(); 
for(String part : input.split(" ")) 
    sum += Integer.parseInt(part); 

Serge Seredenko的答案也是正確的,但是,這是另一個問題。

+0

'hasNextInt()'blocks?你確定嗎?那麼它有什麼意義呢? –

+0

重點在於檢查即將到來的輸入是否可以被解析爲整數,或者是否是其他內容,如文本。 – Dolda2000

0

除了在while循環後面的分號(;)之外,代碼中的所有內容都很好,當然這會導致無限循環。

0
int counter = 0 , sum = 0; 

Scanner scan = new Scanner(System.in); 

System.out.println("enter up to 5 numbers"); 

while(scan.hasNextInt()){ 
    counter++; 
    sum += scan.nextInt(); 
    if(counter >=5) 
     break; 
} 
System.out.println(counter); 
System.out.println(sum); 
scan.close(); 

首先,您需要刪除';'位於while(scan.hasNextInt())之後和{之前;對於;意味着while聲明已完成。其次,當你使用你的代碼時,你需要CTRL + Z來結束你的輸入。通過添加

if(counter >=5) 
    break; 

當您輸入5個數字時,您的輸入將最終結束。

+0

這有效,但如果用戶將4個數字而不是5個數字? – Flow

0

如果你想讀整行然後再做算術運算,那麼你不需要用hasNextInt()方法進行while循環。

我會建議你閱讀線,然後拆分空間和迭代字符串數組。查看代碼片段。

package com.gaurangjadia.code.java; 

import java.util.Scanner; 

public class SO19204901 { 

    public static void main(String[] args) { 
     int counter = 0, 
       sum = 0; 

     System.out.println("enter up to 5 numbers"); 

     Scanner scan = new Scanner(System.in); 

     String strInput = scan.nextLine(); 

     String[] arrayNumbers = strInput.split(" "); 

     for (int i = 0; i < arrayNumbers.length; i++) { 
      int n; 

      try { 
       n = Integer.parseInt(arrayNumbers[i]); 
      } 
      catch (NumberFormatException e) { 
       n = 0; 
      } 

      sum = sum + n; 
      counter++; 
     } 

     System.out.println(sum); 

    } 

} 
0
DataInputStream in = new DataInputStream(System.in); 
    String[]label = {"quiz #","Total","Average"}; 
    int counter = 0; 
    int theSum = 0; 

    System.out.print("Enter up to 5 number : "); 
    String[]tempNum = in.readLine().trim().split("\\s+"); 
    System.out.println(); 

while (counter <= tempNum.length) 
{ 
    if (counter == tempNum.length) 
    { 
    System.out.printf("%10s %12s\n",label[1],label[2]); 
    counter = 0; 
    break; 
    } else { 
    System.out.printf("%10s",label[0] + (counter+1)); 
    } 
    counter++; 
} 

while(counter <= tempNum.length) 
{ 
    if (counter == tempNum.length) 
    {System.out.printf("%10d %10.2f\n",theSum,(double)(theSum/counter)); 
    } else 
    {System.out.printf("%10d",Integer.valueOf(tempNum[counter])); 
    theSum += Integer.valueOf(tempNum[counter]); 
    } 
    counter++; 
}