2013-10-06 75 views
-3
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(); 
    } 
} 

與繼續關鍵字的問題,這是行不通的天氣我們給錯誤的輸入標籤的循環,繼續keyord不起作用

+2

請格式化您的文章 - 不只是縮進代碼(直到代碼字體),而且刪除空白行。此外,提供更多細節 - 「不起作用」是永遠不夠的信息。我還強烈建議您開始遵循Java命名約定。 –

+0

這是什麼'for(int x = 0; x'應該這樣做?你錯過了它的一半? –

+0

是的,這是一個程序,給輸入和計算等級,例如,如果我給一個錯誤的輸入第一個標記爲「100」,我希望該程序將繼續循環(因爲我把標籤循環) – user2814620

回答

2

我不認爲你已經理解了continue關鍵字的含義。你用過的continue在他們目前的位置上不會做任何事情。就拿這一段:

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; 
    } 
} 
0

你的問題是1你的循環長度:loop1: for (int p = 1; p <= 1; p++)一旦重新進入,p<=1被evaluted爲假,循環退出。我建議是這樣的:

 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; 
      } 
     } 
+0

哦...真的謝謝你,它的工作。但是,如果我使用BufferReader掃描儀,這將是一個錯誤信息 未報告的異常IOException;必須被捕獲或聲明爲拋出 \t num = Integer.parseInt(ob1.readLine()); – user2814620

0

我假設你有什麼不明白continue呢,它是用於跳過循環的剩餘部分,並重新開始。標記循環只有在嵌套循環並且想要在外部循環中出現continue(或break以外)時纔是必需的。

(Offtopic:我建議創建一個新的類象Student與姓,名和標誌,使代碼變得更容易閱讀)

0

Java規則#1:如果你使用標籤和跳躍,你的代碼的結構是錯誤的。沒有例外。

因此要解決此問題,您應該將問題更改爲:「如何正確修改此代碼以刪除跳轉?」