I am trying to write a program that prompts the user to enter the total number of floors in the hotel, the number of rooms on each floor and the number of occupied rooms. At the end, it should display the total number of rooms, the total number of occupied rooms and the percentage of occupied premises. I have problems displaying the percentage of occupied rooms. I use all int numbers.
This is the equation I set:
roomsOccPercentage = (totalRoomsOccupied * 100) / totalRooms ;
When I submit the program to my Java Java teacher, it displays:
65 % of the Rooms are occupied.
But one of my professors provided a 66% answer instead, so the program will not accept my file.
Does anyone know what I'm doing wrong? Is this a DecimalFormat error?
Edit: Here is the whole code
import java.util.Scanner;
import java.text.DecimalFormat;
public class hw7_1 {
public static void main(String[]args) {
Scanner keyboard = new Scanner(System.in);
DecimalFormat formatter = new DecimalFormat("#0");
int totalFloors;
int totalRooms = 0;
int numFloors;
int numRooms;
int roomsOccupied;
int totalRoomsOccupied = 0;
int roomsOccPercentage = 0;
do {
System.out.println("Please enter the number of floors in the hotel: ");
numFloors = keyboard.nextInt();
if (numFloors < 1) {
System.out.println("You have entered an invalid number of floors. ");
}
}
while (numFloors < 1);
for ( int Floors = 1; Floors <= numFloors; Floors++) {
if (Floors == 13 ) {
continue;
}
do {
System.out.println("Please enter the number of rooms on floor #: " + Floors );
numRooms = keyboard.nextInt();
if (numRooms < 10) {
System.out.println("You have entered an invalid number of rooms. ");
}
} while (numRooms < 10);
System.out.println("Please enter the number of occupied rooms on floor #: " + Floors);
roomsOccupied = keyboard.nextInt();
totalRooms = totalRooms + numRooms;
totalRoomsOccupied = totalRoomsOccupied + roomsOccupied;
roomsOccPercentage = (totalRoomsOccupied * 100) / totalRooms ;
}
System.out.println("\nThe hotel has a total of " + totalRooms + " rooms.");
System.out.println(totalRoomsOccupied + " of the rooms are occupied.");
System.out.println(formatter.format(roomsOccPercentage) + "% of the rooms are occupied.");
}
}