2013-10-06 25 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:如果你使用标签和跳跃,你的代码的结构是错误的。没有例外。

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