停车费计算器(Parking Charges)

一个多礼拜没写代码了,业精于勤荒于嬉,赶快写几行代码压压惊。

 

代码如下:

package example;
//JHTP Exercise 6.8: Parking Charges
//by pandenghuang@163.com
/**(Parking Charges) A parking garage charges a $2.00 minimum fee to park for up to three
hours. The garage charges an additional $0.50 per hour for each hour or part thereof in excess of three
hours. The maximum charge for any given 24-hour period is $10.00. Assume that no car parks for
longer than 24 hours at a time. Write an application that calculates and displays the parking charges
for each customer who parked in the garage yesterday. You should enter the hours parked for each
customer. The program should display the charge for the current customer and should calculate and
display the running total of yesterday’s receipts. It should use the method calculateCharges to determine
the charge for each customer.*/
import java.util.Scanner;

public class ParkingCharges 
{
	public static double CalculateCharges(double hours){
		double charges=0.0;
		if (hours<=3.0)
			charges=2.00;
		else if (hours>3)
			charges=Math.min(2.0+Math.ceil(hours-3)*0.5,10.00);
		return charges;
	}
public static void main(String[] args)
{
	double hours=0.0;
	double total=0.0;
	int count=0;
	Scanner input=new Scanner(System.in);
	
	
	do {
		System.out.print("请输入下一位顾客的停车时间(输入-1退出):");
		hours=input.nextDouble();
		if (hours>=0){
		total+=CalculateCharges(hours);
		count++;
		System.out.printf("该顾客停车费为:%.2f美元\n",CalculateCharges(hours));
		System.out.printf("共输入了%d位顾客的停车时间,合计停车费为:%.2f美元\n",count,total);
		}
		else if (hours==-1){
			System.out.println("已终止输入。");
			System.out.printf("共输入了%d位顾客的停车时间,合计停车费为:%.2f美元\n",count,total);
			}
		else{
			System.out.println("请输入有效时间!");
			}
		}
	while (hours!=-1);
} 
} 

 

运行结果:

1. Round 1

请输入下一位顾客的停车时间(输入-1退出):2
该顾客停车费为:2.00美元
共输入了1位顾客的停车时间,合计停车费为:2.00美元
请输入下一位顾客的停车时间(输入-1退出):3
该顾客停车费为:2.00美元
共输入了2位顾客的停车时间,合计停车费为:4.00美元
请输入下一位顾客的停车时间(输入-1退出):3.1
该顾客停车费为:2.50美元
共输入了3位顾客的停车时间,合计停车费为:6.50美元
请输入下一位顾客的停车时间(输入-1退出):4.0
该顾客停车费为:2.50美元
共输入了4位顾客的停车时间,合计停车费为:9.00美元
请输入下一位顾客的停车时间(输入-1退出):10
该顾客停车费为:5.50美元
共输入了5位顾客的停车时间,合计停车费为:14.50美元
请输入下一位顾客的停车时间(输入-1退出):30
该顾客停车费为:10.00美元
共输入了6位顾客的停车时间,合计停车费为:24.50美元
请输入下一位顾客的停车时间(输入-1退出):9
该顾客停车费为:5.00美元
共输入了7位顾客的停车时间,合计停车费为:29.50美元
请输入下一位顾客的停车时间(输入-1退出):-2
请输入有效时间!
请输入下一位顾客的停车时间(输入-1退出):3
该顾客停车费为:2.00美元
共输入了8位顾客的停车时间,合计停车费为:31.50美元
请输入下一位顾客的停车时间(输入-1退出):-3
请输入有效时间!
请输入下一位顾客的停车时间(输入-1退出):-1
已终止输入。
共输入了8位顾客的停车时间,合计停车费为:31.50美元

 

2. Round 2

请输入下一位顾客的停车时间(输入-1退出):-1
已终止输入。
共输入了0位顾客的停车时间,合计停车费为:0.00美元



  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值