Monday

March 30, 2015

March 30, 2015

Posted by **AB** on Tuesday, May 22, 2012 at 9:00pm.

**I believe I have the program done correctly, I just can't figure out how to get it not to accept a number less than 10 for the number of rooms on a floor.** Here is the code:

package hotel;

import java.util.Scanner;

public class Hotel {

public static void main(String[]args) {

int floors, occupiedRooms = 0, totalRooms = 0;

Scanner input = new Scanner(System.in);

System.out.print("How many floors are present: ");

floors = input.nextInt();

for(int i = 1; i <= floors; i++) {

System.out.print("Level " + i + ": How many rooms are there: ");

int totalRoomsOnLevel = input.nextInt();

System.out.print("Level " + i + ":How many are occupied: ");

int occupiedRoomsOnLevel = input.nextInt();

System.out.println();

totalRooms += totalRoomsOnLevel;

occupiedRooms += occupiedRoomsOnLevel;

}

System.out.println();

System.out.println("Total hotel rooms: " + totalRooms);

int vacantRooms = totalRooms - occupiedRooms;

System.out.println("Total vacant rooms: " + vacantRooms);

double occupancyRate = (double)occupiedRooms / totalRooms;

System.out.println("Occupancy rate: " + occupancyRate);

}

}

- programming -
**MathMate**, Tuesday, May 22, 2012 at 9:40pmYour code looks carefully thought out.

For making sure there are 10 storeys or more, you can modify the input segment:

System.out.print("How many floors are present: ");

floors = input.nextInt();

while(floors<10){

// print Number of floors must be at least 10

floors = input.nextInt();

}

But this would continue indefinitely until user enters a number greater than 10.

A better way is to use a for-loop to loop a maximum (say 5) of times after which a message will appear and the program terminates.

- programming -
**AB**, Tuesday, May 22, 2012 at 9:59pmI'm sorry. I think I was confusing. There can't be less than 1 for the number of floors, but has to be at least 10 for the number of rooms on a floor.

- programming -
**MathMate**, Tuesday, May 22, 2012 at 10:05pmIt would be along the same lines, just change floors to rooms, and for floors, change 10 to 1 for the number of floors.

- programming -
**AB**, Tuesday, May 22, 2012 at 10:59pmI got it. Thanks so much. You're awesome.

- programming :) -
**MathMate**, Wednesday, May 23, 2012 at 2:51pmYou did most of the work, so you deserve the credit too!

**Answer this Question**

**Related Questions**

math/algebra - I need help in solving this word problem using linear system ...

Production & Operations Mgmt in Business - If someone could help me, I am truly ...

Algebra - I have two questions... 1.Lenear system...If you are painting the ...

Math - A local motel has 100 rooms. The occupancy rate for the winter months is ...

math - Help pleaseeee The Hotel Regal has 500 rooms. Currently the hotel is ...

Managerial Economics - A CA resort offers year-round activities and the resort'...

Managerial Economics - A CA resort offers year-round activities and the resort's...

Pre-Calculus - The Hotel Bellville has 400 rooms. Currently the hotel is filled...

precalculus - The Hotel Thomasina has 500 rooms. Currently the hotel is filled...

Math - The Hotel Regal has 600 rooms. Currently the hotel is filled . The daily ...