2016-03-05 58 views
0

我正嘗試使用菜單系統創建一個小型酒店應用程序。到目前爲止,用戶被要求輸入一個房間號碼(0-9)和一個房間名稱,一旦輸入,我希望用戶返回到其他菜單選項可以輸入的菜單。我不認爲其他菜單選項目前正在要麼:(名稱後的菜單循環,Java

這裏是我到目前爲止的代碼:

package hotel; 
import java.util.*; 

public class Hotel2 { 

    /** 
    * @param args the command line arguments 
    */ 
    public static void main(String[] args) { 
     Scanner input = new Scanner(System.in); 

     Room[] myHotel = new Room[10]; 
     myHotel[0] = new Room(); 
     myHotel[1] = new Room(); 
     myHotel[2] = new Room(); 
     myHotel[3] = new Room(); 
     myHotel[4] = new Room(); 
     myHotel[5] = new Room(); 
     myHotel[6] = new Room(); 
     myHotel[7] = new Room(); 
     myHotel[8] = new Room(); 
     myHotel[9] = new Room(); 

     String roomName; 
     String menuEntry = null; 
     int roomNum = 0; 
     String[] hotel = new String[11]; 
     for (int x = 0; x < 10; x++) hotel[x] = ""; 

     initialise(hotel); 

     while (roomNum < 10) 
     { 

      System.out.println("Please enter one of the following options:\n1) Add customer\n2) Delete customer from room\n3)View all empty rooms\n4) Find a customer\n5) Load program from text file\n6) Order rooms alphabetically\n7) Store program into text file\n8) View all rooms\nInput:"); 
      menuEntry = input.next(); 

      while (menuEntry.equals("1")) 
      { 
       System.out.println("Enter room number (0-9):"); 
       roomNum = input.nextInt(); 

       if (roomNum < 10) 
       { 
         System.out.println("Enter name for room " + roomNum +" :") ; 
         roomName = input.next(); 
         hotel[roomNum] = roomName ;  
       } 
      } 


      if (menuEntry.equals("V")) 
      { 
       for (int x = 0; x < 10; x++) 
       { 
       System.out.println("room " + x + " occupied by " + hotel[x]); 
       } 
      } 

      if (menuEntry.equals("E")); 
      { 

      } 

      if (menuEntry.equals ("D")) 
      { 
      System.out.println("Enter the room number which you would like to delete a customer from:"); 
      roomNum = input.nextInt(); 
      hotel[roomNum] = "empty"; 
      } 
     } 
    } 

    private static void initialise(String hotelRef[]) { 
     for (int x = 0; x < 10; x++) hotelRef[x] = "empty"; 
     System.out.println("initilise\n"); 
    } 
} 
+0

你應該正確地格式化代碼。 – MikeCAT

+0

@MikeCat固定感謝 – Oscar

+0

我建議不要擔心你的文本用戶界面和更多關於該問題的API。讓您的酒店和房間抽象化,用戶界面將變得簡單。兩個提示:沒有幻數(例如10個房間)並使用循環來填充該房間陣列。 – duffymo

回答

1

while循環爲menuEntry1不同的等待,但你永遠不會改變menuEntry內循環,將其更改爲do while循環

do { 
    System.out.println("Please enter one of the following options:\n1) Add customer\n2) Delete customer from room\n3)View all empty rooms\n4) Find a customer\n5) Load program from text file\n6) Order rooms alphabetically\n7) Store program into text file\n8) View all rooms\nInput:"); 
    menuEntry = input.next(); 
    System.out.println("Enter room number (0-9):"); 
    roomNum = input.nextInt(); 
    if (roomNum < 10) 
    { 
     System.out.println("Enter name for room " + roomNum +" :") ; 
     roomName = input.next(); 
     hotel[roomNum] = roomName ;  
    } 
} while (menuEntry.equals("1")); 

你也應該插入的選項進入循環休息,並匹配選項的條件s在菜單中。

while

System.out.println("Please enter one of the following options:\n1) Add customer\n2) Delete customer from room\n3)View all empty rooms\n4) Find a customer\n5) Load program from text file\n6) Order rooms alphabetically\n7) Store program into text file\n8) View all rooms\nInput:"); 
menuEntry = input.next(); 

while (menuEntry.equals("1")) { 
    System.out.println("Enter room number (0-9):"); 
    roomNum = input.nextInt(); 
    if (roomNum < 10) 
    { 
     System.out.println("Enter name for room " + roomNum +" :") ; 
     roomName = input.next(); 
     hotel[roomNum] = roomName ;  
    } 

    System.out.println("Please enter one of the following options:\n1) Add customer\n2) Delete customer from room\n3)View all empty rooms\n4) Find a customer\n5) Load program from text file\n6) Order rooms alphabetically\n7) Store program into text file\n8) View all rooms\nInput:"); 
    menuEntry = input.next(); 
} 
+0

感謝您的回覆,我將如何完全展示這一點?我是新來的Java – Oscar

+0

它似乎仍然不能正常工作,我想我的循環設置錯了? – Oscar

+0

@JamesPatterson嘗試'do while'循環 – Guy

1

像這樣的事情的另一種選擇幫助你的代碼看起來更清潔和理解

while (roomNum < 10 && menuEntry.equals("1")){ 
    //then perform your if statement 
    // or preferabley use a switch(menuEntry){} 
}