我正在尝试编写一个程序,提示用户输入酒店的楼层总数,每层楼的房间数量以及占用房间的数量。最后,它应显示房间总数,占用房间总数以及占用房间的百分比。我在显示占用房间的百分比方面遇到了问题。我正在使用所有的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.");
}
}
答案 0 :(得分:17)
请注意,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
答案 1 :(得分:6)
您需要先将roomsOccPercentage
设为双倍。
double roomsOccPercentage = 0.0;
然后转换任一个操作数,以避免整数除法。
roomsOccPercentage = (totalRoomsOccupied * 100.0) / totalRooms;
你可以使用像(double)totalRoomsOccupied
这样的显式强制转换,也可以将100
设为100.0
这是一个浮点数,从而使得除法也成为浮点数,而不是整数除法。
答案 2 :(得分:1)
a / b + a % b / (b/2)
??
1除以3应为0
{0} <0}的1 / 3 + 1 % 3 / (3/2) = 0 + 1 / 1 = 1
我推荐这个:
(a + a % b) / b
答案 3 :(得分:0)
在整数除法中没有任何称为舍入的因为它只在除数结果中取整数部分。使用double
值进行计算并使用Math.round()
。
答案 4 :(得分: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;
如果它不是整数,则添加一个并将其强制转换。如果是整数,则将其转换为整数。
答案 5 :(得分:0)
给定a/b
,如果你希望它舍入到最接近的整数,如果余数是b的一半或大于一半,则需要加1,因此:
a / b + a % b / (b/2)
会做的伎俩。
答案 6 :(得分:0)
如果您希望进入双打以进行整数除法,那么您可以这样做。如果你不愿意,你不需要,这只是一个非常棘手的问题:
private static int divideRoundUp(int denominator, int divisor) {
return (denominator + divisor - 1) / divisor;
}
我们试一试:
System.out.println(divideRoundUp(196, 3));
System.out.println(divideRoundUp(197, 3));
System.out.println(divideRoundUp(118, 2));
System.out.println(divideRoundUp(119, 2));
打印:
66
66
59
60
如果没有余数,如在118/2中,则得到正常结果。在其他情况下,结果会四舍五入。但是,如何处理负分母和/或除数仍然是读者的练习。
在学校,我们中的许多人都知道65.5或更高应该四舍五入到66,其中65.0和65.5之间的数字应该舍入到65.如果你想要这个,方法有点不同:
private static int divideRoundHalfUp(int denominator, int divisor) {
return (denominator + divisor / 2) / divisor;
}
演示:
System.out.println(divideRoundHalfUp(196, 3));
System.out.println(divideRoundHalfUp(197, 3));
System.out.println(divideRoundHalfUp(118, 2));
System.out.println(divideRoundHalfUp(119, 2));
输出:
65
66
59
60