2016-05-17 66 views
1

我正在寻找能够在创建用户后返回到菜单的方法。任何帮助,将不胜感激。我对编程非常陌生,所以如果你需要额外的信息,请告诉我。简单登录程序,不确定如何返回主菜单。

public static void main(String[] args) { 
     System.out.println("MAIN MENU"); 
     System.out.println(" "); 
     System.out.println("1) Creating a Login"); 
     System.out.println("2) Login"); 
     System.out.println("9) Quit"); 
     System.out.println(" "); 
     System.out.println("Please enter your choice: "); 
     System.out.println(" "); 

     Scanner sc = new Scanner(System.in); 
     int menuOption = sc.nextInt(); 
     while (menuOption != 9) { 
      if (menuOption == 1) { 

       System.out.println("Creating Username, please enter a string:"); 
       String user = sc.nextLine(); 
       user = sc.nextLine(); 
       System.out.println("Creating Password, please enter a string:"); 
       String pass = sc.nextLine(); 
       System.out.println("user is" + user); 
       System.out.println("pass is" + pass); 

       if (menuOption == 2) { 
        System.out.println("Please enter your Username"); 
        String inpUser = sc.nextLine(); 
        System.out.println("Now please enter your Password"); 
        String inpPass = sc.nextLine(); 

        if (inpUser.equals(user) && inpPass.equals(pass)) { 
         System.out.print("Credentials Accepted"); 
        } else { 
         System.out.print("Credentials Declined Please try again."); 
        } 
       } 


       if (menuOption == 9) { 
        break; 
       } 
      } 
     } 
    } 
} 

回答

1

如果你需要重复一段代码,把它放在一个循环中。

0

将菜单移动到while循环中,以便每次重新循环时都打印出来。

例子:

public static void main(String[] args) { 


    Scanner sc = new Scanner(System.in); 
    int menuOption = 0; 
    while (menuOption != 9) { 

     System.out.println("MAIN MENU"); 
     System.out.println(" "); 
     System.out.println("1) Creating a Login"); 
     System.out.println("2) Login"); 
     System.out.println("9) Quit"); 
     System.out.println(" "); 
     System.out.println("Please enter your choice: "); 
     System.out.println(" "); 

     menuOption = sc.nextInt(); 

     switch(menuOption){ 
      case 1: { 
       System.out.println("Creating Username, please enter a string:"); 
       String user = sc.nextLine(); 
       user = sc.nextLine(); 
       System.out.println("Creating Password, please enter a string:"); 
       String pass = sc.nextLine(); 
       System.out.println("user is" + user); 
       System.out.println("pass is" + pass); 
       break; 
      } 
      case 2: { 
       System.out.println("Please enter your Username"); 
       String inpUser = sc.nextLine(); 
       System.out.println("Now please enter your Password"); 
       String inpPass = sc.nextLine(); 
       if (inpUser.equals(user) && inpPass.equals(pass)) { 
        System.out.print("Credentials Accepted"); 
       } else { 
        System.out.print("Credentials Declined Please try again."); 
       } 
       break; 
      } 
      case 9: { 
       break; 
      } 
     } 
    } 
} 
+0

这样,只有菜单选项1工作中。 –

+0

我在编辑帖子时可能错过了编辑器中的支架。我也建议将你的代码移到一个开关中。我会编辑我的答案。 – Underbalanced

+0

@DanGreen我已经更新了我的答案,带有一个开关及其更清晰一点 – Underbalanced

0

如何创建一个新的功能:

private static int menu (final Scanner sc) { 
     System.out.println("MAIN MENU"); 
     System.out.println(" "); 
     System.out.println("1) Creating a Login"); 
     System.out.println("2) Login"); 
     System.out.println("9) Quit"); 
     System.out.println(" "); 
     System.out.println("Please enter your choice: "); 
     System.out.println(" "); 
     return sc.nextInt(); 
} 

,然后改变你的main功能的实现是:

public static void main(String[] args) { 

     Scanner sc = new Scanner(System.in); 
     int menuOption = menu(sc); 
     while (menuOption != 9) { 
      if (menuOption == 1) { 

       System.out.println("Creating Username, please enter a string:"); 
       String user = sc.nextLine(); 
       user = sc.nextLine(); 
       System.out.println("Creating Password, please enter a string:"); 
       String pass = sc.nextLine(); 
       System.out.println("user is" + user); 
       System.out.println("pass is" + pass); 
       menuOption = menu(sc); // everytime option 1 is selected, menu will be shown. 

       if (menuOption == 2) { 
        System.out.println("Please enter your Username"); 
        String inpUser = sc.nextLine(); 
        System.out.println("Now please enter your Password"); 
        String inpPass = sc.nextLine(); 

        if (inpUser.equals(user) && inpPass.equals(pass)) { 
         System.out.print("Credentials Accepted"); 
        } else { 
         System.out.print("Credentials Declined Please try again."); 
        } 
       } 


       if (menuOption == 9) { 
        break; 
       } 
      } 
     } 
    } 
}