所以我非常困惑,只是寻求帮助:L。这就是我的导师的指示。
说明: 使用哨兵值循环。
询问每个用户:
- 车辆类型(可以使用字符串以外的其他内容,例如:1 经济型、轿车 2 等)
- 租赁天数
计算(对于每个客户):
- 租金成本,
- 税收,
- 应付款总额。
共有三种不同的租赁选项,价格各不相同:经济型 @ 31.76、轿车 @ 40.32、SUV @ 47.56。 [注意:仅考虑全天单位(无小时费率)]。
销售税 = 总额的 6%。
创建摘要数据:
- 客户数量
- 收取的总金额。
此外,还包括 IPO、算法和案头检查值(设计文档)。
{我要做的事情和我的问题}
package yipe;
public class Umm {
import java.util.*;
int count = 0;
static int CarType, days;
static double DailyFee, Total;
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
System.out.print("What vehical would you like to rent?\n");
System.out.println("Enter 1 for an economy car\n");
System.out.println("Enter 2 for a sedan car\n");
System.out.println("Enter 3 for an SUV");
CarType = keyboard.nextInt();
if (CarType == '1')
DailyFee=(int)31.76;
else if(CarType == '2')
DailyFee=(int)40.32;
else if(CarType == '3')
DailyFee=(int)43.50;
System.out.print("Please enter the number of days rented. (Example; 3) : ");
days = keyboard.nextInt();
Total = (DailyFee * days * 6/100);
System.out.printf("The total amount due is $" + Total);
}
}
- 如何修正 IF 语句以获得正确的数学结果?
- 如何让它循环输入多个信息?
- 如何制作汇总数据?
- 如何将总计四舍五入到小数点后两位?
最佳答案
请注意,'1'
实际上是字符 1
,不是整数1
。它们实际上非常不同。
在 Java(以及 C#)中,int
和 char
类型可以相互转换。
为了说明这一点,下面实际上打印了 49:
public class HelloWorld
{
public static void main(String[] args)
{
System.out.print((int)'1');
}
}
类似地,以下内容打印 true
:
System.out.println('1' == 49);
如您所见,字符被隐式转换为等效的 int
值。
要了解为什么'1'
等于 49,请查看字符的表示方式。特别是,请查看 ASCII chart (这是字符编码的常见约定) - 事实证明,字符 '1'
是 ASCII 49。实际上,我们可以执行与上面“相反”相同的操作来“转换” ASCII 49 为其等效字符,以下行打印 1
:
System.out.println((char)49);
要了解这种转换的工作原理,您可能需要阅读 this rather excellent article linked to in the comments 。如果您对 C# 中的工作原理感到好奇,您可能还想阅读 this question .
还有一点:当您执行 DailyFee=(int)31.76
时,将其转换为 int
实际上会“丢弃”小数点后的所有内容,所以这是与编写 DailyFee = 31
没有什么不同。这是因为 31 是整数,而 31.76 不是整数(它是有理数)。
一个小风格点:您可以考虑在此处使用 switch
语句。
关于java - 用java编写一个基本的汽车租赁程序/问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52999147/