首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >计算停车费

计算停车费
EN

Code Review用户
提问于 2023-06-03 14:20:06
回答 1查看 14关注 0票数 0

下面是描述:

请编写一个管理停车费的Java程序。每小时3美元。

  1. 发票必须包含: ID、金额和小计。
  2. 当天的最高收费为30美元,并提供一张5美元的优惠券.
  3. 停车位总数必须超过0。如果输入的汽车小于0时,将显示错误消息。
  4. 总停车时间必须>0小时和<24小时。如果输入的时数不正确,则将显示错误消息。

这是输出的一个例子:

代码语言:javascript
复制
Please enter today's total parked cars: -3

The number you entered is less than 0, please re-enter: 0

The number you entered is less than 0, please re-enter: 8

Please enter your 1st car's parking hours: -1

The parking hours entered is incorrect, please re-enter: 25

The parking hours entered is incorrect, please re-enter: 30

The parking hours entered is incorrect, please re-enter: 24

Please enter your 2nd car's parking hours: 2

Please enter your 3rd car's parking hours: 5

Please enter your 4th car's parking hours: 9

Please enter your 5th car's parking hours: 4

Please enter your 6th car's parking hours: 3

----today's parking invoice----

ID.   Amount      Subtotal

1 _______30 _______ 30   *coupon

2 _______6 _______  36      

3 _______ 12_______ 48

4 _______ 3 _______   51  

5 _______30  _______  81    *coupon         

Today's total:$81

这是我的代码:

代码语言:javascript
复制
import java.util.Scanner;

public class Tryagainparking {
    public static void main(String[] args) 
    {
        Scanner keyboard=new Scanner(System.in);
        System.out.print("Total parked cars:");
        int NUMBER_OF_CARS = keyboard.nextInt();
        
        while (NUMBER_OF_CARS<=0)
        {
            System.out.print("Cars has to >0,please re-entere:");
            int NUMBER_OF_CARS2 = keyboard.nextInt();
            if (NUMBER_OF_CARS2>0)
                break;
        }
        
        double hoursParked=0;
        double EveryAmount=0;
        int totalincome=0;
        int counter=1;
        
         
        for (int count=1;count<=NUMBER_OF_CARS;count++)
        {
             System.out.printf("this is the %d data:",count);
             hoursParked=keyboard.nextInt();
                while(hoursParked<=0)
                { 
                    System.out.printf("parking hour is incorrect, please re-eneter");
                    int hoursParked2=keyboard.nextInt();
                }
                while(hoursParked>=24)
                { 
                    System.out.printf("parking hour is incorrect, please re-eneter");
                    int hoursParked2=keyboard.nextInt();
                }
            EveryAmount=(hoursParked)*30;
                if (EveryAmount>=300)
                    EveryAmount=300;
            totalincome+=EveryAmount;
        
            
        }
        System.out.print("----invoice------\n");
        System.out.print("ID     Amount     Subtotal");
        
        System.out.printf("%s",counter);
    
        
        
    }
}
EN

回答 1

Code Review用户

发布于 2023-06-03 14:42:52

这个测试看上去是错误的:

while(hoursParked<=0) { System.out.printf("parking hour is incorrect, please re-eneter"); int hoursParked2=keyboard.nextInt(); } while(hoursParked>=24) { System.out.printf("parking hour is incorrect, please re-eneter"); int hoursParked2=keyboard.nextInt(); }

在第二个循环中,我们可以输入一个负值,这将被接受。

我们希望替换我们之前读到的值,而不是扔掉它(当我们读取汽车数量时,我们似乎遇到了同样的问题)。

我们需要一个循环来重复,直到满足所有条件:

代码语言:javascript
复制
                while (hoursParked<=0 || hoursParked>=24)
                { 
                    System.out.printf("parking hour is incorrect, please re-enter");
                    hoursParked = keyboard.nextInt();
                }
票数 2
EN
页面原文内容由Code Review提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://codereview.stackexchange.com/questions/285320

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档