Java: integer division is rounded

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;

        //prompting users to input # of floors, no inputs below 1 floor
        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 loops on how many rooms on each hotel floors
        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.");
    }
}
+4
6

roomsOccPercentage .

double roomsOccPercentage = 0.0;

, .

roomsOccPercentage = (totalRoomsOccupied * 100.0) / totalRooms;

(double)totalRoomsOccupied 100 100.0, , , , .

+5

, java , . , , .

double roomsOccPercentage;
roomsOccPercentage =Math.ceil((double)5/4); //will be 2
roomsOccPercentage =Math.ceil(5/4); //will be 1
roomsOccPercentage =Math.floor((double)5/4); //will be 1

, java intresting

+13

a / b + a % b / (b/2)?
1 3 0
1 / 3 + 1 % 3 / (3/2) = 0 + 1 / 1 = 1, 0

:

(a + a % b) / b
+2

a/b, , 1, b, :

a / b + a % b / (b/2)

.

+1

, . double Math.round().

0

:

double dnum; = 47.213; //make this your value to round up
int inum; //null for now, will be your rounded up integer
if(dnum > (int)num)
     inum = (int)(num + 1.0);
else
     inum = (int)num;

, . , .

0

All Articles