【发布时间】:2015-04-14 03:12:27
【问题描述】:
NetBeans 不允许我运行我的程序,因为公共类 HotelManagement 和公共类 Room 存在问题。谁能帮我解决这个问题,这样我的程序才能顺利运行。谢谢
public class HotelManagement {
public static void main(String[] args) {
Room room1 = new Room(3348, 80, "Single Bed");
Room room2 = new Room(3347, 90, "Double Bed");
Room room3 = new Room(3346, 140, "Suite");
room1.Book();
room2.printRoomInfo();
room3.setRate(180);
room1.Release();
room1.printRoomInfo();
room3.printRoomInfo();
}
}
public class Room {
private final int number;
private final String type;
private int rate;
private boolean isBooked;
public Room(int number, int rate, String type) {
this.number = number;
this.type = type;
this.rate = rate;
this.isBooked = false;
}
public void Book() {
System.out.println("Room " + this.number + " has successfully been booked.");
this.isBooked = true;
}
// This method "releases" the booking on the room by setting the instance variable isBooked to false
public void Release() {
System.out.println("Room " + this.number + " has successfully been released.");
this.isBooked = false;
}
// Print basic info about the current Room objects state
public void printRoomInfo() {
String state;
if (this.isBooked == true) {
state = "Unavailable";
}
else {
state = "Available";
}
System.out.println("Room " + this.number + " is a " + this.type + ". Rate is: " + this.rate + "$ per night. " + state + ".");
}
public void setRate(int newRate) {
this.rate = newRate;
}
}
【问题讨论】:
-
您的程序对我来说运行良好,您是否将所有代码放在一个文件中?
-
是的,每个文件都有一个顶级类。
-
@RobertMoskal 您在一个文件中只能有一个“公共”类。因此,您应该将除“HotelManagement”以外的所有课程设为非公开课程或遵循以下答案。