某市出租车计费标准如下图所示,
请根据此标准完成一个出租车计费模拟功能,能够计算总费用和列出产生费用
项目详细情况说明,帮助出租车师傅和乘客了解计费标准
题目描述:
结合上述表格,可以得出:总车费=里程费用+低速行驶费(或者等候费)
+预约叫车服务费+空驶费+夜间收费+燃油附加费。需要收集的数据有:里程数、 低速行驶时长(早晚高峰期行驶时长和其他时间段行驶时长)、是否预约叫车 (按四小时为标准)、开始乘坐出租车时间、出租车到达终点站时间,结合这
些数据和表中提供的标准就可以使用程序进行计算总车费了
代码:
import java.util.Scanner;
// 抛出异常 测试
public class Taxi {
public static void main(String[] args) {
Scanner input =new Scanner(System.in);
//1. 声明好程序所需的变量,用于存储数据,请注意数据类型。
int totalCost = 0;
//里程费用
double mileageCost = 0;
//低速行驶费(或者等候费)
double lowCost =0;
//预约叫车服务费
int callCarCosrt =0;
//空驶费
double emptyCost =0;
//夜间收费
double nightCost =0;
//燃油附加费
int oilCost = 0;
//提示用户输入总里程数(符合交付 仍需抛出异常)
System.out.println("请输入总里程数:");
double km =0;
if(input.hasNextDouble())
{
km = input.nextDouble();
}
else
{
System.out.println("你的输入有误!!");
return;
}
//总乘车时间 = 下车时间 - 上车时间
int onHour = 0;
int onMinutes = 0;
int onSeconds = 0;
System.out.println("输入上车时间:");
System.out.println("时:");
if (input.hasNextInt()) {
onHour = input.nextInt();
if (onHour < 0 | onHour > 23) {
System.out.println("你的输入有误!!");
return;
}
} else {
System.out.println("你的输入有误!!");
return;
}
System.out.println("分:");
if (input.hasNextInt()) {
onMinutes = input.nextInt();
if (onMinutes < 0 | onMinutes > 59) {
System.out.println("你的输入有误!");
return;
}
} else {
System.out.println("你的输入有误!");
return;
}
System.out.println("秒:");
if (input.hasNextInt()) {
onSeconds = input.nextInt();
if (onSeconds < 0 | onSeconds > 59) {
System.out.println("你的输入有误!");
return;
}
} else {
System.out.println("你的输入有误!");
return;
}
int offHour = 0;
int offMinutes = 0;
int offSeconds = 0;
System.out.println("输入下车时间:");
System.out.println("时:");
if (input.hasNextInt()) {
offHour = input.nextInt();
if (offHour < 0 | offHour > 23) {
System.out.println("你的输入有误!!");
return;
}
} else {
System.out.println("你的输入有误!!");
return;
}
System.out.println("分:");
if (input.hasNextInt()) {
offMinutes = input.nextInt();
if (offMinutes < 0 | offMinutes > 59) {
System.out.println("你的输入有误!");
return;
}
} else {
System.out.println("你的输入有误!");
return;
}
System.out.println("秒:");
if (input.hasNextInt()) {
offSeconds = input.nextInt();
if (offSeconds < 0 | offSeconds > 59) {
System.out.println("你的输入有误!");
return;
}
} else {
System.out.println("你的输入有误!");
return;
}
//是否预约叫车
Boolean appointment = null;
//是否四小时
Boolean exceed = null;
System.out.println("是否预约叫车:(true/false)");
if (input.hasNextBoolean()) {
appointment = input.nextBoolean();
if (appointment) {
//是不是提前四小时预约
System.out.println("是不是提前四小时:");
if (input.hasNextBoolean()) {
exceed = input.nextBoolean();
} else {
System.out.println("输入错误");
return;
}
} else {
System.out.println("输入有误");
return;
}
}
//是否低速行驶
Boolean lowSpeed = null;
//高峰
int peakDuration = 0;
//非高峰
int offpeakDuration = 0;
System.out.println("是否低速行驶:(true false )");
if (input.hasNextBoolean()) {
lowSpeed = input.nextBoolean();
if (lowSpeed) {
System.out.println("早晚高峰形式的时长低速:分钟");
if (input.hasNextInt()) {
peakDuration = input.nextInt();
}
System.out.println("早晚非高峰形式的时长低速:分钟");
if (input.hasNextInt()) {
offpeakDuration = input.nextInt();
}
} else {
System.out.println("输入有误");
return;
}
}
//预约费
if (appointment) {
if (exceed) {
callCarCosrt = 6;
} else {
callCarCosrt = 5;
}
}
//往返载客
Boolean linger = null;
if (km >= 15) {
System.out.println("是否往返载客");
if (input.hasNextBoolean()) {
linger = input.nextBoolean();
} else {
System.out.println("输入有误");
return;
}
}
//总车费=里程费用+低速行驶费(或者等候费)+预约叫车服务费+空驶费+夜间收费+燃油附加费。
/* int totalCost = 0;
//里程费用
double mileageCost = 0;
//低速行驶费(或者等候费)
double lowCost =0;
//预约叫车服务费
int callCarCosrt =0;
//空驶费
double emptyCost =0;
//夜间收费
double nightCost =0;
//燃油附加费
int oilCost = 0;*/
//里程费用
if (km < 3) {
mileageCost = 13;
} else if (km >= 15) {
mileageCost = km * 2.3;
if (linger) {
} else {
emptyCost = mileageCost * 1.5;
}
} else {
mileageCost = km * 2.3;
}
//低速行驶
if (lowSpeed) {
lowCost=(peakDuration/5)*2*2.3+(offpeakDuration/5)*1*2.3;
}
//夜间费
if (onHour==23 | onHour <5 |offHour ==23 | offHour <5){
nightCost=1.2*mileageCost;
}
totalCost =(int)(mileageCost+lowCost+callCarCosrt+emptyCost+nightCost+oilCost);
System.out.println("总费用:");
System.out.println(totalCost );
}
}
如果觉得有用就留个赞吧!客官.