简体   繁体   English

代码未打印任何内容

[英]Code is not printing anything

I am trying to take input from the console, but nothing is getting printed. 我正在尝试从控制台获取输入,但是没有打印任何内容。 I debugged the code and it's correctly storing values in the array, but nothing is getting printed. 我调试了代码,它可以将值正确存储在数组中,但是没有输出任何内容。 I am new to java. 我是Java新手。 Please help. 请帮忙。

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]);
        } 
    }
}

I don't find anything wrong with your code, it may just behave a little different than you expect. 我的代码没有发现任何问题 ,它的行为可能与您预期的有所不同。 So here is how I would do it. 所以这就是我要怎么做。

One thing first: class names should always start with a capital letter (not an error but rather a convention that helps to understand the code) 首先要注意的是:类名应始终以大写字母开头(不是错误,而是有助于理解代码的约定)

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]);
    }
}

This is neither a solution, nor the best way to do it. 这既不是解决方案,也不是最佳方法。 I am merely trying to show you how you can guide your user and handle unwanted input. 我只是想向您展示如何引导用户并处理不需要的输入。

edit : in a nutshell, these things should be considered: 编辑 :简而言之,这些事情应该考虑:

  • don't make any assumption on the intelligence of your user, he/she could input anything: 1 two 2.3 , 4 . @¹" 不要对用户的智能作任何假设,他/她可以输入任何东西: 1 two 2.3 , 4 . @¹" 1 two 2.3 , 4 . @¹"
  • be sure you want 10 numbers, otherwise either use an array of a different size, or a list (if you don't know how many numbers you need) 确保您需要10数字,否则请使用其他大小的数组或列表(如果您不知道需要多少个数字)
  • maybe the user doesn't want to input as many numbers and wants to quit earlier ( if(in.next().equalsIgnoreCase("q") could do the trick ) 也许用户不想输入那么多数字并想更早退出( if(in.next().equalsIgnoreCase("q")可以解决问题))
  • do you accept any integers? 你接受任何整数吗? even negative ones? 甚至消极的?
  • should you accept long as well or even BigInteger ? 你应该接受long以及甚至BigInteger
  • what about floating points? 浮点数呢?
  • and how do you want to handle the error? 以及如何处理该错误? ignore it, replace it with a default value, exit the loop or even the program? 忽略它,将其替换为默认值,退出循环甚至程序?

And here are some example runs: 这是一些示例运行:

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

because the loop will not stop: 因为循环不会停止:

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

so the code cannot be executed: 因此无法执行代码:

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

may be you can use this: 也许你可以使用这个:

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]);
            } 
        } 

You need to tell your loop where to stop waiting for input. 您需要告诉循环在哪里停止等待输入。 If you want to enter a line of integers you could just use nextLine() and use that String instead. 如果要输入integers行,则可以使用nextLine()并使用该String代替。

This example will take one line of input and output valid ints. 本示例将采用一行输入和输出有效int。

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));
    }
}

Input: 输入:

1 2 3.5 foo 50 bar 1234

Output: 输出:

1
2
-1
-1
50
-1
1234

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

   Scanner in = new Scanner(System.in); 

    while (in.hasNextInt()) {

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

    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM