繁体   English   中英

Switch语句使第一种情况跳过一行

[英]Switch Statement is making the first case skip a line

因此,我猜想解决方案将非常简单,但我不知道自己在寻找什么,因此需要帮助。 发生的是,当我运行该程序并选择情况1时,它同时打印“狗的名字”和“狗的比赛”,而没有给我机会填写狗的名字。 因此,当我选择情况1时,我开始只是参加狗比赛,它有多沉重,多大年龄! 这是我正在使用的代码...

do {
        System.out.println("(1 - reg\n2 - tail\n3- delete\n4-exit\nEnter number: ");

        // so this is where the switch stuff starts
        int option=sc.nextInt();
        switch (option) {



        case 1: System.out.println("Dog's Name: ");
            String na=sc.nextLine();

            System.out.println("Dog Race: ");
            String ra=sc.nextLine();

            System.out.println("How heavy?");
            double wey=sc.nextDouble();

            System.out.println("How old?");
            double ag=sc.nextDouble();

            dog doggy= new dog(na, ra, wey, ag);
            kennel.add(doggy);
            break;

        case 2: System.out.println("its a tail");
            break;

        case 3: System.out.println("you delete");
            break;

        case 4: System.out.println("QUITTING\n(Data was not saved srry.)");
            play = false;


        default: System.out.println("try again");
            }
        }while(play);

我相信你需要调用nextLine()您的来电后nextInt()因为没有先进的扫描仪到下一行呢。

第一个sc.nextInt中有一个换行符提醒,您可以将定界符更改为\\ n或仅调用nextLine();。 在读取选项之后(使用sc.useDelimiter(“ \\ n”))

尝试:
int option=Integer.parseInt(sc.nextLine());
这既具有将光标前进到下一行并获得键入的数字的效果。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM