2016-10-13 170 views
1

我試圖從控制檯輸入,但沒有打印。我調試了代碼,它正確地將值存儲在數組中,但沒有打印出來。我是新來的Java。請幫忙。代碼不打印任何東西

import java.util.Scanner; 

public class noofdays { 

    public static void main(String[] args) { 
     // TODO Auto-generated method stub 

     int[] date = new int[10]; 
     int i = 0; 

     Scanner in = new Scanner(System.in); 

     while (in.hasNextInt()) { 
      date[i]=in.nextInt(); 
      i++; 
     } 

     for(i=0;i<3;i++) 
     { 
      System.out.println(date[i]); 
     } 
    } 
} 
+0

是什麼'while'循環在這裏做什麼? – emotionlessbananas

+0

使用調試器,你會發現什麼是發生的 – Jens

+0

while循環用於從控制檯獲取輸入@AsteriskNinja – user3797489

回答

2

我沒有發現任何錯誤你的代碼,它可能只是表現比預期稍有不同。所以這裏是我將如何做到這一點。

一兩件事第一:類名應該總是以一個大寫字母(不是一個錯誤,而是一個慣例,有助於理解代碼)開始

public static void main(String[] args) throws IOException{ 
    int[] date = new int[10];  // as mentioned above, a fixed size array will limit you - but if 10 is what you want, then this is what you need 
    int i = 0; 

    System.out.println("Please enter " + date.length + " numbers"); // just some output to tell the user that the program has started and what to do next 
    Scanner in = new Scanner(System.in);  // perfect 
    // if you absolutely want your array filled, check if you reached the end of your input to avoid IndexOutOfBoundsExceptions. 
    // in.hasNext() will check for ANY input, which makes it easier to handle unwanted user input 
    while(i < date.length && in.hasNext()){ 
     if(in.hasNextInt()){  // here you check if the input starts with a number. Beware that "1 w 2" is valid too! 
      date[i] = in.nextInt(); 
      i++; 
     }else{ 
      // this is to advise the user of an input error 
      // but more importantly, in.next() will read the invalid input and remove it from the inputstream. Thus your scanner will continue to read the input until it ends 
      System.out.println("sorry \"" + in.next() + "\" is not a valid number"); 
     } 
    } 
    System.out.println("your input:"); 
    for(i = 0; i < date.length; i++){ // you don't need any advanced loops, it is perfectly fine to use indexed loops. Just try to make your break condition more dynamic (like checking the length of the array instead of a constant value) 
     System.out.println(date[i]); 
    } 
} 

這既不是一個解決辦法,也不是最好的辦法做到這一點。我只是想告訴你如何引導你的用戶並處理不需要的輸入。

編輯:概括地說,這些事情應該考慮:

  • 不作任何假設您的用戶的情報,他/她可以輸入任何東西:1 two 2.3 , 4 . @¹"
  • 肯定您需要10數字,否則使用不同大小的數組或列表(如果您不知道需要多少個數字)
  • 也許用戶不想輸入儘可能多的數字並且想要退出早些時候(if(in.next().equalsIgnoreCase("q")可以做的技巧)
  • 你接受任何整數?甚至是負面的?
  • 你應該接受long還是BigInteger
  • 浮點數呢?
  • 以及您想如何處理錯誤?忽略它,用默認值替換它,退出循環甚至程序?

這裏有一些例子運行:

Please enter 10 numbers 
1 
2 
3 4 5 6 7 8 9 
10 
your input: 
1 
2 
3 
4 
5 
6 
7 
8 
9 
10 

Please enter 10 numbers 
1 2 3 4 5 6 7 8 9 10 
your input: 
1 
2 
3 
4 
5 
6 
7 
8 
9 
10 

Please enter 10 numbers 
1 2 3 4 r 5 6 7 8 9 10 
sorry "r" is not a valid number 
your input: 
1 
2 
3 
4 
5 
6 
7 
8 
9 
10 

Please enter 10 numbers 
1 2 3 4 5 6 7 8 9 10 11 
your input: 
1 
2 
3 
4 
5 
6 
7 
8 
9 
10 
-2

int [] date = new int [10]; int i = 0;

Scanner in = new Scanner(System.in); 

    while (in.hasNextInt()) { 

     date[i]=in.nextInt(); 
     System.out.println(date[i]); 
     i++; 

    } 
+2

你介意解釋這是如何解決OP的問題嗎? – GameDroids

1

因爲循環不會停止:

while (in.hasNextInt()) { 
      date[i]=in.nextInt(); 
      i++; 
} 

這樣的代碼不能執行:

for(i=0;i<3;i++) 
     { 
      System.out.println(date[i]); 
} 

可能是你可以使用這個:

public static void main(String[] args){ 
      int[] date = new int[10]; 
      int i = 0; 
      Scanner in = new Scanner(System.in); 
      for(i=0;i<3;i++) { 
       date[i]=in.nextInt(); 
      } 

      for(i=0;i<3;i++) 
      { 
       System.out.println(date[i]); 
      } 
     } 
+0

這是一個正確的答案,你在'while'中遇到了無限循環,這似乎是取代你想要做的顯而易見的選擇。 – px06

+1

@ px06 OP問題中沒有無限循環。 – John

+0

@John它似乎是這樣,因爲掃描儀將繼續閱讀輸入,直到它達到某種「結束」狀態,這似乎不會發生在上下文中。 [這解釋了它更好一點](http://stackoverflow.com/questions/10490344/how-to-get-out-of-while-loop-in-java-with-scanner-method-hasnext-as-condition )。 – px06

1

你需要告訴你的循環在哪裏停止等待輸入。如果你想輸入一行integers,你可以使用nextLine()並使用String來代替。

本示例將採用一行輸入和輸出有效整數。

public static void main(String[] args) { 

    // use a List, unless you want to enter exactly 10 integers 
    List<Integer> date = new ArrayList<Integer>(); 
    int i = 0; 
    String x = ""; 

    Scanner in = new Scanner(System.in); 
    x = in.nextLine(); 

    String [] tokens = x.split(" "); 


    for (int y = 0; y < tokens.length; y++) { 
     try { 
      date.add(Integer.parseInt(tokens[y])); 
     } catch (Exception e) { 
      // error handling 
      date.add(-1); // set a default value instead or just do nothing 
     } 
    } 

    in.close(); // don't forget to close your Scanner 

    for (i = 0; i < date.size(); i++) { 
     System.out.println(date.get(i)); 
    } 
} 

輸入:

1 2 3.5 foo 50 bar 1234 

輸出:

1 
2 
-1 
-1 
50 
-1 
1234 
+0

關閉掃描儀是一個好點! – GameDroids