【问题标题】:Order in while loop when using hasNextInt statement使用 hasNextInt 语句时在 while 循环中排序
【发布时间】:2018-07-23 08:21:44
【问题描述】:

我刚开始接触java。我正在尝试编写一个使用用户输入将数字添加到 ArrayList 的代码。我找到了一种运行 while 循环的方法,当我使用“hasNextInt”语句输入除数字以外的任何内容时该循环停止,但由于某种原因,它直接输入,然后才从第一个 if 语句中打印出消息。我做错了什么?

输出如下所示:

This program takes grades from 1 to 100 You may begin typing numbers now To stop setting grades, type any word (like 'done') Enter the 1st number: 68 54 Enter the 2nd number 94 Enter the 3rd number

这是我写的代码:

public static void main(String args[]){
    Scanner input = new Scanner(System.in);
    ArrayList<Integer> myClassroom = new ArrayList<Integer>();
    GradeAnalyzer myAnalyzer = new GradeAnalyzer();
    System.out.println("This program takes grades from 1 to 100");
    System.out.println("You may begin typing numbers now");
    System.out.println("To stop setting grades, type any word (like 'done') ");
    int counter = 1;
    System.out.println("Enter the 1st number:");
    while(input.hasNextInt()) {
        if (counter == 21 || counter == 31 || counter == 41 || counter == 51 || counter == 61) {
            System.out.println("Enter the " + counter + "st" + " number");
        } else if (counter == 2 ||counter == 22 ||counter == 32 ||counter == 42 ||counter == 52 ||counter == 62) {
            System.out.println("Enter the " + counter + "nd" + " number");
        } else if (counter == 3 ||counter == 23 ||counter == 33 ||counter == 43 ||counter == 53 ||counter == 63){
            System.out.println("Enter the " + counter + "rd" + " number");
        } else if (counter == 1) {
            System.out.print("");
        } else {
            System.out.println("Enter the " + counter + "th" + " number");
        }
        int cijfer = input.nextInt();
        if(cijfer < 0 || cijfer > 100) {
            System.out.println("Please enter a number between 1 and 100.");
        } else {
            myClassroom.add(cijfer);
            counter++;
        }
    }  
    System.out.println("You entered " + counter + " valid numbers.");
}

【问题讨论】:

  • hasNextInt() 阻塞,直到它知道是否有下一个 Int 。在您输入第二个数字之前,它无法返回任何内容。请注意,对于第一个数字,您如何打印消息,然后调用 hasNextInt()。对于第二个数字,你做相反的事情。

标签: java if-statement while-loop user-input


【解决方案1】:

这是因为hasNextInt 方法会阻塞,直到您输入输入。当您到达int cijfer = input.nextInt(); 时,扫描仪将立即获取您输入的号码,并将其添加到列表中。然后循环将再次调用input.hasNextInt(),这将在打印您的文本之前要求另一个号码。

要修复它,您可以将获取输入的代码移至循环顶部。像这样:

while(input.hasNextInt()) {
    int cijfer = input.nextInt();
    if(cijfer < 0 || cijfer > 100) {
        System.out.println("Please enter a number between 1 and 100.");
    } else {
        myClassroom.add(cijfer);
        counter++;
    }

    if(counter == 21 || counter == 31 || counter == 41 || counter == 51 || counter == 61) {
        System.out.println("Enter the " + counter + "st" + " number");
    } else if(counter == 2 ||counter == 22 ||counter == 32 ||counter == 42 ||counter == 52 ||counter == 62) {
        System.out.println("Enter the " + counter + "nd" + " number");
    } else if(counter == 3 ||counter == 23 ||counter == 33 ||counter == 43 ||counter == 53 ||counter == 63){
        System.out.println("Enter the " + counter + "rd" + " number");
    } else if(counter == 1) {
        System.out.print("");
    } else {
        System.out.println("Enter the " + counter + "th" + " number");
    }
}

PS。当您在此处退出循环时,counter 变量将是一个。你可以自己解决这个问题;)

【讨论】:

  • @Broeskoenoe 如果对您有帮助,请随时接受答案;)
  • 不知道它是这样工作的,我接受了答案!
猜你喜欢
  • 2014-12-21
  • 1970-01-01
  • 2017-05-02
  • 2015-07-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-11-07
相关资源
最近更新 更多