JAVA计算出租车运费
时间: 2024-04-28 07:04:07 浏览: 73
以下是一个简单的JAVA程序,用于计算出租车运费:
```
import java.util.Scanner;
public class TaxiFareCalculator {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("请输入里程数(公里):");
double distance = input.nextDouble();
System.out.print("请输入等待时间(分钟):");
int waitingTime = input.nextInt();
double fare = calculateFare(distance, waitingTime);
System.out.println("本次出租车费用为:" + fare + "元");
}
public static double calculateFare(double distance, int waitingTime) {
double baseFare = 13.0; // 起步价
double distanceFare = 2.3; // 每公里加价
double waitingFare = 0.25; // 每分钟等待加价
double totalFare = baseFare + distanceFare * distance + waitingFare * waitingTime;
return totalFare;
}
}
```
程序中,用户需要输入里程数和等待时间,然后调用calculateFare函数计算出租车费用,最后输出结果。calculateFare函数根据起步价、每公里加价和每分钟等待加价来计算费用。
阅读全文