首页 > 解决方案 > 写一个关于java/问题的基本租车程序

问题描述

所以我非常困惑,只是在寻求帮助: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);

    }


}
  1. 如何修复我的 IF 语句以获得正确的数学结果?
  2. 我如何让它循环输入多个信息?
  3. 如何制作汇总数据?
  4. 如何将总数四舍五入到小数点后两位?

标签: javaloopsif-statementsummarysentinel

解决方案


请注意,'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在这里使用一个声明。


推荐阅读