简体   繁体   中英

Troubles with variable scope outside the for loop

So, there is a class Hotel , that contains 20 Rooms in form of a matrix 4x5 (4 floors, 5 rooms on every floor). The class Room has the properties:

floorNumber(int),  
roomNumber(int),  
numberOfBeds(int),  
occupation(boolean)
etc.

For occupation , true means busy, and false means free.

One of methods I have to implement in Hotel is the one that reserves a room

reserve(int floorNumber, int roomNumber) 

This method should check if occupation is true or false.
If it is true, then reservation fails, and if it is false, I should set occupation to true, with method

setOccupation(boolean t).  

Also, method reserve return boolean (true or false), depending on whether reservation succeeded or not. In that method, you guess, is problem with scope of one variable. So there it is:

public boolean reserve(int floorNumber, int roomNumber){
    boolean flag = false;
    for ( int i = 0; i < 5; i++){
      if(rooms[floorNumber][i].getRoomNumber() == roomNumber){//every element in matrix rooms has this property: rooms[floorNumber][some_number_from_1_to_5]
        if (rooms[floorNumber][i].getOccupancy() == false){
          rooms[floorNumber][i].setOccupancy(true);
          flag = true;
        }
        else
          flag = false;
      }
    }
    return flag;
  }

The problem is, when I set (in first line) flag to true, function returns true, and when I set flag to false, function returns false.
The reason I have to assign some value to flag in first line is because compiler shows:

Error: variable flag might not have been initialized

So, the problem is that it seems like method never executes code with for loop.
I know that variables defined in loop don't exist outside loop, but those defined outside should change their values in loop. Like in this question here: Java - Access variable inside and outside of for-loop

There is a simpler way to accomplish what you want to do. You don't need a boolean flag at all; you can just return true immediately on success or return false if the entire loop executed without finding a room.

public boolean reserve(int floorNumber, int roomNumber){
  for (int i = 0; i < 5; i++) {
    //every element in matrix rooms has this property:
    //rooms[floorNumber][some_number_from_1_to_5]
    if (rooms[floorNumber][i].getRoomNumber() == roomNumber){
      if (rooms[floorNumber][i].getOccupancy() == false){
        rooms[floorNumber][i].setOccupancy(true);
        return true;
      }
    }
  }
  return false;
}

But if you insist on applying your original approach that uses a flag, then: First give it a value of false (in case no room succeeded). When we find an unoccupied room (successful), set it to true. If we find an occupied room, don't touch the flag value .

public boolean reserve(int floorNumber, int roomNumber){
  boolean flag = false;
  for (int i = 0; i < 5; i++) {
    //every element in matrix rooms has this property:
    //rooms[floorNumber][some_number_from_1_to_5]
    if (rooms[floorNumber][i].getRoomNumber() == roomNumber){
      if (rooms[floorNumber][i].getOccupancy() == false){
        rooms[floorNumber][i].setOccupancy(true);
        flag = true;
      } // else DO NOTHING
    }
  }
  return flag;
}

I found what the problem was.
It was actually index floorNumber in matrix rooms[floorNumber][] that goes from 0 to 3 (there are 4 floors), of course.
But in real life, floor numbers go from 1, and I passed argument to

reserve(int floorNumber,int roomNumber)

without considering that.
So, I just decremented floorNumber by 1 in body of method, and it works now.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM