Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 09-30-2012, 03:18 PM   PM User | #1
CodyJava
New Coder

 
Join Date: Sep 2012
Posts: 25
Thanks: 21
Thanked 0 Times in 0 Posts
CodyJava is an unknown quantity at this point
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);

}
}
CodyJava is offline   Reply With Quote
Old 09-30-2012, 04:25 PM   PM User | #2
Trollypolly
New to the CF scene

 
Join Date: Sep 2012
Posts: 4
Thanks: 0
Thanked 1 Time in 1 Post
Trollypolly is an unknown quantity at this point
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.

Last edited by Trollypolly; 09-30-2012 at 04:31 PM..
Trollypolly is offline   Reply With Quote
Users who have thanked Trollypolly for this post:
CodyJava (09-30-2012)
Old 09-30-2012, 04:29 PM   PM User | #3
Trollypolly
New to the CF scene

 
Join Date: Sep 2012
Posts: 4
Thanks: 0
Thanked 1 Time in 1 Post
Trollypolly is an unknown quantity at this point
Sorry for so many edits, I had a few mistakes when I was typing it up.
Trollypolly is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 05:13 AM.


Advertisement
Log in to turn off these ads.