所以我非常困惑,只想寻求帮助:L。这就是我老师的指导。
说明:使用标记值循环。
询问每个用户:
计算(针对每个客户):
有三种不同的租车选择,单独收费:经济舱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);
}
}
发布于 2018-10-26 07:28:00
请注意,'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'
是ASCII49。实际上,我们可以执行与上面“反向”相同的操作,将ASCII49“转换”为它的等效字符,下面的代码行将打印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
语句。
https://stackoverflow.com/questions/52999147
复制相似问题