Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/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 - Fatal编程技术网

Java循环之类的

Java循环之类的,java,Java,我一直在试验,我希望我的退出能够识别是或否 如果我选择yes一切都会结束,但是如果我选择no我希望它回到主屏幕 以下是我的实验: public static void Exit() { String user; Scanner in = new Scanner(System.in); System.out.println("Y For yes"); System.out.println("N For No"); System.out.print("\n

我一直在试验,我希望我的
退出
能够识别

如果我选择
yes
一切都会结束,但是如果我选择
no
我希望它回到主屏幕

以下是我的实验:

public static void Exit() {
    String user;
    Scanner in = new Scanner(System.in);
    System.out.println("Y  For yes");
    System.out.println("N  For No");
    System.out.print("\nAre you Sure you want to exit: ");
    user = in.nextLine();

    if (user.equalsIgnoreCase("y")) {
        System.out.println("***Thank you for coming***");
    else if (user.equalsIgnoreCase("n")) {
        switch(user) {
        case n:
            NewStudent();
            break;
        }
    }
}

if语句后缺少一个右括号,case
n

public static void Exit() {

        String user;
        Scanner in = new Scanner(System.in);
        System.out.println("Y  For yes");
        System.out.println("N  For No");
        System.out.print("\nAre you Sure you want to exit: ");
        user = in.nextLine();

          if (user.equalsIgnoreCase("y")) {
            System.out.println("***Thank you for coming***");
          } else if (user.equalsIgnoreCase("n")) {
                    NewStudent();
          }
       }
因为您使用的是if-else,所以代码中不需要switch语句

您可以使用“return”语句退出方法

public static void Exit() {
     String user;
     Scanner in = new Scanner(System.in);
     System.out.println("Y  For yes");
     System.out.println("N  For No");
     System.out.print("\nAre you Sure you want to exit: ");
     user = in.nextLine();

     if (user.equalsIgnoreCase("y")) {
         System.out.println("***Thank you for coming***");
     }
     else if (user.equalsIgnoreCase("n")) {
          switch(user) {
    case n:
        NewStudent();
        //Exit from this method
        return;
    }
}

}

“循环或其他”非常接近于一种可能的解决方案。您可以将菜单包装在
while(true){}
块中,并在用户选择yes时执行
System.exit(0)
,但还有更多选项。也就是说,您的代码不遵循Java编码约定。方法名称应该是camelCased。您想要什么?有什么问题?请查看并发布一篇文章,这里到底有什么问题?不清楚。@aaron,请不要更改建议的代码。。。您添加了一个右括号。。。纠正这里的一个问题(我已经删除了该部分)switch语句的目的是什么?你想返回到什么函数?你应该对解决方案做更多的解释。为什么
案例n
不正确,为什么不需要它。。。