使用hasNextInt语句时在while循环中排序

时间:2018-07-22 13:58:21

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

我只是从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.");
}

1 个答案:

答案 0 :(得分:0)

这是因为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变量将为一。您可以自己修复该问题;)