我目前正努力让我的程序运作起来。我必须以对象的形式完成所有操作这是我目前在主文件中的内容:
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
Room[] myHotel = new Room[6];
//myHotel[x] = new Room();
myHotel[0] = new Room();
myHotel[1] = new Room();
myHotel[2] = new Room();
myHotel[3] = new Room();
myHotel[4] = new Room();
String roomName = null;
String choice;
int roomNum = 0;
initialise(myHotel);
while ( roomNum < 5 )
{
System.out.println("Hotel Booking Options");
System.out.println("V: To View all rooms");
System.out.println("A: To Add a customer to a room");
choice = input.next();
if (choice.equals("V")) //views all the rooms
{
view(myHotel, roomName);
}
if (choice.equals("A")) //To add a customer to a room
{
System.out.println("Enter room number (0-5) or 6 to stop:" );
roomNum = input.nextInt();
System.out.println("Enter name for room " + roomNum + " : " ) ;
roomName = input.next();
myHotel[roomNum].mainName = roomName ;
//myHotel[roomNum].setName(roomName);
add(myHotel, roomName);
System.out.println(" ");
}
}
}
private static void initialise( Room hotelRef[] ) {
for (int x = 0; x < 5; x++ ) hotelRef[x].mainName = "empty";
System.out.println( "initilise ");
}
public static void view(Room myHotel[], Room roomName){
for (int x = 0; x <5; x++)
{
int z = 0;
Room Roomname = roomName;
myHotel[z]= Roomname;
if (myHotel[x].mainName.equals("empty"))
System.out.println("room " + x + " is empty");
else {
System.out.println("room " + x + " occupied by " + myHotel[x].mainName);
//System.out.println("room " + x + " is occupied by "+ myHotel[x]);
}
}
}
private static void add(Room myHotel[], String roomName){
for (int x = 0; x <5; x++)
{
int z = 0;
String Roomname = roomName;
myHotel[z]= Roomname;
if (myHotel[x].mainName.equals("empty"))
{
System.out.println("room " + x + " is empty");
}
else {
System.out.println("room " + x + " is occupied by "+ myHotel[x]);
}
}
}
}
在我的另一个带有对象变量的.java文件中:
public class Room {
String mainName;
//private String mainName;
int guestsInRoom;
public Room() {
mainName = "k";
System.out.println("made a room ");
}
public void setName(String aName) {
System.out.println("add name class method ");
mainName = aName;
}
public String getName() {
return mainName;
}
}
我不确定如何修复第54行 - view(myHotel,roomName);对不起,如果我的变量名称令人困惑和误导......