Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

# Thread: Need Help Question About Variables

1. ## Need Help Question About Variables

Hi my assignment was to ask the user how many floors in a hotel building there were. Then ask the user for the number of rooms on each floor then the number occupied rooms. I put all of this in a do loop and my question is how can i set a variable to update its value each time through the loop.

Here is my code:

import java.util.Scanner;
public class HotelOccupancy {
public static void main(String [] args){
int floor;
int i;
int room;
int occupied;
int rate;
int vacant;

Scanner scan = new Scanner(System.in);
System.out.print("How many floors?: ");
floor = scan.nextInt();
if( floor < 1){
System.out.println("Error, enter a number greater than 1");
System.exit(0);
}
for(i=1; i<=floor; i++){
System.out.print("How many rooms are there?: ");
room = scan.nextInt();
if( room < 10){
System.out.println("Error, enter a number greater than 10");
System.exit(0);
}
System.out.print("How many rooms are occupied?: ");
occupied = scan.nextInt();

}

rate = (room / occupied); //determines occupancy rate
vacant = (room - occupied); //determine vacant rooms
System.out.println("Number of rooms:"+room);
System.out.println("Number of Occupied rooms:"+occupied);
System.out.println("Number of vacant rooms:"+vacant);
System.out.println("Occupany rate:"+rate);

}
}

2. Code:
```
int totalRooms = 0;  //Added here.
int totalOccupied = 0; //Added here
...
...
for(i=1; i<=floor; i++){

System.out.print("How many rooms are there?: ");
room = scan.nextInt();
totalRooms += room; //Added here.

if( room < 10){
System.out.println("Error, enter a number greater than 10");
System.exit(0);
}

System.out.print("How many rooms are occupied?: ");
occupied = scan.nextInt();
totalOccupied += occupied;  //Added here

}
rate = (totalRooms / totalOccupied); //determines occupancy rate  //Added here
vacant = (totalRooms - totalOccupied); //determine vacant rooms  //Added here
System.out.println("Number of rooms:" + totalRooms);  //Added here
System.out.println("Number of Occupied rooms:" + totalOccupied);  //Added here```
I think this is what you are asking for. I put comments on the rows that I added. If this was the answer you are looking for you were very close . If you have any other questions feel free to ask.

3. ## Users who have thanked Trollypolly for this post:

CodyJava (09-30-2012)

4. Sorry for so many edits, I had a few mistakes when I was typing it up.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•