Input Validation: Do not accept a number less than one for the number of floors. Do not accept a number less than 10 for the number of rooms on a floor.
I'm getting a wrong occupancy rate. the 'rooms' and 'occupied' keeps resetting after each loop.
i'm very new to this so please help!
Thanks
/*
this program determines the occupancy rate of a hotel
*/
import java.util.Scanner;
public class HotelOccupancy
{
public static void main ( String [] args)
{
Scanner keyboard = new Scanner(System.in);
int floors = 0; //number of floors
int rooms =0; //number of rooms
int occupied = 0; //number of occupied rooms
final int MIN_FLOOR = 1; //minimum number of floors
final int MIN_ROOMS = 10; // minimum number of rooms
double occupancy = 0; //occupancy rate
int total; //total number of rooms
int vacant = 0; //number of vacant rooms
//number of floors
System.out.println("Enter the number of floors");
//to validate the number of floors
do
{
floors = keyboard.nextInt();
System.out.println("Floors have to be more than 1");
}
while (floors <= MIN_FLOOR);
//to insert a blank line
System.out.println();
for (int i = 0; i < floors; ++i)
{
// number of rooms on each floor
System.out.println("Please enter the number of rooms on floor " + i);
rooms = keyboard.nextInt();
//room occupied per floor
System.out.println("How many rooms on floor " + i + " are occupied");
occupied = keyboard.nextInt();
}
//calculations
total = (floors * rooms);
vacant = (total - occupied);
occupancy = ((double)occupied / total);
}
}

New Topic/Question
This topic is locked



MultiQuote


|