java 整数除法整数_Java:整数除法

我正在尝试编写一个程序,提示用户输入酒店的楼层总数,每层楼的房间数量以及占用房间的数量.最后,它应显示房间总数,占用房间总数以及占用房间的百分比.我在显示占用房间的百分比方面遇到了问题.我正在使用所有的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.");

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值