有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java运行程序而不结束它

如何选择1、2或3,并在运行程序后返回选择选项菜单。。。 我不希望我的程序在选择1或2后结束我该怎么做。。。提前谢谢

这是我的选择选项程序

public class Dialog
{

        AddList ad = new AddList();
        int select;
        void showDialog()
        {
            System.out.println("Enter The 1 for addnig data");
            System.out.println("Enter The 2 for Waching the MARK data");
            System.out.println("Enter The 3 for Waching the NAME data");
            System.out.println("Enter The 4 for Waching All the data of students");
            System.out.println("Enter The 5 for Waching SUM of the mark of Students");
        }

        void progressInput()
        {

            Scanner scan = new Scanner(System.in);
            select = scan.nextInt();

            if (select == 1)
            {
                ad.AddListIntoArray();
            }
            else if (select == 2)
            {
                ad.PrintMarkFromTheArray();
            }
            else if (select == 3)
            {
                ad.PrintNameFromTheArray();
            }
            else if (select == 4)
            {
                ad.PrintNameMarkFromTheArray();
            }
            else if (select == 5)
            {
                ad.SendMark();
            }
            else
            {
       System.out.println("Please Input range from 1 to 5 and not something else");
            }
        }
}

这是我的主要节目。。。。这里一切正常,但我不想我的程序结束后,选择1或2我的意思是程序1执行,并显示结果,并返回到选择选项菜单

public class Main
{

    public static void main(String[] args)
    {

         Dialog dlg = new Dialog();
         dlg.showDialog();
         dlg.progressInput();

    }

}

共 (2) 个答案

  1. # 1 楼答案

    将代码放入while(true)循环中

    Dialog dlg = new Dialog();
    while(true){
        dlg.showDialog();
        dlg.progressInput();
    }
    
  2. # 2 楼答案

    加上

    System.out.println("Enter The 1 for addnig data");
    System.out.println("Enter The 2 for Waching the MARK data");
    System.out.println("Enter The 3 for Waching the NAME data");
    System.out.println("Enter The 4 for Waching All the data of students");
    System.out.println("Enter The 5 for Waching SUM of the mark of Students");
    System.out.println("Enter The 6 for Exit");
    

    然后

    Scanner scan = new Scanner(System.in);
    int select = 0;
    do {
        System.out.println("Enter your option");
        select = scan.nextInt();
        if (select == 1) {
            ad.AddListIntoArray();
        } else if (select == 2) {
            ad.PrintMarkFromTheArray();
        } else if (select == 3) {
            ad.PrintNameFromTheArray();
        } else if (select == 4) {
            ad.PrintNameMarkFromTheArray();
        } else if (select == 5) {
            ad.SendMark();
        }else if (select == 6) {
            System.out.println("Exiting...");
        }
        else {
            System.out.println("invalid input");
        }
    } while (select != 6);