有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java if-else语句赋值

编辑:我修改了一些代码,但如果用户输入的时间少于一周,我仍然无法让它计算费率。我的新代码如下。任何援助都将得到感谢。 我必须编写一个代码来模拟给出一个租车报价,用户输入颜色、租车天数,并在两种类型的车辆之间进行选择。每种类型有两种价格,一周一次,一天一次。然后我必须找到最佳速率并输出它。当我在下面的代码中输入white、economy和4时,它不会输出任何东西。我想在不到一周的时间里,我的汇率出现了一个错误,但我想不出如何解决它

 import java.util.Scanner;
public class lab3
 {


    public static final double ECONOMY_DAILY = 25.5;
    public static final double FULL_DAILY = 39.4;
    public static final double ECONOMY_WEEKLY = 120.5;
    public static final double FULL_WEEKLY = 216.25;
    public static void main (String[] args)
     {
        Scanner Keyboard = new Scanner(System.in);

        System.out.println("Enter the color of the vehicle:");
            //string the next input as color
        String color = Keyboard.next();

        System.out.println ("Economy or Full:");
            // string the type of the vehicle
        String Type = Keyboard.next();
            // to get the character value to uppercase for the switch statement
        char FirstTypeLetter = Type.toUpperCase()
        .charAt(0); 
        System.out.println("For how many days?");
            //set days as the next integer entered and calculate the amount of weeks and daysleftover using the / and % operators
        int days = Keyboard.nextInt();
        int weeks = days/7;
        int daysLeftOver = days%7;
        double weeksRounded = ((days/7)*100)/100;

    double rate1,rate2,rate3;

        // create a switch using the variable defined earlier
    switch (FirstTypeLetter) 
    {
            // if the Type entry starts with an e
            case 'F':
                // calculate the 3 rates for full size using the full size constants (could have put this code anywhere above the next if statement.)
            rate1 = weeksRounded * FULL_WEEKLY;
            rate2 = (weeks * FULL_WEEKLY) + (days * FULL_DAILY);
           rate3 = (days * ECONOMY_DAILY);
           break;
        case 'E':
            // calculate all available rents for economy using the constants defined earlier
   rate1 = weeksRounded * ECONOMY_WEEKLY;
     rate2 = (weeks * ECONOMY_WEEKLY) + (days * ECONOMY_DAILY);
     rate3 = (days * ECONOMY_DAILY);

        break;
        default:
       System.out.println("Try Again!");
        rate1 = 0;
        rate2 = 0;
        rate3 = 0;
       }
            if ((rate1 < rate2) & (rate1 < rate3) & (rate1 != 0)) 
            {   
                    // print out the first rate as well as the color and type that the user entered
                System.out.printf("This is the best rate for a" + " " + color + " " + Type + " vehicle for" + " " + days + "days:" + "%.2f",rate1);
            }
                // if not, and if the second rate is cheapest
            if ((rate2 < rate1) & (rate2 < rate3) & (rate1 != 0))
            {
                    // print out the second rate as well as the color and type that the user entered
                System.out.printf("This is the best rate for a" + " " + color + " " + Type + " vehicle for" + " " + days + "days:" + "%.2f", rate2);
            }
                    // if the third rate is cheapest then print out that rate
            else if ((rate3 < rate2) & (rate3 < rate1) & (rate3 != 0)) 

                System.out.printf("This is the best rate for a" + " " + color + " " + Type + " vehicle for" + " " + days + "days:" + "%.2f", rate3);
            }



   }

共 (2) 个答案

  1. # 1 楼答案

    您的第二个和第三个if stmt位于您的第一个if块中。把它移到外面去

  2. # 2 楼答案

    您需要考虑以下几点:

    • 案例F和案例E之间缺少一个断点,这意味着 费率F将替换为费率E
    • 比较速率的逻辑(第53行)不应仅应用于switch语句之外的“E”情况
    • 没有使用每日选项验证费率的选项(如果天数少于7天,则将超出周期(检查逻辑以计算“费率1”)
    • 您应该使用调试器来验证分配给程序的值(使用JUnit或testNG的自动单元测试将变得很方便)
    • 如果您没有调试器,System.out.println()可以用于验证指定的值,只需记住将它们取出即可