Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如果不创建无限循环来创建菜单,就很难找到正确的使用循环的方法。。。全文_Java_Loops_If Statement_Netbeans_While Loop - Fatal编程技术网

Java 如果不创建无限循环来创建菜单,就很难找到正确的使用循环的方法。。。全文

Java 如果不创建无限循环来创建菜单,就很难找到正确的使用循环的方法。。。全文,java,loops,if-statement,netbeans,while-loop,Java,Loops,If Statement,Netbeans,While Loop,因此,我正在为兽医诊所创建一个假菜单,我需要显示菜单并使用用户输入 决定显示什么。。。顺便说一句,在我的代码中,我刚开始使用它,我正在胡乱使用2,所以我试图确保它是一个int,然后我需要提示用户输入更多信息,等等。。。真正的问题是如何编写一个函数循环,将用户输入的数字从1取到8,然后输出列表中的任何内容 输出应如下所示: switch (selection1) { case 1: doDisplayGraphic(); break; case 2:

因此,我正在为兽医诊所创建一个假菜单,我需要显示菜单并使用用户输入 决定显示什么。。。顺便说一句,在我的代码中,我刚开始使用它,我正在胡乱使用2,所以我试图确保它是一个int,然后我需要提示用户输入更多信息,等等。。。真正的问题是如何编写一个函数循环,将用户输入的数字从1取到8,然后输出列表中的任何内容

输出应如下所示:

switch (selection1) {
    case 1:
        doDisplayGraphic();
        break;
    case 2:
        doAddClient();
        break;
    ...
}
菜单:

1显示图形

2添加客户端

3添加动物

4增加雇员

5列出客户名单

6列出动物

7.列出雇员名单

8退出

请选择:5

1戴夫·罗伯茨,123-4567

2苏西·史密斯,234-2345

3爱丽丝·琼斯,789-2345

史蒂文·布莱克,234-4567

    int selection1;       
    do                 //sorry didnt copy scanner in, i do have one.
    {
        System.out.println("Menu:");
        System.out.println("1) Display Graphic" );
        System.out.println("2) Add Client");
        System.out.println("3) Add Animal");
        System.out.println("4) Add Employee");
        System.out.println("5) List Clients");
        System.out.println("6) List Animals");
        System.out.println("7) List Employees");
        System.out.println("8) Quit"); 

        System.out.println("Please Select A Number:");
        selection1 = sc.nextInt();

        while(!sc.hasNextInt())             //If you think i should use some other loop to make it
                                             // easier let me know.
        {
            sc.nextLine();
            System.out.println("Please Enter Valid Number:");
        }
        selection1 = sc.nextInt();
        if(selection1 == 2)
        {
            do
            {
                System.out.println("Name:");
                sc.nextLine();

            }while(selection1==0);

        }

    }while(selection1 >0 && selection1 <9); // <-- infinate loop if user enters number between 1 and 8 but i need input from 1 to 8 to start loop.

在第一个while循环上方有一个额外的selection1=sc.nextInt。另外,由于8退出,终止循环条件应该是..&&selection1<8,而不是9。我的意思是它看起来基本正确,我会将if链重构为如下内容:

switch (selection1) {
    case 1:
        doDisplayGraphic();
        break;
    case 2:
        doAddClient();
        break;
    ...
}

你有什么问题吗?另外,您从未创建过名为SCX的扫描仪。我不知道在while条件中放置什么,该条件将接收读入值,而不会使其进入无限循环。谢谢,我想我会按照您的方式尝试,是否有方法确保在使用switch语句时它是int。如果sc.haxNextInt和sc.nextInt正常工作,然后selection1将始终是一个int。无论如何,如果其中任何一个返回错误的类型,您将得到一个编译错误。但是sc.nextInt只是提示一些输入,因此如果我要求用户输入int,他们键入了一个字母,那么程序将崩溃,但我的程序无法运行,需要重新提示他们键入int。nextInt的返回类型是什么?另外,如果你能把nextInt的尸体贴在这里,我们可以看看