-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-condition が成功すると、何かを出力してから、 loop1 の次の繰り返しに進みますcontinueキーワードはループ セグメントの最後にあるため、いずれにせよ、次の反復が続行されます。ただし、すべての for ループは 1 回しか実行されないため、次の反復はなく、ループは停止します。

おそらく、より良い解決策は、次のような 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

問題は、ループの長さが 1loop1: 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 に答える
0

何が何をするのか理解していないと思いますcontinueが、ループの残りの部分をスキップして最初からやり直すために使用されます。ループにラベルを付ける必要があるのは、ネストされたループがあり、外側のループに入れたいcontinue(またはbreak外に出したい) 場合だけです。

Student(トピック外:コードが読みやすくなるように、名、姓、マークなどの新しいクラスを作成することをお勧めします。)

于 2013-10-06T20:43:12.320 に答える