Java 代码没有打印任何东西

Java 代码没有打印任何东西,java,java.util.scanner,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]; in

我试图从控制台获取输入,但没有打印任何内容。我调试了代码,它正确地将值存储在数组中,但没有打印任何内容。我是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]);
        } 
    }
}
import java.util.Scanner;
公务舱正午{
公共静态void main(字符串[]args){
//TODO自动生成的方法存根
整数[]日期=新整数[10];
int i=0;
扫描仪输入=新扫描仪(系统输入);
while(在.hasNextInt()中){
日期[i]=in.nextInt();
i++;
}
对于(i=0;iint[]日期=新int[10];
int i=0

   Scanner in = new Scanner(System.in); 

    while (in.hasNextInt()) {

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

    }

因为循环不会停止:

while (in.hasNextInt()) {
            date[i]=in.nextInt();
            i++;
 }
因此,代码无法执行:

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

对于(i=0;i你需要告诉你的循环在哪里停止等待输入。如果你想输入一行
整数
,你可以使用
nextLine()
并使用该
字符串

此示例将获取一行输入和输出有效整数

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
-1
-1
50
-1
1234
我没有发现您的代码有任何问题,它的行为可能与您预期的稍有不同

首先一件事:类名应该总是以大写字母开头(不是一个错误,而是一个有助于理解代码的约定)


while
loop在这里做什么?使用调试器,你会发现发生了什么,while loop用于从控制台@asteriskninjayYou确定?只有两件事会导致麻烦,无效输入或冗长输入。我使用了调试器,但调试器在while loop@Jenswould之后不会继续运行。你介意解释一下这个解决方法吗es OPs问题?这是正确的答案,您在
中运行了一个无限循环,而这似乎是替换您试图执行的操作的明显选择。@px06 OP问题中没有无限循环。@John似乎是这样的,因为扫描仪会一直读取输入,直到到达某个“结束”在上下文中似乎没有发生的条件。@px06我也没有在这个上下文中找到任何无限循环question@px06我知道发生了什么。扫描器只是被阻塞,直到字符被读取。但这不是无限循环,根据定义,它总是计算为真。
1 2 3.5 foo 50 bar 1234
1
2
-1
-1
50
-1
1234
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]);
    }
}
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