代码不打印任何东西

时间:2016-10-13 07:46:18

标签: java java.util.scanner

我正在尝试从控制台获取输入,但没有任何内容被打印出来。我调试了代码,它正确地将值存储在数组中,但没有任何内容被打印出来。我是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]);
        } 
    }
}

4 个答案:

答案 0 :(得分: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

答案 1 :(得分: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]);
            } 
        } 

答案 2 :(得分:1)

你需要告诉你的循环在哪里停止等待输入。如果您想输入*ngFor="let state of states" 行,可以使用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

答案 3 :(得分:-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++;

    }