java - 写一个关于java/问题的基本租车程序
问题描述
所以我非常困惑,只是在寻求帮助: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 图表(这是字符编码的常用约定) - 结果表明该字符'1'
是 ASCII 49。事实上,我们可以做与上面“反向”相同的事情来“转换”ASCII 49到它的等效字符,并且下面的行打印1
:
System.out.println((char)49);
要了解这种转换是如何工作的,您可能需要阅读评论中链接的这篇相当出色的文章。如果您对它在 C# 中的工作方式感到好奇,您可能还想阅读这个问题。
还有一点:当您这样做时DailyFee=(int)31.76
,将其转换为 anint
实际上会“删除”小数点后的所有内容,因此这与编写DailyFee = 31
. 这是因为 31 是整数,而 31.76不是(它是一个有理数)。
一个小的风格点:你可以考虑switch
在这里使用一个声明。
推荐阅读
- gentoo - 台面标头之间出现冲突
- docker - How to dockerizing a spring boot app with one Dockerfile?
- c - 有没有办法在实际执行卸载之前“尝试卸载”?
- cookies - 如何在 ASP.NET Core 中设置永不过期的 cookie?
- python - 获取 i 和 j 的条件匹配的行
- javascript - 将寻呼机添加到表格的顶部和底部
- java - 使用 RestTemplate 发送包含 json 值的文件
- swift - Swift 4 在视图之间传递数据
- java - 在循环期间刷新 JLayeredPane()
- ruby-on-rails - initialize_with factory_girl 的参数数量错误