I'm having trouble with the mathematical parts of my code and I can't seem to spot the problem(s). I've messed around but it becomes worse. Here's my code:
package chapter5; // Imports Scanner class of utility package. import java.util.Scanner; // HotelOccupancy class. public class HotelOccupancy { // Main function. public static void main(String[] args) { // Variable declarations. int floors, rooms, occupiedRooms, vacantRooms; // Integer variables for floors, rooms, occupiedRooms & vacantRooms. int totalRooms = 0; // Set the totalRooms to zero. int totalOccupiedRooms = 0; // Set the totalOccupiedRooms to zero. int totalVacantRooms = 0; // Set the totalVacantRooms to zero. double occupancyRate; // Double variable for occupancyRate. // Create scanner object for input. Scanner keyboard = new Scanner(System.in); // Ask the user for the number of floors. System.out.print("How Many Floors in the Hotel?: "); floors = keyboard.nextInt(); // Check conditions. while (floors < 1) { System.out.print("Invalid. Enter 1 or more: "); floors = keyboard.nextInt(); } if (floors >= 1) { for (int i = 1; i <= floors; i++) { // Ask the user for the number of rooms on the floor. System.out.print("Enter the Number of Rooms On the Floor: " + i + ": "); rooms = keyboard.nextInt(); while (rooms < 10) { System.out.print("Invalid. Enter 10 or more: "); rooms = keyboard.nextInt(); totalRooms += rooms; } if (rooms >= floors) { // Ask the user for the number of occupied rooms. System.out.print("Enter the Number of Rooms Occupied: "); occupiedRooms = keyboard.nextInt(); vacantRooms = (rooms - occupiedRooms); totalRooms += rooms; // Sum of the totalRooms. totalOccupiedRooms += occupiedRooms; // Sum of the occupiedRooms. totalVacantRooms += vacantRooms; // Sum of the vacantRooms. } else // Else display invalid error message. System.out.println("Invalid Number for Rooms."); } // Display the total number of totalRooms. System.out.println("\n Total Rooms in the Hotel Are: " + totalRooms); // Display the number of totalOccupiedRooms. System.out.println("\n Total Occupied Rooms in the Hotel Are: " + totalOccupiedRooms); // Display the number of totalVacantRooms. System.out.println("\n Total Vacant Rooms in the Hotel Are: " + totalVacantRooms); // Calculate the occupancyRate using the modulus. occupancyRate = (totalOccupiedRooms % totalRooms); // Display the occupancyRate. System.out.println("\n The Hotel Occupancy Rate is: " + occupancyRate); } else // Else display invalid error message. System.out.print("Invalid Number for the Floor."); // Close the keyboard object. keyboard.close(); } }
And this is the output my professor is expecting:
How many floors does the hotel have? 0 Invalid. Enter 1 or more:2 How many rooms does floor 1 have? 3 Invalid. Enter 10 or more:10 How many occupied rooms does floor 1 have? 3 How many rooms does floor 2 have? 10 How many occupied rooms does floor 2 have? 7 Number of rooms: 20 Occupied rooms: 10 Vacant rooms: 10 Occupancy rate: 0.5
Thanks for any help/tips.