문제

  //Name: Justin 

  import java.util.Scanner;

  public class hotelOccupancy
  {

  public static void main(String[] args)
  {

  Scanner keyboard = new Scanner(System.in);
  int floors, roomNo, occRooms, emptyRooms, occRoomsT = 0, roomNoT = 0;
  double occupancyRate;

  System.out.print("Enter the amount of floors in the hotel(must enter at least 1): ");
    floors = keyboard.nextInt();

  while(floors > 0)
     {
  System.out.print("Enter the amount of rooms on floor " + floors + ":");
    roomNo = keyboard.nextInt();

     if(roomNo < 10)
     {
  System.out.println("There is a minimum of 10 rooms per floor");
  System.out.print("Enter the amount of rooms on floor " + floors + ":");
    roomNo = keyboard.nextInt();
     }

  System.out.print("Enter the amount of rooms on floor " + floors + " that are occupied: ");
    occRooms = keyboard.nextInt();

     if(occRooms < 0 || occRooms > roomNo)
     {
  System.out.println("Please enter a valid number of occupied rooms");
  System.out.print("Enter the amount of rooms on floor " + floors + " that are occupied: ");
    occRooms = keyboard.nextInt();
     }

   occRoomsT = occRoomsT + occRooms;
   roomNoT = roomNoT + roomNo;  
   floors--;
   }

  occupancyRate = occRoomsT / roomNoT;
  emptyRooms = roomNoT - occRoomsT;

  System.out.println("The total amount of rooms is: " + roomNoT);
  System.out.println("The total amount of occupied rooms is: " + occRoomsT);
  System.out.println("The occupancy rate is: " + occupancyRate);
  System.out.println("The amount of vacant rooms is: " + emptyRooms);

  }
  }

Hello, this program is used to calculate the occupancy rate, total amount of rooms, total amount of occupied rooms, and total amount of vacant rooms, all in a hotel. The inputs are the amount of floors, the amount of rooms per floor, and the amount of occupied rooms on a given floor. After the input data is entered, the occupancy rate always comes out to 0.0 which is my problem. I have a feeling that the problem lies in my variable counters named: occRoomsT and roomNoT.

도움이 되었습니까?

해결책

Just make,

occupancyRate = ((double)occRoomsT) / roomNoT;

Total number of rooms will always be an integer and declaring them as double is not a good practice and this occupies more space compared to an integer.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top