繁体   English   中英

代码未打印任何内容

[英]Code is not printing anything

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

我的代码没有发现任何问题 ,它的行为可能与您预期的有所不同。 所以这就是我要怎么做。

首先要注意的是:类名应始终以大写字母开头(不是错误,而是有助于理解代码的约定)

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 . @¹" 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

因为循环不会停止:

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

您需要告诉循环在哪里停止等待输入。 如果要输入integers行,则可以使用nextLine()并使用该String代替。

本示例将采用一行输入和输出有效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));
    }
}

输入:

1 2 3.5 foo 50 bar 1234

输出:

1
2
-1
-1
50
-1
1234

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++;

    }

暂无
暂无

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

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