gpt4 book ai didi

Java编程简单的If/Else语句项目

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

好吧,伙计们,我一直在尝试用 java 编写一些代码,我无法获取代码来显示全尺寸的定价选项。 我无法让程序继续执行我列为案例 2 的第二个选项。

该项目基本上为用户提供了询问他是否租车的选项[是或否]:

  • 如果输入Y则下一个问题

    • 它询问的是“全尺寸紧凑型?”
      • 如果用户选择紧凑,项目将显示用户已选择紧凑,并且
      • 如果代码显示 fullsize,则项目将显示用户已选择 fullsize。
  • 然后它会询问用户是否有优惠券,如果用户对优惠券回答 Y,则价格为 30.50 的 7% 折扣。

    • 如果用户回答 N,则价格为正常的 30.50。全尺寸正常价格为 40.50,带优惠券的价格为 40.50 的 7% 折扣。以下是我目前编写的代码。

代码:

public class CarRental {
public static void main(String[] args) {
for (int i = 0; i < 4; i++) {

System.out.println("Programmed by .");

double standardCompact = 30.50;
double couponCompact = ((30.50) - (30.50 * 0.07));
double standardFullSize = 40.50;
double couponFullSize = ((40.50) - (40.50 * 0.07));

//Scanner Input
Scanner input = new Scanner(System.in);
System.out.print("Rent a Car? [Y or N]: ");

//Response String
String response = input.next().toUpperCase();

if (response.equals("N")) {
System.out.println("You entered no. Bye. ");
} else if (response.equals("Y")) {
System.out.print("Compact or Full-Size? [C or F]: ");

//case1
response = input.next().toUpperCase();
if (response.equals("C")) {
System.out.println("You selected Compact. ");
} else if (response.equals("F")) {
System.out.println("You have selected Full-Size. ");

System.out.print("Have coupon? [Y or N]: ");
response = input.next().toUpperCase();
if (response.equals("N")) {
System.out.println("Price is" + " " + standardCompact + " " + "per day.");
} else if (response.equals("Y")) {
System.out.println("Price is" + " " + couponCompact + " " + "per day.");
//case 2
response = input.next().toUpperCase();
if (response.equals("F")) {
System.out.println("You have selected Full-Size.");
System.out.println("Have coupon? [Y or N]: ");
response = input.next().toUpperCase();
if (response.equals("N")) {
System.out.println("Price is" + " " + standardFullSize + " " + "per day.");
} else if (response.equals("Y")) {
System.out.println("Price is" + " " + couponFullSize + " " + "per day.");

}
}
}

最佳答案

您在 else 子句后面缺少一些 }。示例:

        response = input.next().toUpperCase();
if (response.equals("C")) {
System.out.println("You selected Compact. ");
//Put code that should only execute if you select Compact here.
}else if(response.equals("F")){
System.out.println("You have selected Full-Size. ");
//Put code that should only execute if you select Full-size here.
//Should have a } here!
//Put code that should always execute here.

因为您从未关闭 else 子句中的代码块,所以后面的所有代码仍然是 else 的一部分,因此只有在以下情况下才会执行else 被选中,但并非在所有情况下都如您所愿。

关于Java编程简单的If/Else语句项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21650116/

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