java scanner构造函数_如何用Java中的用户输入填充构造函数?

我在作业上碰壁,一直在梳理网站以寻求任何有用的信息(出现空白)。我需要创建一个类,在该类中创建一个构造函数,然后再创建一个子类以扩展超类。然后,我需要使用main方法创建一个新文件来演示这两种情况。从概念上来说没问题。

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

现在我得到的错误是:“类CarRental中的构造函数CarRental无法应用于给定类型;必需:String,int,String,int找到:无参数原因:实际和正式参数列表的长度不同”

请不要对“错误告诉您问题出在哪里”发表任何评论。不,它没有告诉 我 它是什么。我是这个东西的小宝贝,需要一点点握力。

我将在下面粘贴我的3个班级。因为我真是个新手,它们可能会让您痛苦不已(而且,我的课程是8周的简短课程,实际上没有时间专门用于伪代码,因此我对逻辑本身也有额外的挑战)。

我并不是在寻找任何人为我做作业,我只是在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();

}

}

(省略了一些无关紧要的原因,主要是试图使对象实例化起作用)

谢谢你的帮助!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值