2016-10-13 113 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