我正在尝试编写一个程序,提示用户输入酒店的楼层总数,每层楼的房间数量以及占用房间的数量.最后,它应显示房间总数,占用房间总数以及占用房间的百分比.我在显示占用房间的百分比方面遇到了问题.我正在使用所有的int数字.
这是我提出的等式:
roomsOccPercentage = (totalRoomsOccupied * 100) / totalRooms ;
当我将程序提交给我教授的Java运行程序时,它会显示:
65 % of the Rooms are occupied.
但是我教授提供的那个输出了66%的答案,所以程序不接受我的文件.
有谁知道我做错了什么?它是DecimalFormat错误吗?
编辑:这是整个代码
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.");
}
}