gpt4 book ai didi

Java - Setters 和继承 - 为什么我的 dailyFee 参数设置不正确?

转载 作者:行者123 更新时间:2023-11-29 04:55:08 24 4
gpt4 key购买 nike

<分区>

大家晚上好!

作业越来越困惑了!同样,不是要求直接的答案,而是指向正确方向的指针,因此我们将不胜感激。

编辑 2:这里是有问题的练习,如果它有助于清除任何东西:

enter image description here

我想知道如何使用 UseCarRental.java 文件在 CarRental.java 文件中设置 dailyFee 变量。 CarRental.java 中的 if 语句似乎无法正常工作(输入大小不会设置 dailyFee 变量)。此外,如果我为尺寸输入“l”,它不会提示 LuxuryCarRental.java。这是代码:

汽车租赁.java

import javax.swing.JOptionPane;

public class CarRental
{
private String name;
private String zipCode;
private String size;
private double dailyFee;
private int rentalDays;
private double totalFee;

public CarRental(String name, String zipCode, String size, double dailyFee, int rentalDays, double totalFee)
{
this.name = name;
this.zipCode = zipCode;
this.size = size;
if (size == "e")
{
this.dailyFee = 29.99;
}
else if (size == "m")
{
this.dailyFee = 38.99;
}
else if (size == "f")
{
this.dailyFee = 43.50;
}
this.dailyFee = dailyFee;
this.rentalDays = rentalDays;
totalFee = dailyFee * rentalDays;
}

public CarRental(){}

public void display()
{
JOptionPane.showMessageDialog(null, "Luxury car for " + name + " from zip code " + zipCode + "\n"
+ "Type = " + size + "\n"
+ "Daily Fee = " + dailyFee + "\n"
+ "Days = " + rentalDays + "\n"
+ "Your rental is $" + totalFee);
}

//includes getters and setters but I didn't include them in this post

LuxuryCarRental.java

import javax.swing.JOptionPane;

public class LuxuryCarRental extends CarRental
{
public LuxuryCarRental(String name, String zipCode, String size, double dailyFee, int rentalDays, double totalFee, String includeChauffeur)
{
super(name, zipCode, size, dailyFee, rentalDays, totalFee);
if (size == "l")
{
dailyFee = 79.99;
includeChauffeur = JOptionPane.showInputDialog(null, "Include chauffeur? Y/N");
if (includeChauffeur == "Y")
{
totalFee = totalFee + 200;
}
}
}
}

UseCarRental.java(不完整)

import javax.swing.JOptionPane;

public class UseCarRental
{

public static void main(String[] args)
{
CarRental userInfo = new CarRental();
userInfo.setName(JOptionPane.showInputDialog("Enter name"));
userInfo.setZipCode(JOptionPane.showInputDialog("Enter zip code"));
userInfo.setSize(JOptionPane.showInputDialog("Enter type of car" + "\n" + "e - economy" + "\n" + "m - midsize" + "\n" + "f - full" + "\n" + "l - luxury"));
userInfo.setRentalDays(Integer.parseInt(JOptionPane.showInputDialog("Enter days to rent")));


System.out.println(userInfo.getDailyFee());

userInfo.display();

}
}

如您所见,我完全不知道如何使用 UseCarRental.java 中的 setDailyFee 来设置 dailyFee 变量,这是进行 totalFee 计算所需要的。

编辑:被要求解释为什么这不是一个重复的问题。我修复了字符串比较问题(此处尚未更新),但核心问题仍然是 setter 和继承。

编辑 3:嗯,我想这个世纪的问题(为了清楚起见)是:如何设置 dailyFee 值?我以为一旦输入大小,它就会由 CarRental.java 中的 if 语句设置?

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