How can i repeat statement "enter marks or grade" with in while loop.In this case every time control back to start of loop to enter subject again?

huangapple go评论77阅读模式
英文:

How can i repeat statement "enter marks or grade" with in while loop.In this case every time control back to start of loop to enter subject again?

问题

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

继续控制会回到循环开始,并提示用户重新输入科目。是否有可能允许用户输入分数,直到用户想要离开?

英文:

How can i repeat statement "enter marks or grade" with in while loop.In this case every time control back to start of loop. and i want to repeat enter marks until user want to leave?


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

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

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

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

After continue control went to start of loop and ask to enter subject again. Is it possible to enter grade until user wants to leave?

答案1

得分: 1

你需要在第一个循环内部再加入一个 while 循环,如果我理解正确的话,你想在进入 "change" 后更改主题,如果是这样,那么这段代码将会有效:

public static void main(String args[])
{
    Scanner s = new Scanner(System.in);
    System.out.println("科目列表如下:");
    System.out.println("1-物理");
    System.out.println("2-数学");
    System.out.println("3-计算机");

    setSubject:
    while (true)
    {
        System.out.println("请输入科目编号:");
        String sub = s.nextLine();

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

            int marks = 0;
            while (true)
            {
                System.out.println("输入分数 / 输入 change 更改科目 / 输入 exit 退出");
                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("请输入有效选项");
                        continue;
                    }
                }

                if (marks < 40)
                {
                    System.out.println("学生不及格");
                } else if (marks == 40)
                {
                    System.out.println("学生不及格,需要更多练习");
                } else if (marks < 70)
                {
                    System.out.println("需要更多练习,但也不错");
                } else if (marks == 70)
                {
                    System.out.println("良好");
                } else if (marks < 90)
                {
                    System.out.println("不错,也很优秀");
                } else if (marks == 90)
                {
                    System.out.println("优秀");
                } else if (marks < 100)
                {
                    System.out.println("杰出");
                } else if (marks == 100)
                {
                    System.out.println("不错,也很优秀");
                } else
                {
                    System.out.println("");
                }
            }
        }
    }
}

一个样本运行

科目列表如下:
1-物理
2-数学
3-计算机
请输入科目编号:
1
物理
输入分数 / 输入 change 更改科目 / 输入 exit 退出
change
请输入科目编号:
1
物理
输入分数 / 输入 change 更改科目 / 输入 exit 退出
50
需要更多练习,但也不错
输入分数 / 输入 change 更改科目 / 输入 exit 退出
60
需要更多练习,但也不错
输入分数 / 输入 change 更改科目 / 输入 exit 退出
exit
进程以退出代码 0 结束

使用一个 while 循环:

public static void main(String args[])
{
    Scanner s = new Scanner(System.in);
    System.out.println("科目列表如下:");
    System.out.println("1-物理");
    System.out.println("2-数学");
    System.out.println("3-计算机");

    String subject = "";
    int marks = 0;

    while (true)
    {
        if (subject.isEmpty())
        {
            System.out.println("请输入科目编号:");
            subject = s.nextLine();
        }

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

            System.out.println("输入分数 / 输入 change 更改科目 / 输入 exit 退出");
            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("请输入有效选项");
                    continue;
                }
            }

            if (marks < 40)
            {
                System.out.println("学生不及格");
            } else if (marks == 40)
            {
                System.out.println("学生不及格,需要更多练习");
            } else if (marks < 70)
            {
                System.out.println("需要更多练习,但也不错");
            } else if (marks == 70)
            {
                System.out.println("良好");
            } else if (marks < 90)
            {
                System.out.println("不错,也很优秀");
            } else if (marks == 90)
            {
                System.out.println("优秀");
            } else if (marks < 100)
            {
                System.out.println("杰出");
            } else if (marks == 100)
            {
                System.out.println("不错,也很优秀");
            } else
            {
                System.out.println("");
            }
            marks = 0;
        }
    }
}
英文:

