我有这个主要课程
public class Hotel
{
Scanner scan = new Scanner(System.in);
Register[] items = new Register[15];
int count = 0;
public static void main(String[] args)
{
DecimalFormat fmt = new DecimalFormat("0.##");
Hotel run = new Hotel();
int quantity, sale,night;
Register deluxe = new Deluxe();
Register family = new Family();
Register suite = new Suite();
Scanner input = new Scanner(System.in);
System.out.println("Enter customer's name:");
String name = input.next();
run.enterItems();
if(run.count != 0)
{
System.out.println("\nHotel Reservation Payment");
System.out.println("============================");
System.out.println("Customer name: " + name);
deluxe.displayInfo(); //supposed to print the details
family.displayInfo(); //supposed to print the details
suite.displayInfo(); //supposed to print the details
System.out.println("The final total is RM" + fmt.format(run.calcTotal()));
}
else
{
System.out.println("No items entered.");
run.enterItems();
}
}
public double calcTotal()
{
double total = 0;
for(int i = 0;i<count;i++)
{
total += items[i].total();
}
return total;
}
应该将我在扫描仪中输入的值返回到类中作为主类的enterItems()
public void enterItems()
{
int type, quantity, sale,night;
double price;
............................
System.out.println("\nNow please enter how many of Deluxe Room you want to book.");
quantity = scan.nextInt();
System.out.println("\nHow many night?");
night = scan.nextInt();
items[count] = new Deluxe(quantity,sale,night);
count++;
}
所以这将传递给名为Deluxe
的类,其中我有一个名为displayInfo()
的方法
public class Deluxe implements Register
{
int quantity,night;
double sale;
public Deluxe(){}
....................................
public double total()
{
double total, price = 200.0;
price = price*quantity*night;
total = price - (price * (sale/100));
total += total *.06;
return total;
}
public void displayInfo(){
if (quantity > 0){
System.out.println("Room Type : Deluxe Room");
System.out.println("Quantity: " +quantity);
System.out.println("Discount: " +sale);
}
}
}
问题是,在检查quantity > 0
时,它实际上没有得到我在扫描仪中输入的任何值。无论我投入多少金额,它总是会返回0。
但计算工作正常。计算是计算房间预订多少(数量)房间x房间的价格。
该计算也与类displayInfo()
中的Deluxe
属于同一类。
所以我想知道我在这里做错了什么。
答案 0 :(得分:1)
您在此处输入的数量:
System.out.println("\nNow please enter how many of Deluxe Room you want to book.");
quantity = scan.nextInt();
进入此处定义的main(String[] args).quantity
:
public class Hotel
{
....
public static void main(String[] args)
{
...
int quantity, sale,night;
...
}
您在此处查看的数量:
if (quantity > 0){...}
是一个不同的参数 - 它是Deluxe.quantity
并在此处定义:
public class Deluxe implements Register
{
int quantity,night;
...
}
这两个参数没有关系。如果您希望它们都相同,则需要将Deluxe
参数传递给类main(String[] args).quantity
。您可以通过Hotel.main(String[] args)
下的自己的构造函数执行此操作,如下所示:
DecimalFormat fmt = new DecimalFormat("0.##");
Hotel run = new Hotel();
int quantity, sale,night;
Register family = new Family();
Register suite = new Suite();
Scanner input = new Scanner(System.in);
System.out.println("Enter customer's name:");
String name = input.next();
quantity = run.enterItems();
Register deluxe = new Deluxe(quantity,0,0); // entering the default value for the other two parameters like the empty constructor would leave them.
if(run.count != 0)
{
System.out.println("\nHotel Reservation Payment");
System.out.println("============================");
System.out.println("Customer name: " + name);
deluxe.displayInfo(); //supposed to print the details
family.displayInfo(); //supposed to print the details
suite.displayInfo(); //supposed to print the details
System.out.println("The final total is RM" + fmt.format(run.calcTotal()));
}
else
{
System.out.println("No items entered.");
run.enterItems();
}
如果您更改enterItems()
函数以返回所需的数量参数,例如:
public int enterItems()
{
int type, quantity, sale,night;
double price;
.........
System.out.println("\nNow please enter how many of Deluxe Room you want to book.");
quantity = scan.nextInt();
System.out.println("\nHow many night?");
night = scan.nextInt();
items[count] = new Deluxe(quantity,sale,night);
count++;
return quantity;
}
注意,这仅针对数量参数解决了这个问题。如果您需要更多内容,则可能需要从Deluxe
返回enterItems()
结构。祝你好运!