英文:
Boolean with multiple classes
问题
我正试图进行一个包含三个类的程序,并传递一个布尔值来预订一个房间。我有驱动程序、建筑物和房间程序。我将预订设置为false,但我无法弄清楚在已设置为true时如何打印出文本语句。我认为我要么在从驱动程序通过类传递布尔值方面做错了,要么漏掉了某些东西。我已经在建筑物类中的reserveRoom中尝试使用if语句来查看它是否已经为true以打印一个语句,无论我选择哪种方式都不起作用。
任何帮助将不胜感激。
谢谢。
从将布尔值发送到建筑物程序的驱动程序:
  System.out.print("您想预订哪个房间?");
  System.out.print(building);
  System.out.print("预订:");
  reservNum = input.nextInt();
  building.reserveRoom(reserve, reservNum);
从我的建筑物类:
public void reserveRoom(boolean reserve, int count)
{
    //类常量
    
    //类变量
    
    /*****************************************************/
    
    room[count].updateReserve(reserve);
    
} // 结束
从我的房间类:
public void updateReserve(boolean newReserve)
{
    //类常量
    
    //类变量
    
    /*****************************************************/
    
    if (newReserve == false)
    {
        roomAvail = true;
    }
    else
    {
        roomAvail = false;
    }
} // 结束
英文:
I am trying to take a 3 class program and pass a boolean for reserving a room. I have driver program, building, room programs. I set the reserve to false and I can't figure out how to print out a text statement when it's already set to true. I think I am either doing the passing of the boolean through the classes from the driver wrong or missing something. I have played with reserveRoom in building class with an if statement to see if it's already true to print a statement and no matter which way I go it doesn't work.
Any help would be appreciated.
Thank you.
From my driver program that sends the boolean to the building program
  System.out.print ("Which room would you like to reserve?");
  System.out.print (building);
  System.out.print ("reserve: ");
  reservNum = input.nextInt();
  building.reserveRoom(reserve, reservNum);
From my building class.
public void reserveRoom (boolean reserve, int count)
{
	//class constant
	//class variables
	/*****************************************************/
	room [count].updateReserve(reserve);
} // end 
From the room class.
	public void updateReserve(boolean newReserve)
{
	//class constant
	//class variables
	/*****************************************************/
	if (newReserve == false)
	{
		roomAvail = true;
	}
	else
	{
		roomAvail = false;
	}
} // END 
答案1
得分: 1
以下是翻译好的内容:
嗯,你的问题中有一些信息不完整,不过我认为你在寻找以下内容:
public void updateReserve(boolean newReserve) {
  if(newReserve && !roomAvail) {
    System.out.println("对不起,这个房间已经被预订");
  } else {
    roomAvail = !newReserve;
  }
}
英文:
Well, there is some information missing in your question, however it think you are looking for:
public void updateReserve(boolean newReserve) {
  if(newReserve && !roomAvail) {
	System.out.println("Sorry this room is taken")
 } else {
 	roomAvail = !newReserve;
 }
}
答案2
得分: 0
根据我对您问题的理解,以下是我整理出的代码部分的翻译:
public class Reservation {
    public static void main(String args[]) {
        Building building = new Building(20);
        boolean reserve = false;
        System.out.println("您想要预订哪个房间?");
        System.out.println(building);
        System.out.println("reserve: ");
        int reservNum = 2;
        building.reserveRoom(reserve, reservNum);
        System.out.println("是否已预订?:" + building.getRoom(reservNum).getRoomAvail());
    }
}
class Building {
    Room room[];
    public Building(int numOfRooms) {
        room = new Room[numOfRooms];
        for (int i = 0; i < numOfRooms; i++) {
            room[i] = new Room();
        }
    }
    public String toString() {
        return "这栋楼有 " + room.length + " 个房间";
    }
    public Room getRoom(int roomNum) {
        return room[roomNum];
    }
    public void reserveRoom(boolean reserve, int count) {
        room[count].updateReserve(reserve);
    }
}
class Room {
    boolean roomAvail;
    public boolean getRoomAvail() {
        return roomAvail;
    }
    public void updateReserve(boolean newReserve) {
        if (newReserve == false) {
            roomAvail = true;
        } else {
            roomAvail = false;
        }
    }
}
请注意,我只对代码部分进行了翻译。如果您有任何问题或需要进一步的帮助,请随时提问。
英文:
With whatever i could understand about your question this is what i came up with -
    public class Reservation {
public static void main(String args[]) {
Building building = new Building(20);
boolean reserve= false;
System.out.println("Which room would you like to reserve?");
System.out.println(building);
System.out.println("reserve: ");
int reservNum = 2;
building.reserveRoom(reserve, reservNum);
System.out.println("Is Reserved?:"+building.getRoom(reservNum).getRoomAvail());
}
}
class Building {
Room room[];
public Building(int numOfRooms) {
room = new Room[numOfRooms];
for(int i=0; i<numOfRooms; i++) {
room[i] = new Room();
}
}
public String toString() {
return "This Building has "+room.length+"rooms";
}
public Room getRoom(int roomNum){
return room[roomNum];
}
public void reserveRoom (boolean reserve, int count)
{
//class constant
//class variables
/*****************************************************/
room [count].updateReserve(reserve);
} // end
}
class Room {
boolean roomAvail;
public boolean getRoomAvail() {
return roomAvail;
}
public void updateReserve(boolean newReserve)
{
//class constant
//class variables
/*****************************************************/
if (newReserve == false)
{
roomAvail = true;
}
else
{
roomAvail = false;
}
} // END 
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论