【问题标题】:cannot back to previous statement after a user input用户输入后无法返回上一个语句
【发布时间】:2019-04-20 17:04:30
【问题描述】:

这是主菜单:

    System.out.println("Main menu");
    System.out.print("(1) User login \n(2) Sign up \n(3) Exit \n==> ");
    int inp = s.nextInt();
    do{ some code }

我想在这里用户输入4后返回第一个菜单

} while(inp!=4);{
                    System.out.println("=== Logged out ===");
                }
            }

有什么方法我不能解决这个问题吗?

【问题讨论】:

标签: java


【解决方案1】:

我不知道我是否真的得到了你想要做的事情,但是如果你想在用户选择给定选项(数字 4)后继续显示主菜单,那么将它全部放在 while() 循环中应该是有用,一旦它总是回到那段代码的开头,直到给定条件为假。

while(true) {
    System.out.println("Main menu");
    System.out.print("(1) User login \n(2) Sign up \n(3) Exit \n==> ");
    int inp = s.nextInt();
    do { 
        some code;
    } while(inp!=4);
    System.out.println("=== Logged out ===");
}

【讨论】:

    【解决方案2】:

    也许这会有所帮助

    static int inp;
    static void mainMenu() {
        Scanner s = new Scanner(System.in);
        System.out.println("Main menu");
        System.out.print("(1) User login \n(2) Sign up \n(3) Exit \n==> ");
        inp = s.nextInt();
    }
    
    public static void main(String[] args) {
                mainMenu();
            do{ 
    
            }   
            while(inp!=4);{
                System.out.println("=== Logged out ===");
            }
            if(inp == 4) {
                mainMenu();
            }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-21
      • 1970-01-01
      • 2020-09-26
      • 1970-01-01
      相关资源
      最近更新 更多