我正嘗試使用菜單系統創建一個小型酒店應用程序。到目前爲止,用戶被要求輸入一個房間號碼(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");
}
}
你應該正確地格式化代碼。 – MikeCAT
@MikeCat固定感謝 – Oscar
我建議不要擔心你的文本用戶界面和更多關於該問題的API。讓您的酒店和房間抽象化,用戶界面將變得簡單。兩個提示:沒有幻數(例如10個房間)並使用循環來填充該房間陣列。 – duffymo