gpt4 book ai didi

java - 如何在 Java 中使用用户输入填充构造函数?

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:49:01 26 4
gpt4 key购买 nike

我在一项任务中遇到了瓶颈,一直在梳理网站以寻找任何有用的东西(空无一物)。我需要创建一个类,该类中的一个构造函数,然后是一个子类来扩展父类(super class)。然后,我需要创建一个包含 main 方法的新文件来演示这两种情况。没问题,概念上。

我的问题是:如何使用构造函数初始化一个对象,但需要用户输入?

现在我得到的错误是:“类 CarRental 中的构造函数 CarRental 不能应用于给定类型; 必需:字符串,整数,字符串,整数 发现:没有参数 原因:实际和形式参数列表的长度不同"

请不要对“错误告诉您问题所在”进行冷嘲热讽。不,它没有告诉它是什么。我是这方面的小宝贝,需要一点帮助。

我将在下面粘贴我的 3 个类(class)。他们可能会让你痛苦地扭动 body ,因为我是个新手(另外,我的类(class)是一个为期 8 周的缩减类(class),几乎没有时间专门用于伪代码,所以我面临着构思逻辑本身的额外挑战)。

我不是在找任何人帮我做作业,我只是在 UseCarRental.java 文件中寻找帮助。这是我的代码..

public class CarRental {
protected String renterName;
protected int zipCode;
protected String carSize;
protected double dailyRate;
protected int rentalDays;
protected double totalCost;
final double ECONOMY = 29.99;
final double MIDSIZE = 38.99;
final double FULLSIZE = 43.50;

public CarRental(String renterName, int zipCode, String carSize, int rentalDays){

totalCost = dailyRate * rentalDays;
}
public String getRenterName(){
return renterName;
}
public void setRenterName(String renter){
renterName = renter;
}
public int getZipCode(){
return zipCode;
}
public void setZipCode(int zip){
zipCode = zip;
}
public String getCarSize(){
return carSize;
}
public void setCarSize(String size){
carSize = size;
}
public double getDailyRate(){
return dailyRate;
}
public void setDailyRate(double rate){
switch (getCarSize()) {
case "e":
rate = ECONOMY;
break;
case "m":
rate = MIDSIZE;
break;
case "f":
rate = FULLSIZE;
break;
}
}
public int getRentalDays(){
return rentalDays;
}
public void setRentalDays(int days){
rentalDays = days;
}
public double getTotalCost(){
return totalCost;
}
public void setTotalCost(double cost){
totalCost = cost;
}

public void displayRental(){
System.out.println("==============================================");
System.out.println("Renter Name: " + getRenterName());
System.out.println("Renter Zip Code: " + getZipCode());
System.out.println("Car size: " + getCarSize());
System.out.println("Daily rental cost: $" + getDailyRate());
System.out.println("Number of days: " + getRentalDays());
System.out.println("Total cost: $" + getTotalCost());

}

}

子类 LuxuryCarRental....

public class LuxuryCarRental extends CarRental {

final double chauffeur = 200.00;
final double dailyRate = 79.99;
protected String chauffeurStatus;

public LuxuryCarRental(String renterName, int zipCode, String carSize, int rentalDays) {
super(renterName, zipCode, carSize, rentalDays);
}

public String getChauffeurStatus(){
return chauffeurStatus;
}
public void setChauffeurStatus(String driver){
chauffeurStatus = driver;
}
public double getChauffeurFee(){
return chauffeur;
}
public void setTotalLuxuryCost(){
if (chauffeurStatus=="y")
setTotalCost((dailyRate * getRentalDays()) + (chauffeur * getRentalDays()));
else
setTotalCost(dailyRate * getRentalDays());
}

@Override
public void displayRental(){
System.out.println("==============================================");
System.out.println("Renter Name: " + getRenterName());
System.out.println("Renter Zip Code: " + getZipCode());
System.out.println("Car size: " + getCarSize());
System.out.println("Optional Chauffeur fee: $" + getChauffeurFee());
System.out.println("Daily rental cost: $" + getDailyRate());
System.out.println("Number of days: " + getRentalDays());
System.out.println("Total cost: $" + getTotalCost());

}
}

现在是带有 main 方法的类:

import java.util.Scanner;
public class UseRentalCar {

public static void main(String[] args){
Scanner keyboard = new Scanner(System.in);
CarRental rentalCar = new CarRental();

System.out.println("==========================");
System.out.println("RENTAL CAR SELECTION");
System.out.println("==========================");
System.out.println("Enter your name: ");
rentalCar.setRenterName(keyboard.next());
System.out.println("Enter your zip code: ");
rentalCar.setZipCode(keyboard.nextInt());
System.out.println("Enter the car size ("e=Economy, m=Midsize, f=Fullsize: ");
rentalCar.setCarSize(keyboard.next());
System.out.println("Enter the number of days: ");
rentalCar.setRentalDays(keyboard.nextInt());

rentalCar.displayRental();



}
}

(省略了一些无关紧要的,主要是试图让对象实例化工作)

感谢您的帮助!!

最佳答案

在您的 main 方法中创建局部变量,比如 String 和 int 变量,然后在这些变量填充了用户输入后,使用它们调用构造函数。

我将发布一个通用示例,因为这是作业,最好向您展示概念,然后让使用该概念来创建代码:

public class Foo {
private String name;
private int value;

public Foo(String name, int value) {
this.name = name;
this.value = value;
}
}

别处

import java.util.Scanner;

public class Bar {

public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
System.out.print("Please enter name: ");
String name = keyboard.nextLine(); // local variable
System.out.print("Please enter value: " );
int number = keyboard.nextint(); // another local variable
keyboard.nextLine(); // to handle the end of line characters

// use local variables in constructor call
Foo foo = new Foo(name, number);
}

关于java - 如何在 Java 中使用用户输入填充构造函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20442044/

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