どのように私はすべての時間制御バックが再び件名を入力するようにループの先頭にloop.Inこの場合、しばらくして「マークやグレードを入力し、」文を繰り返すことができますか?

saqibリアズ:

どのように私は文のループの先頭にloop.Inしばらくして、この場合、すべての時間制御バック「マークやグレードを入力し、」繰り返すことができます。そして私は、ユーザが残したいまでマークを入力リピートしたいですか?


public class Marks2 {
    public static void main(String args[]){
        Scanner s=new Scanner(System.in);
        System.out.println("Here is Subject list");
        System.out.println("1-Physics");
        System.out.println("2-Math");
        System.out.println("3-computer");
        boolean input=true;

        while (input){
            System.out.println("Enter subject number here");
            String sub=s.nextLine();

            if(sub.equals("1")) {
                System.out.println("Physics");
                System.out.println("");
                System.out.println("Enter marks  /Enter change for change  /Enter exit for leave");
                String change1 = null;
                String change2 = null;
                int marks = 0;
                try {
                    change1 = s.nextLine();
                    marks = Integer.parseInt(change1);
                } catch (Exception e) {
                    System.out.println("Please enter only string value");
                    change2 = s.nextLine();
                    if (change2.equals("change")) {
                        continue;
                    } else if (change2.equals("exit")) {
                        System.exit(0);
                    }
                }

                if(marks<40){
                    System.out.println("Student is fail");
                }
                else if(marks==40){
                    System.out.println("Student is fail he need more practice");
                }
                else if(marks<70){
                    System.out.println("need more practice but also good");
                }
                else if(marks==70){
                    System.out.println("Good");
                }
                else if(marks<90){
                    System.out.println("Good but also Excellent");
                }
                else if(marks==90){
                    System.out.println("Excellent");
                }
                else if(marks<100){
                    System.out.println("Outstanding");
                }
                else if(marks==100){
                    System.out.println("Good but also excellent");
                }
                else if(change1.equals("change")){
                    continue;
                }
                else if(change2.equals("exit")){
                    System.exit(0);
                }
                else  {
                    System.out.println("");
                }
                continue;
            }
        }
    }
}

後に制御を継続することは、ループの開始と再び件名を入力するようにお願いしました。それは、ユーザが残したいまでのグレードを入力することは可能ですか?

B.Mik:

あなたは私が右理解していれば、あなたが変更を入力したら、件名を変更したい、最初の1の内側に別のwhileループが必要ですか?そのような場合には、これは動作します:

public static void main(String args[])
    {
        Scanner s = new Scanner(System.in);
        System.out.println("Here is Subject list");
        System.out.println("1-Physics");
        System.out.println("2-Math");
        System.out.println("3-computer");

        setSubject:
        while (true)
        {
            System.out.println("Enter subject number here");
            String sub = s.nextLine();

            if (sub.equals("1"))
            {
                System.out.println("Physics");

                int marks = 0;
                while (true)
                {
                    System.out.println("Enter marks  /Enter change for change  /Enter exit for leave");
                    if (s.hasNextInt())
                    {
                        marks = s.nextInt();
                        s.nextLine();
                    } else
                    {
                        String command = s.nextLine();
                        if (command.equalsIgnoreCase("exit")) break setSubject;
                        else if (command.equalsIgnoreCase("change")) continue setSubject;
                        else
                        {
                            System.out.println("Please enter a valid option");
                            continue;
                        }
                    }

                    if (marks < 40)
                    {
                        System.out.println("Student is fail");
                    } else if (marks == 40)
                    {
                        System.out.println("Student is fail he need more practice");
                    } else if (marks < 70)
                    {
                        System.out.println("need more practice but also good");
                    } else if (marks == 70)
                    {
                        System.out.println("Good");
                    } else if (marks < 90)
                    {
                        System.out.println("Good but also Excellent");
                    } else if (marks == 90)
                    {
                        System.out.println("Excellent");
                    } else if (marks < 100)
                    {
                        System.out.println("Outstanding");
                    } else if (marks == 100)
                    {
                        System.out.println("Good but also excellent");
                    } else
                    {
                        System.out.println("");
                    }
                }
            }
        }
    }

サンプルの実行

Here is Subject list
1-Physics
2-Math
3-computer
Enter subject number here
1
Physics
Enter marks  /Enter change for change  /Enter exit for leave
change
Enter subject number here
1
Physics
Enter marks  /Enter change for change  /Enter exit for leave
50
need more practice but also good
Enter marks  /Enter change for change  /Enter exit for leave
60
need more practice but also good
Enter marks  /Enter change for change  /Enter exit for leave
exit

Process finished with exit code 0

whileループ1の場合:

public static void main(String args[])
    {
        Scanner s = new Scanner(System.in);
        System.out.println("Here is Subject list");
        System.out.println("1-Physics");
        System.out.println("2-Math");
        System.out.println("3-computer");

        String subject = "";
        int marks = 0;

        while (true)
        {
            if (subject.isEmpty())
            {
                System.out.println("Enter subject number here");
                subject = s.nextLine();
            }

            if (subject.equals("1"))
            {
                System.out.println("Physics");

                System.out.println("Enter marks  /Enter change for change  /Enter exit for leave");
                if (s.hasNextInt())
                {
                    marks = s.nextInt();
                    s.nextLine();
                } else
                {
                    String command = s.nextLine();
                    if (command.equalsIgnoreCase("exit")) break ;
                    else if (command.equalsIgnoreCase("change")) {
                        subject = "";
                        continue ;
                    }
                    else
                    {
                        System.out.println("Please enter a valid option");
                        continue;
                    }
                }

                if (marks < 40)
                {
                    System.out.println("Student is fail");
                } else if (marks == 40)
                {
                    System.out.println("Student is fail he need more practice");
                } else if (marks < 70)
                {
                    System.out.println("need more practice but also good");
                } else if (marks == 70)
                {
                    System.out.println("Good");
                } else if (marks < 90)
                {
                    System.out.println("Good but also Excellent");
                } else if (marks == 90)
                {
                    System.out.println("Excellent");
                } else if (marks < 100)
                {
                    System.out.println("Outstanding");
                } else if (marks == 100)
                {
                    System.out.println("Good but also excellent");
                } else
                {
                    System.out.println("");
                }
                marks = 0;
            }
        }
    }

おすすめ

転載: http://43.154.161.224:23101/article/api/json?id=292037&siteId=1