2016-05-17 68 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; 
       } 
      } 
     } 
    } 
}