gpt4 book ai didi

java - Main 方法错误地显示类字符串?

转载 作者:行者123 更新时间:2023-12-02 05:13:29 25 4
gpt4 key购买 nike

我正在编写一个程序,该程序提供酒店住宿的最终账单,其中包含房间号、客人人数、入住时间以及每人每晚的价格等参数。

我的 BBRoom 类中的数学正确,但它似乎没有获取和处理类参数中的信息?

驱动程序类别:

public class BedAndBreakfastDriver {

public static void main(String[] args) {

//float price, int rmNum, int numOccupants, int duration
//or
//int rmNum, int numOccupants, int duration, float price

BBRoom Smith;

Smith = new BBRoom(29.95, 16, 1, 5);

System.out.println(Smith);

}

}

BBRoom 类(class):

public class BBRoom {

final int MAXCAP = 4;
final int MINCAP = 1;
final int MINSTAY = 1;

int room;
int persons;
int nights;
double cost;
double surcharge;
double cleanUp;
double cottageCost;

NumberFormat fmt1 = NumberFormat.getCurrencyInstance();

BBRoom(double price, int rmNum, int numOccupants, int duration){}


BBRoom( int rmNum, int numOccupants, int duration, double price){}

private int getRoomNumber(int rmNum){
room = rmNum;
return room;
}

private int getPersons(int numOccupants){
if (numOccupants < MINCAP){
persons = MINCAP;
}if(numOccupants > MAXCAP){
persons = MAXCAP;
}else{
persons = numOccupants;
}
return persons;
}

private int getNights(int duration){

if(duration < MINSTAY){
nights = MINSTAY;

}else{

nights = duration;

}

return nights;
}

private double setCost(double price){
return price;

}
private double getCost(double price){



if (persons < 2){
cost = price*2*nights;
}else{
cost = (persons*price*nights);
}

return cost;
}

private double BBCottage(double cost){

surcharge = (12.95*nights);

cleanUp = 47.99;

cottageCost = cost+surcharge+cleanUp;

return cottageCost;

}

public String toString(){

String bill = ("Room Number"+room+" "+"Guests:"+persons+" "+"Nights:"+nights+" "+"Basic Package:"+ fmt1.format(cost)+" "+"Cottage Upgrade:"+fmt1.format(cottageCost) );
return bill;
}

}

出于某种原因,我不断获得输出以将每个变量显示为

“房间号:0 客人:0 晚数:0 基本套餐:0.00 小屋升级:0.00”

如有任何帮助,我们将不胜感激。谢谢!

附注我还希望将 BBCotage 方法变成一个子类,但我不太确定如何实现这一点。如果我也能得到一些指导,那就太好了!

最佳答案

你的构造函数有一个空的主体

BBRoom(double price, int rmNum, int numOccupants, int duration){} // <<<<<<< nothing going on in {}

所有相关字段都初始化为其默认值0。实现构造函数来执行您想要的操作。

关于java - Main 方法错误地显示类字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27161439/

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