1

我有应该可以工作的代码,但是底部的大括号给了我一个错误。可能有一个我找不到的简单解决方案,可能是因为我已经筋疲力尽了。这两个代码都属于不同的类。如果我删除一个大括号,那么另一个大括号会给我一个错误。

import java.util.Scanner;

public class question {

    public static void main(String[] args) {
        int a = 0;
        int b = 0;
        int c = 0;
        int d = 0;
        int f = 0;
        int grade = 0;
        Scanner keyboard = new Scanner(System.in);

        System.out.println("Enter grade scores: ");
        do {
            System.out.print("Grade: ");
            grade = keyboard.nextInt();

            if (grade >= 0 && grade < 60) {
                f++;
            } else if (grade < 70) {
                d++;
            } else if (grade < 80) {
                c++;
            } else if (grade < 90) {
                b++;
            } else if (grade <= 100) {
                a++;
            }

            while (grade >= 0) {
                System.out.println("Total scores: " + (a + b + c + d + f));
                System.out.println("A: " + a);
                System.out.println("B: " + b);
                System.out.println("C: " + c);
                System.out.println("D: " + d);
                System.out.println("F: " + f);
            }
        } // this one gives error
    }
}
4

4 回答 4

4

语法是

do {
} while ( boolean expression) ;

你错过了这个while部分

http://docs.oracle.com/javase/tutorial/java/nutsandbolts/while.html

于 2013-07-14T01:51:57.183 回答
4

如果您阅读文档do while循环应该是这样的:

do {
  statement(s)
} while (expression);

你目前拥有的是:

do { 

}

所以你错过了这while (expression) ;部分。

于 2013-07-14T01:53:54.937 回答
2

您缺少代码中的“while”部分。do-while 循环必须有条件在每次迭代后进行检查。您的语法应类似于:

do
{
//code here
}
while (condition-here);

注意:do-while 循环在语句末尾使用分号。不要错过

于 2013-07-14T01:58:00.720 回答
2

我认为您正在尝试这样做:

public static void main(String[] args){


    int a = 0;
    int b = 0;
    int c = 0;
    int d = 0;
    int f = 0;
    int grade = 0;
     Scanner keyboard = new Scanner(System.in);

    System.out.println("Enter grade scores: ");
    do{
        System.out.print("Grade: ");
        grade = keyboard.nextInt();

        if(grade >= 0 && grade < 60){
            f++;
        }
        else if(grade < 70){
            d++;
        }
        else if(grade < 80){
            c++;
        }
        else if(grade < 90){
            b++;
        }
        else if(grade <= 100){
            a++;
        } 

    } while (grade >= 0)

        System.out.println("Total scores: " + (a + b + c + d + f));
        System.out.println("A: " + a);
        System.out.println("B: " + b);
        System.out.println("C: " + c);
        System.out.println("D: " + d);
        System.out.println("F: " + f);

} 
于 2013-07-14T01:58:25.723 回答