-1

成績を取るロジックを作りたい。クラスを使用してユーザーからの入力として合計マークを取得する方法で進み、それがと(両方を含む)Scannerの間にある場合はマークを検証しています。0100

マークがこの範囲内にない場合は、「有効なマークを入力してください!!」と出力し、前のステップに戻ってユーザーに再度入力を求めるようにします。

import java.util.Scanner;
class Performance
{
    public static void main(String[] aa)
    {
        Scanner scnr = new Scanner(System.in);
        System.out.println("Enter the marks :"); // Line 7
        int marks= scnr.nextInt();
        if(marks<0 || marks>100)
        {
            System.out.println("Enter valid marks!!");
        }
    // Now if this condition is true then I want the control to go again to line 7
    // Please suggest me the way to Proceed
    }
}

上記のコードの変更を進める方法を提案してください。

4

5 に答える 5

2

このリンクを参照してください。

あなたはそのようなことをしたい:

do {
   code line 1;
   code line 2;
   code line 3;
} while(yourCondition);

yourConditionが満たされると、コードは再び に進みます(とcode line 1の間のコード ブロックを実行します)。dowhile

これがどのように機能するかを理解したら、これをタスクに簡単に適用できます。

于 2013-04-20T16:13:06.063 に答える
1

これを試して:

boolean b = true;
while(b){

    if(marks<0 || marks>100){
        System.out.println("Enter valid marks!!");
        marks= scnr.nextInt();
     }

     else{
         b= false;
        //Do something
     }
}
于 2013-04-20T16:12:25.187 に答える
1
Scanner scnr = new Scanner(System.in);
do {
    System.out.println("Enter the marks :"); // Line 7
    int marks= scnr.nextInt();
    if(marks<0 || marks>100)
    {
        System.out.println("Enter valid marks!!");
    } else
        break;
} while (true);
于 2013-04-20T16:16:42.573 に答える
0
8     int marks = scnr.nextInt();
9     while(marks<0 || marks>100)
10    {
11        System.out.println("Enter valid marks!!");
12        System.out.println("Enter the marks :");
13        marks = scnr.nextInt();
14    }
于 2013-04-20T16:17:28.407 に答える