由于公共类HotelManagement和公共类Room的问题,NetBeans不会让我运行我的程序。任何人都可以帮我解决这个问题所以我的程序将顺利运行。感谢
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;
}
}
答案 0 :(得分:0)
如果你想将两个类放在一个你需要的文件中:
public static class Room {
所以,最终的结构将是这样的:
public class HotelManagement {
public static void main(String[] args) {
...
}
public static class Room {
....
}
}
答案 1 :(得分:0)
在.java文件中只能有一个公共类,它将是具有public static void main的类。如果在同一个文件中的其他类不能公开。
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();
}
}
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;
}
}
答案 2 :(得分:0)
每个源文件只能有一个公共类。因此,要使代码正常工作,您需要执行其中一项操作
public
Room
访问修饰符
醇>
另一种方法是使类Room
保持静态并将其放在类HotelManagement
中。但是,当你非常擅长基本概念时,我建议你学习Java中的静态嵌套类和内部类等高级概念。