下面我創建了一個酒店計劃,允許客戶留在房間裏。我最近已經能夠創建一個查看我的程序當前所有房間的功能,但是我無法將正確的代碼寫入添加一個客戶裏面的addCustomer()
功能。這是我目前的代碼:酒店計劃 - 添加客戶
package hotelprogram;
import java.util.Scanner;
public class HotelProgram {
// global variables
static String[] hotel = new String[7];
private static void initialise(String hotelRef[]) {
for (int x = 0; x < 6; x++) {
hotelRef[x] = "e";
System.out.println("initilise");
}
}
private static void viewALL() {
// Views all current rooms
for (int x = 0; x < 6; x++) {
if (!hotel[x].equals("e")) {
System.out.println("Room " + x + " occupied by " + hotel[x]);
}else{
System.out.println("Room" + x + " is Empty");
}
}
}
private static void addCustomer()
{
// Add a customer
Scanner sc = new Scanner (System.in);
int roomNumfc;
int roomNamefc;
for (int x = 0; x < 6; x++)
{
System.out.println("Select room number to add a customer: ");
roomNumfc = sc.nextInt();
System.out.println("New customer to add: ");
roomNamefc = sc.nextInt();
}
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String roomName;
int roomNum = 0;
char selectChoice;
for (int x = 0; x < 6; x++) {
hotel[x] = "";
initialise(hotel);
}
while (roomNum < 6) {
// Displays current rooms
for (int x = 0; x < 6; x++) {
// displays an empty room
if (hotel[x].equals("e")) {
System.out.println("Room" + x + " is Empty");
}
}
// Enter a room prompt
System.out.println("Enter a room number from (0-5) or higher to stop (10 to display menu): ");
roomNum = input.nextInt();
if (roomNum == 10) {
// menu if roomNum = 10
System.out.println("Make a selection: ");
System.out.println("A. Add a customer to room");
System.out.println("V. View all rooms");
selectChoice = input.next().charAt(0);
switch (selectChoice) {
case 'A':
addCustomer();
break;
case 'V':
viewALL();
break;
default:
System.out.println("Bad input, exiting program...");
break;
}
break;
}
// Name current room promt
System.out.println("Please name this room: " + roomNum + " :");
roomName = input.next();
hotel[roomNum] = roomName;
// Room displayed
for (int x = 0; x < 6; x++) {
if (!hotel[x].equals("e")) {
System.out.println("Room " + x + " occupied by " + hotel[x]);
}
}
}
}
}
我不太確定如何訪問所使用的全局數組,真的可以在這方面使用一些幫助。提前致謝!