You need another while loop inside the first one, if i understand right, you want to change the subject once you enter change? if that is the case then this will work:

public static void main(String args[])
{
Scanner s = new Scanner(System.in);
System.out.println(&quot;Here is Subject list&quot;);
System.out.println(&quot;1-Physics&quot;);
System.out.println(&quot;2-Math&quot;);
System.out.println(&quot;3-computer&quot;);
setSubject:
while (true)
{
System.out.println(&quot;Enter subject number here&quot;);
String sub = s.nextLine();
if (sub.equals(&quot;1&quot;))
{
System.out.println(&quot;Physics&quot;);
int marks = 0;
while (true)
{
System.out.println(&quot;Enter marks  /Enter change for change  /Enter exit for leave&quot;);
if (s.hasNextInt())
{
marks = s.nextInt();
s.nextLine();
} else
{
String command = s.nextLine();
if (command.equalsIgnoreCase(&quot;exit&quot;)) break setSubject;
else if (command.equalsIgnoreCase(&quot;change&quot;)) continue setSubject;
else
{
System.out.println(&quot;Please enter a valid option&quot;);
continue;
}
}
if (marks &lt; 40)
{
System.out.println(&quot;Student is fail&quot;);
} else if (marks == 40)
{
System.out.println(&quot;Student is fail he need more practice&quot;);
} else if (marks &lt; 70)
{
System.out.println(&quot;need more practice but also good&quot;);
} else if (marks == 70)
{
System.out.println(&quot;Good&quot;);
} else if (marks &lt; 90)
{
System.out.println(&quot;Good but also Excellent&quot;);
} else if (marks == 90)
{
System.out.println(&quot;Excellent&quot;);
} else if (marks &lt; 100)
{
System.out.println(&quot;Outstanding&quot;);
} else if (marks == 100)
{
System.out.println(&quot;Good but also excellent&quot;);
} else
{
System.out.println(&quot;&quot;);
}
}
}
}
}

A Sample Run

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

With one while loop:

public static void main(String args[])
{
Scanner s = new Scanner(System.in);
System.out.println(&quot;Here is Subject list&quot;);
System.out.println(&quot;1-Physics&quot;);
System.out.println(&quot;2-Math&quot;);
System.out.println(&quot;3-computer&quot;);
String subject = &quot;&quot;;
int marks = 0;
while (true)
{
if (subject.isEmpty())
{
System.out.println(&quot;Enter subject number here&quot;);
subject = s.nextLine();
}
if (subject.equals(&quot;1&quot;))
{
System.out.println(&quot;Physics&quot;);
System.out.println(&quot;Enter marks  /Enter change for change  /Enter exit for leave&quot;);
if (s.hasNextInt())
{
marks = s.nextInt();
s.nextLine();
} else
{
String command = s.nextLine();
if (command.equalsIgnoreCase(&quot;exit&quot;)) break ;
else if (command.equalsIgnoreCase(&quot;change&quot;)) {
subject = &quot;&quot;;
continue ;
}
else
{
System.out.println(&quot;Please enter a valid option&quot;);
continue;
}
}
if (marks &lt; 40)
{
System.out.println(&quot;Student is fail&quot;);
} else if (marks == 40)
{
System.out.println(&quot;Student is fail he need more practice&quot;);
} else if (marks &lt; 70)
{
System.out.println(&quot;need more practice but also good&quot;);
} else if (marks == 70)
{
System.out.println(&quot;Good&quot;);
} else if (marks &lt; 90)
{
System.out.println(&quot;Good but also Excellent&quot;);
} else if (marks == 90)
{
System.out.println(&quot;Excellent&quot;);
} else if (marks &lt; 100)
{
System.out.println(&quot;Outstanding&quot;);
} else if (marks == 100)
{
System.out.println(&quot;Good but also excellent&quot;);
} else
{
System.out.println(&quot;&quot;);
}
marks = 0;
}
}
}

huangapple
  • 本文由 发表于 2020年3月16日 16:40:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/60702689.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定