Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Sep 2012
    Posts
    32
    Thanks
    25
    Thanked 0 Times in 0 Posts

    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
    New to the CF scene
    Join Date
    Sep 2012
    Posts
    4
    Thanks
    0
    Thanked 1 Time in 1 Post
    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.

  • Users who have thanked Trollypolly for this post:

    CodyJava (09-30-2012)

  • #3
    New to the CF scene
    Join Date
    Sep 2012
    Posts
    4
    Thanks
    0
    Thanked 1 Time in 1 Post
    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
    •