如何为酒店入住率计划确定小数格式百分比?

时间:2019-05-19 23:37:47

标签: java percentage decimalformat

在for循环之后,我得到了占用率,并为从占用率中得到的百分比创建了一个十进制格式。

问题是我在程序中输入完某些内容后有时会在最终显示中显示0%。

例如,当我输入: 3 [输入] 20 [输入] 17 [输入] 20 [输入] 12 [输入] 15 [输入] 7 [输入]

在程序结束时,它将为最终显示的占用率显示0%的百分比。

import java.util.Scanner;
import java.text.DecimalFormat;
public class HotelOccupancy 
{
    public static void main(String[] args) 
    {
        //#declaring variables
        int floors;
        int rooms = 0; 
        int roomsOccupied = 0;
        int totalRooms = 0;
        int totalRoomsOccupied = 0;
        int totalVacancy = 0;
        int occupancyRate = 0;

        //#scanner keybaord
        Scanner keyboard = new Scanner(System.in);

        //#title
        System.out.println("Hotel Occupancy Calculator");
        System.out.println("==========================");

        //#asking user for how many floors in hotel
        System.out.print("How many floors are in the hotel?===> ");
        floors = keyboard.nextInt();

        //#Do not accept a value less than 1 for the number of floors.
        while(floors < 1)
        {
            System.out.print("Invalid Input." + 
                "Please enter a number of floors greater than 0==> ");
            floors = keyboard.nextInt();
        }

        for(int num = 0; num < floors; num++)
        {
            System.out.print("\nHow many rooms are on floor " 
                + (num + 1) + "?===> ");
            rooms = keyboard.nextInt();

            //#Do not accept a number less than 10 for the number of rooms on a floor.
            while(rooms < 10)
            {
                System.out.print("Invalid input. " + 
                    "\nPlease enter a number of rooms " + 
                    "greater than 9 for floor " + 
                    (num + 1) + "==> ");
                rooms = keyboard.nextInt();
            }

            //#asking user for rooms occupied
            System.out.print("\nHow many rooms are occupied on floor " + 
                (num + 1) + "?==> ");
            roomsOccupied = keyboard.nextInt();

            //#total rooms
            totalRooms += rooms;

            //#Occupied Rooms
            totalRoomsOccupied += roomsOccupied;
        }

        //#vacant rooms
        totalVacancy = totalRooms - totalRoomsOccupied; 

        //#occupancy rate
        occupancyRate = (totalRoomsOccupied/totalRooms);

        //#decimal fomatting for percent
        DecimalFormat formatter1 = new DecimalFormat("#.##%");

        //#final display of calculations
        System.out.println("\nOccupancy Rate Calculations");
        System.out.println("---------------------------");
        System.out.println("Total Rooms = " + totalRooms + "\nOccupied Rooms = " + totalRoomsOccupied + 
            "\nVacant Rooms = " + totalVacancy + "\nOccupancy Rate = " 
            + formatter1.format(occupancyRate) + "%");
    }
}

因此,根据示例,当用户输入以下内容时: 3 [输入] 20 [输入] 17 [输入] 20 [输入] 12 [输入] 15 [输入] 7 [输入]

显示屏应显示:

> Hotel Occupancy Calculator
> ========================== 
> How many floors are in the hotel?===> 3
> 
> How many rooms are on floor 1?===> 20
> 
> How many rooms are occupied on floor 1?==> 17
> 
> How many rooms are on floor 2?===> 20
> 
> How many rooms are occupied on floor 2?==> 12
> 
> How many rooms are on floor 3?===> 15
> 
> How many rooms are occupied on floor 3?==> 7
> 
> Occupancy Rate Calculations
> --------------------------- 
> Total Rooms = 55 
> Occupied Rooms = 36 
> Vacant Rooms = 19 
> Occupancy Rate = 65.45%

0 个答案:

没有答案