gpt4 book ai didi

java - if else 语句赋值

转载 作者:行者123 更新时间:2023-12-01 09:24:47 25 4
gpt4 key购买 nike

编辑:我重新编写了一些代码,但如果用户输入的时间少于一周,我仍然无法让它计算费率。我的新代码如下。如有任何帮助,我们将不胜感激。我必须编写一段代码来模拟给出汽车租赁报价,其中用户输入颜色、租赁天数,并在两种类型的车辆之间进行选择。每种类型有两种费率,每周一次,每天一次。然后我必须找到最佳速率并将其输出。当我在下面的代码中输入白色、经济和 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);
}



}

最佳答案

您需要考虑一些事情:

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

关于java - if else 语句赋值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39928419/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com