开发者社区> 问答> 正文

我该如何在while循环中重复“输入分数或等级”的语句

我该如何在while循环中重复声明“输入标记或等级”。在这种情况下,每次控制都返回到循环的开始。我想重复输入标记,直到用户要离开?

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

继续控制后,进入循环开始并要求再次输入主题。在用户想离开之前是否可以输入成绩?

问题来源:Stack Overflow

展开
收起
montos 2020-03-22 20:22:51 918 0
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循环:

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

    回答来源:Stack Overflow

    2020-03-22 20:23:55
    赞同 展开评论 打赏
问答地址:
问答排行榜
最热
最新

相关电子书

更多
4个迭代,从批量交...1573957773.pdf 立即下载
低代码开发师(初级)实战教程 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载