import java.util.Scanner;

public class lab3
{
	public static void main(String[]args)
	{
		int floor;
		int rooms;
		int occupied;
		int totalRooms;
		int totalOccupied;
		int totalVacant;
		double occupancyRate;

		Scanner keyboard=new Scanner(System.in);

		System.out.println("Please enter the number of floors in the hotel: ");
		floor=keyboard.nextInt();
		while(floor<1)
		{
			System.out.println("The number of floors cannot less than 1, please re-enter: ");
			floor=keyboard.nextInt();
		}
		totalRooms=0;
		totalOccupied=0;

		for(int i=1;i<=floor;i++)
		{
			System.out.println("Please enter the number of rooms in floor "+i+": ");
			rooms=keyboard.nextInt();
			while(rooms<10)
			{
				System.out.println("Number of rooms cannot less than 10, please re-enter: ");
				rooms=keyboard.nextInt();
			}
			totalRooms+=rooms;

			System.out.println("Please enter the number of occupied rooms: ");
			occupied=keyboard.nextInt();
			totalOccupied+=occupied;

			keyboard.nextLine();


		}
			System.out.println("The total number of rooms in the hotel is "+totalRooms);
			System.out.println("The total rooms occupied in the hotel is "+totalOccupied);

			totalVacant=totalRooms-totalOccupied;
			System.out.println("The total vacant rooms in the hotel is: "+totalVacant);

			occupancyRate=totalOccupied / totalRooms;
			System.out.println("The occupancy rate for the hotel is "+occupancyRate);

}
}

why the oocupancy rate is always zero after i compile?

why the oocupancy rate is always zero after i compile?

After you "compile" your program none of your variables have any values in them and none of your methods are executed.

It is only when you run the program all the fancy work of variables being initialised, methods being called, objects being created etc happens.
As far as your program goes, tell us what the following line prints:-

System.out.println("The total rooms occupied in the hotel is "+totalOccupied);

If that prints "0" then its a case for simple arithmetic that "occupancyRate" is zero because of :-

occupancyRate = totalOccupied / totalRooms;

However the most probable fault is the fact that you are dividing an "int" by another "int" so the result would be an "int" with the decimal part truncated which you are assigning to a double.
You will need to either declare your variable "totalOccupied" either as double or cast it as a double while performing the division operation, if you want the decimal part of the result to be preserved.

thank you.
i did it already.
the occupancy rate is no more zero.:)

This article has been dead for over six months. Start a new discussion instead.