2014-10-02 86 views
0

我試圖寫一個程序,它會告訴你:如何添加用戶輸入列表中的數字?

  1. 整數在命令提示符下
  2. 這些整數的和用戶輸入列表中的號碼。

但我在計算出如何訪問這些單個數字時遇到了一些麻煩。我已經試過編寫while循環,以及「if」語句。

我遇到的另一個問題是,當我嘗試運行我的程序時,出現以下錯誤消息:線程「main」java.lang.IllegalStateException中的異常:掃描程序已關閉。

注:我對Java非常陌生,所以主要使用掃描器,下一個方法和hasNext方法的更簡單的解決方案會更好!

import java.util.Scanner; 

public class InputParser 
{ 
    public static void main(String[] args) 
    { 
     Scanner scanner = new Scanner(System.in); 
     System.out.print("How many values do you want to parse?: "); 
     int numValues = scanner.nextInt(); 
     System.out.println("Please enter " + numValues + " values: "); 

     while(scanner.hasNextLine()) 
     { 
     if(scanner.hasNext()) 
     { 
      if(scanner.hasNextInt()) 
      { 
       int sum; 

       System.out.println("The sum of your values is: " + sum + "."); 
      } 
     } 
     scanner.close(); 
     } 
    } 
} 
+2

您可以將每個數字只存儲在一個ArrayList,然後總結所有元素的數組列表設置完畢後獲取數字。 – 2014-10-02 00:39:24

+0

哦!也許我使用了錯誤的詞,我的意思是「命令提示符」是用戶在終端窗口輸入值。對不起,編程還是很新的! – Karen 2014-10-02 00:41:00

+0

這可能是一個愚蠢的問題,但我將如何將每個數字存儲在數組中?我知道如何迭代數組並獲得總數,但是如何存儲數字? – Karen 2014-10-02 00:42:54

回答

1

我不明白你爲什麼會問用戶他們想提前總結多少個數字。基本上,您的代碼可以簡化並處理任意數量的數字。這也是一個真的不好主意致電close()Scanner包裝System.in(因爲你不能重新打開它,並且如果你將它解壓縮到一個方法,你會創建一個難以調試和發現問題)。無論如何,你可以不喜歡,

Scanner scanner = new Scanner(System.in); 
// System.out.print("How many values do you want to parse?: "); 
// int numValues = scanner.nextInt(); 
System.out.println("Please enter values to sum (type quit to stop)"); 
int sum = 0; // <-- start at 0. 
int count = 0; 
while (scanner.hasNext()) { 
    if (scanner.hasNextInt()) { 
     count++; 
     sum += scanner.nextInt(); 
    } else { 
     String str = scanner.next(); 
     if (str.equalsIgnoreCase("quit")) { 
      break; // <-- end the loop. 
     } 
     System.out.printf("The value '%s' is not an int (quit to stop).%n", str); 
    } 
} 
System.out.printf("The sum of your %d values is %d.%n", count, sum); 
// scanner.close(); // <-- Really Bad Idea 

編輯基於您的評論,

Scanner scanner = new Scanner(System.in); 
System.out.println("Please enter values to sum (type quit to stop)"); 
while (scanner.hasNextLine()) { 
    String str = scanner.nextLine(); 
    str = (str != null) ? str.trim() : ""; 
    if (str.equalsIgnoreCase("quit")) { 
     break; // <-- end the loop. 
    } else if (str.length() == 0) { 
     continue; 
    } 
    int sum = 0; // <-- start at 0. 
    int count = 0; 
    Scanner scan2 = new Scanner(str); 
    while (scan2.hasNextInt()) { 
     count++; 
     sum += scan2.nextInt(); 
    } 
    System.out.printf("The sum of your %d values is %d.%n", count, sum); 
} 
+0

中的值。謝謝關於scanner.close()的問題,我沒有意識到把它放在這裏是一件壞事。對不起,Java真的很新鮮。但是,謝謝你的回答!真的有幫助。 – Karen 2014-10-02 00:56:54

+0

@Karen通常情況下,關閉掃描儀就沒問題。不幸的是,'System.in'是一個全局變量,調用'scanner.close()'也會調用'System.in.close()'。 – 2014-10-02 00:59:13

+0

啊,我明白了。好的,謝謝,這是有道理的。 :) – Karen 2014-10-02 00:59:42

2

您的代碼應該是這樣的:

Scanner scanner = new Scanner(System.in); 
    System.out.print("How many values do you want to parse?: "); 
    int numValues = scanner.nextInt(); 
    int[] values = new int(numValues); 
    int sum = 0,i=0; 
    while(i<numValues) 
    { i++; 
    System.out.print("Enter "+ i+" number : "); 
    values[i-1] = scanner.nextInt(); 
    sum+= values[i-1]; 
    } 
    System.out.println("Sum is : "+sum); 
    scanner.close(); 

還沒有真正考慮錯誤處理。

+0

掃描儀讀取多少個數字? – 2014-10-02 00:47:20

+0

啊,我看你做了什麼。如果我想要計算列表中有多少個整數(假設用戶可以輸入一個整數/雙精度列表),那麼看起來像什麼?你提到將數字保存到一個數組我相信? – Karen 2014-10-02 00:51:43

+0

此代碼僅用於整數,請嘗試加倍你的自我。如果你卡住,然後問。請檢查編輯中是否存儲了數組 – StackFlowed 2014-10-02 00:58:44

相關問題