-4
import java.util.Scanner;

class Details {
    int num;
    String NAMES[][];
    double MARKS[][];
    double TOTAL[];
    String GRADES[];

    void getData() {
        Scanner ob = new Scanner(System. in );
        System.out.print("Enter the number of students : ");
        num = ob.nextInt();
        NAMES = new String[num][2];
        MARKS = new double[num][4];
        for (int x = 0; x
            System.out.print("First Name : ");
            NAMES[x][0] = ob.next();
            System.out.print("Last Name : ");
            NAMES[x][1] = ob.next();
            loop1: 
            for (int p = 1; p <= 1; p++)
            {
                System.out.print("\tFirst Test Marks : ");
                MARKS[x][0] = ob.nextDouble();
                if ((MARKS[x][0] < 0) || (MARKS[x][0] > 15))
                {
                    System.out.println("Marks should be within 0 to 15");
                    continue loop1;
                }
            }
            loop2: 
            for (int p = 1; p <= 1; p++)
            {
                System.out.print("\tMid Term Marks : ");
                MARKS[x][1] = ob.nextDouble();
                if (MARKS[x][1] < 0 || MARKS[x][1] > 20)
                {
                    System.out.println("Marks should be within 0 to 20");
                    continue loop2;
                }
            }
            loop3: 
            for (int p = 1; p <= 1; p++)
            {
                System.out.print("\tLab Test Marks : ");
                MARKS[x][2] = ob.nextDouble();
                if (MARKS[x][2] < 0 || MARKS[x][2] > 15)
                {
                    System.out.println("Marks should be within 0 to 15");
                    continue loop3;
                }
            }
            loop4: 
            for (int p = 1; p <= 1; p++)
            {
                System.out.print("\tFinal Marks : ");
                MARKS[x][3] = ob.nextDouble();
                if (MARKS[x][3] < 0 || MARKS[x][3] > 50)
                {
                    System.out.println("Marks should be within 0 to 20");
                    continue loop4;
                }
            }
            System.out.println();
        }
    }
    public void total() {
        TOTAL = new double[num];
        for (int x = 0; x
            TOTAL[x] = MARKS[x][0] + MARKS[x][1] + MARKS[x][2] + MARKS[x][3];
        }
    }
    public void grades() {
        GRADES = new String[num];
        for (int x = 0; x
            if (TOTAL[x] >= 80) {
                GRADES[x] = "A";
            } else if (TOTAL[x] >= 70) {
                GRADES[x] = "B";
            } else if (TOTAL[x] >= 60) {
                GRADES[x] = "C";
            } else if (TOTAL[x] >= 50) {
                GRADES[x] = "D";
            } else {
                GRADES[x] = "F";
            }
        }
    }
    void print() {
        System.out.println("\t\t\tRESULT SHEET\n\n\tFirst Name\tLast Name\tGrade");
        for (int x = 0; x
            System.out.println("\t" + NAMES[x][0] + "\t\t" + NAMES[x][1] + "\t\t" + GRADES[x]);
        }
    }
}
class test {
    public static void main(String[] args) {
        Details data = new Details();
        data.getData();
        data.total();
        data.grades();
        data.print();
    }
}

continue 关键字的问题,它不起作用天气我们输入错误

4

4 回答 4

2

我认为您没有理解continue关键字的含义。您使用的continues 在当前位置不会做任何事情。以这个片段为例:

loop1: 
for (int p = 1; p <= 1; p++)
{
    System.out.print("\tFirst Test Marks : ");
    MARKS[x][0] = ob.nextDouble();
    if ((MARKS[x][0] < 0) || (MARKS[x][0] > 15))
    {
        System.out.println("Marks should be within 0 to 15");
        continue loop1;
    }
}

当 if 条件成功时,它会打印一些内容,然后继续进行 loop1 的下一次迭代。无论如何,它会继续下一次迭代,因为continue关键字位于循环段的末尾。但是,由于您所有的 for 循环只运行一次,因此没有下一次迭代并且循环停止。

也许更好的解决方案是使用这样的while循环:

while(true) {
    System.out.print("\tFirst Test Marks : ");
    MARKS[x][0] = ob.nextDouble();
    if ((MARKS[x][0] < 0) || (MARKS[x][0] > 15)) {
        System.out.println("Marks should be within 0 to 15");
    } else {
        break;
    }
}
于 2013-10-06T20:12:03.667 回答
0

Java 规则#1:如果你使用标签和跳转,你的代码结构是错误的。没有例外。

因此,要解决此问题,您应该将问题更改为:“如何正确修改此代码以删除跳转?”

于 2013-10-06T21:11:59.997 回答
0

我假设你不明白做什么continue,它用于跳过循环的剩余部分并重新开始。仅当您有嵌套循环并且想要continue(或break退出)外部循环时,才需要标记循环。

(题外话:我建议创建一个新类,如Student名字、姓氏和标记,以便代码更易于阅读。)

于 2013-10-06T20:43:12.320 回答
0

你的问题是你的循环长度为 1:loop1: for (int p = 1; p <= 1; p++)一旦它被重新输入,p<=1被评估为 false 并退出循环。我会建议这样的事情:

        boolean firstConditionSatisfied = false;
        while (!firstConditionSatisfied) {
            System.out.print("\tFirst Test Marks : ");
            MARKS[x][0] = ob.nextDouble();
            if ((MARKS[x][0] < 0) || (MARKS[x][0] > 15)) {
                System.out.println("Marks should be within 0 to 15");
            } else {
                firstConditionSatisfied = true;
            }
        }
于 2013-10-06T20:15:16.403 回答