2016-01-31 41 views
0

我試圖創建一個菜單系統,用戶選擇是否有管理員或客戶,然後他們可以輸入登錄詳細信息。如何在Java中創建嵌套菜單?

登錄管理員後,客戶會看到不同的菜單。我也希望能夠註銷,並且會返回到登錄菜單,詢問他們是否有管理員或客戶。

這是我到目前爲止有:

import java.util.Scanner; 

public class Main { 
    public static void main(String[] args){ 

     login login = new login(); 
     Scanner in = new Scanner(System.in); 

     String user; 
     String pwd; 


     int loginOption = 0; 

     login.databaseConnection(); 

     System.out.println("======================================"); 
     System.out.println("|  Please select and option:  |"); 
     System.out.println("======================================"); 
     System.out.println("| Options:       |"); 
     System.out.println("|  [1] Admin     |"); 
     System.out.println("|  [2] Account Holder   |"); 
     System.out.println("|  [3] Exit     |"); 
     System.out.println("======================================"); 



     do{ 
      System.out.print("Option:"); 
      loginOption = in.nextInt(); 

      // Switch construct 
      switch (loginOption) { 
       case 1: 
        System.out.println(""); 
        System.out.println("Username: "); 
         user = in.next(); 
        System.out.println("Password: "); 
         pwd = in.next(); 
        System.out.println(""); 
        break; 
       case 2: 
        System.out.println(""); 
        System.out.println("Account Number: "); 
         user = in.next(); 
        System.out.println("Password: "); 
         pwd = in.next(); 
        System.out.println(""); 

        if(login.validate_login(user, pwd)){ 
         System.out.println("Valid Login"); 
         System.out.println(""); 
         System.out.println("Hello, Account Holder"); 
         System.out.println(""); 
         System.out.println("======================================"); 
         System.out.println("|  Please select and option:  |"); 
         System.out.println("======================================"); 
         System.out.println("| Options:       |"); 
         System.out.println("|  [1] Check balence   |"); 
         System.out.println("|  [2] Withdraw    |"); 
         System.out.println("|  [3] Logout     |"); 
         System.out.println("======================================"); 

         int userOption = 0; 

         do{ 
          System.out.println("Option"); 
          userOption = in.nextInt(); 
          switch (userOption){ 
           case 1: 
            break; 
           case 2: 
            break; 
           case 3: 
            loginOption = 3; 
            break; 
          } 
         }while (loginOption != 3); 
        }else{ 
         System.out.println("Invalid"); 
        } 
        break; 
       case 3: 
        System.out.println("Exit selected"); 
        System.exit(0); 
        break; 
       default: 
        System.out.println("Invalid selection"); 
        break; // This break is not really necessary 
      } 
     }while (loginOption != 3); 


     in.close(); 
    } 
} 

我在他們時刻離開第二開關,回到第一個主要的問題。

任何幫助,將不勝感激:)

+0

它在正確的'switch(loginOption){'1爲管理員或2爲客戶或3爲退出,將是真實的。如果你選擇1,它會詢問usarname,然後再次將它轉到main。 – Satya

+0

@Satya yerr,它只是一旦他們輸入正確的登錄信息一個新的菜單顯示,我希望有一個選項,他們能夠註銷,並返回到他們選擇的地方,無論是管理員還是客戶。目前該程序剛剛結束 – IeuanW

回答

1

您可以添加一個布爾變量ext這樣的:

     boolean ext = true; 
         do{ 
         ... 
          case 3: 
           loginOption = 3; 
           ext = false; 
           break; 
         } 
        }while (loginOption != 3 && ext); 

有了,你可以從退出while循環時昱歐有loginOption = 3;

+0

真棒,thcx一直卡在那一段時間:'D – IeuanW