本文介绍了Java:整数除法向上取整的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个程序,提示用户输入酒店的楼层总数、每层楼的房间数以及已入住的房间数.最后,它应该显示房间总数、已占用房间总数和已占用房间百分比.我在显示已占用房间的百分比时遇到问题.我正在使用所有 int 数字.

I'm trying to write a program that prompts the user to enter the total amount of floors in a hotel, the number of rooms in each floor, and the number of occupied rooms. In the end it should display the total # of rooms, the total # of rooms occupied, and the percentage of the rooms occupied. I am having problems with displaying the percentage of rooms occupied. I'm using all int numbers.

这是我提出的等式:

roomsOccPercentage = (totalRoomsOccupied * 100) / totalRooms ;

当我将程序提交到教授的 Java 运行器时,它显示:

When I submit the program into my professor's Java runner it displays:

65 % of the Rooms are occupied.

但是我的教授提供的答案输出了 66%,因此程序不会接受我的文件.

But the one my professor provided outputted the answer 66 % instead so the program won't accept my file.

有谁知道我做错了什么?是 DecimalFormat 错误吗?

Does anyone know what I'm doing wrong? Is it a DecimalFormat error?

这是整个代码

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.");
    }
}

推荐答案

您需要先将 roomsOccPercentage 设为双倍.

You need to make your roomsOccPercentage a double first.

double roomsOccPercentage = 0.0;

然后转换任一操作数以避免整数除法.

and then cast either of the operands so avoid an integer division.

roomsOccPercentage = (totalRoomsOccupied * 100.0) / totalRooms;

您可以使用像 (double)totalRoomsOccupied 这样的显式转换,也可以将 100 设为 100.0,这是一个浮点数,因此也进行除法,浮点除法而不是整数除法.

You can either use an explicit cast like (double)totalRoomsOccupied or just make 100 as 100.0 which is a floating point number, thus making the division too, a floating point one and not an integer division.

这篇关于Java:整数除法向上取整的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-18 08:22
查看更多