简体   繁体   English

如何让这个while循环一次只提示用户一次

[英]How to I get this while loop to only prompt the user once at a time

// basically i need to promp the user to enter grades and count the number of A's B's and so on. // 基本上我需要提示用户输入成绩并计算 A 和 B 的数量等等。 I cant figure out how to get the loop to loop once per user input.我无法弄清楚如何让循环在每个用户输入时循环一次。 It just spams Enter an exam grade.它只是垃圾邮件 输入考试成绩。 Edit thank you all for the help I figured it out!编辑谢谢大家的帮助,我想通了!

public static void main(String[] args) {
    Scanner input = new Scanner(System.in); 
    int numAs = 0;
    int numBs = 0;
    int numCs = 0;
    int numFs = 0;
    int grade = 0;
    while (grade >= 0 && grade <= 100)
    System.out.println("Enter an exam grade");
    grade = input.nextInt();
    if (grade >= 90) {numAs++;
    }else if (grade >=80 && grade<= 89 ) {numBs++;
    }else if (grade >= 70 && grade <= 79) {numCs++;
    }else if (grade <= 69) {numFs++;
    }else if (grade < 0 || grade > 100) {System.out.println("Error invalid grade entered.");
    }
    }

} }

You are missing your curly braces after your while statement.在您的 while 语句之后,您缺少花括号。 If there are no curly braces specified, the while loop only includes the following statement, not the whole block.如果没有指定花括号,则 while 循环仅包括以下语句,而不包括整个块。

public static void main(String[] args) {
    Scanner input = new Scanner(System.in); 
    int numAs = 0;
    int numBs = 0;
    int numCs = 0;
    int numFs = 0;
    int grade = 0;
    while (grade >= 0 && grade <= 100){
    System.out.println("Enter an exam grade");
    grade = input.nextInt();
    if (grade >= 90) {numAs++;
    }
    else if (grade >=80 && grade<= 89 ) {numBs++;
    }
    else if (grade >= 70 && grade <= 79) {numCs++;
    }
    else if (grade <= 69) {numFs++;
    }
    else if (grade < 0 || grade > 100) {System.out.println("Error invalid grade entered.");
    }

    }
}

If you formatted the code correctly, you would see that your While loop doesn't have curly braces, thus it includes only the following line in it.如果您正确格式化代码,您会看到您的 While 循环没有花括号,因此它仅包含以下行。

Scanner input = new Scanner(System.in);
int numAs = 0;
int numBs = 0;
int numCs = 0;
int numFs = 0;
int grade = 0;
while (grade >= 0 && grade <= 100) 
    System.out.println("Enter an exam grade");
    //WHILE ENDS HERE
grade = input.nextInt();
if (grade >= 90) {
    numAs++;
} else if (grade >= 80 && grade <= 89) {
    numBs++;
} else if (grade >= 70 && grade <= 79) {
    numCs++;
} else if (grade <= 69) {
    numFs++;
} else if (grade < 0 || grade > 100) {
    System.out.println("Error invalid grade entered.");
}

And even if You add the missing { , it doesn't solve multiple issues - what happens on an incorrect input;即使您添加了缺少的{ ,它也不能解决多个问题 - 输入不正确会发生什么; and how do you stop the loop correctly.以及如何正确停止循环。 So all things considered, this might be a bit better:所以考虑到所有的事情,这可能会好一点:

Scanner input = new Scanner(System.in);
int numAs = 0, numBs = 0, numCs = 0, numFs = 0;
int grade = 0;
String in = "";
while (true) {
    System.out.println("Enter an exam grade or Q to exit");
    //WHILE ENDS HERE
    in = input.nextLine();
    if ("Q".equals(in)) {
        System.out.println("That's it!");
        break;
    }
    try {
        grade = Integer.parseInt(in); //get caught if not a num
        if (grade < 0 || grade > 100) { //gets caught if bad num
            throw new Exception();
        }
    } catch (Exception e) {
        System.out.println("Error invalid grade entered.");
    }
    if (grade >= 90)
        numAs++;
    else if (grade >= 80 && grade <= 89)
        numBs++;
    else if (grade >= 70 && grade <= 79)
        numCs++;
    else if (grade <= 69)
        numFs++;
    System.out.println("Saved.");
}
//Continue after breaking the loop

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

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