我在这里做作业,我遇到了问题
“TotalCharge = ptr.calculateCharge(NightsStay,perNight)”旁边会出现一个红色标记;“并且错误是double不能转换为Integer。
我尝试过研究解决问题,但没有任何工作。
非常感谢你。
HiibiscusHotelSpa9674 ptr = new HiibiscusHotelSpa9674();
Scanner keyboard = new Scanner(System.in);
Integer compare = 0;
String response = null;
String number = null;
Double Price = 0.00;
Integer TotalCharge = 0;
Integer ItemNo = 0;
String surName;
Integer perNight = 0;
Integer roomNumber = 0;
Double amountPaid = 0.0;
String temp = null;
String Name = null;
Double cashPaid = 0.0;
Double Change = 0.0;
Integer NightsStay = 0;
temp = JOptionPane.showInputDialog("Enter The amount of Items :");
int Size = Integer.parseInt(temp);
String[] ItemName = new String[Size];
Integer[] ItemId = new Integer[Size];
double[] ItemPrice = new double[Size];
Integer index = 0;
while (index < Size) {
ItemName[index] = JOptionPane.showInputDialog("Enter The Item Name:");
temp = JOptionPane.showInputDialog("Enter Item ID for " + ItemName[index] + " :");
ItemId[index] = Integer.parseInt(temp);
temp = JOptionPane.showInputDialog("Enter The Price for " + ItemName[index] + " :");
ItemPrice[index] = Double.parseDouble(temp);
index++;
ptr.displayMenu(ItemName, ItemId, ItemPrice);
Name = ptr.getDataSetA();
cashPaid = ptr.getDataSetB(ItemId);
Change = ptr.performCalc(cashPaid, ItemPrice);
ptr.displayResults(Change, cashPaid);
response = JOptionPane.showInputDialog("Sale Complete? Enter Y or N: ");
switch (response) {
case "N":
perNight = ptr.GetGuestInfo();
NightsStay = ptr.GetDate();
TotalCharge = ptr.calculateCharge(NightsStay, perNight);
ptr.displayGuestBill(NightsStay, TotalCharge);
break;
case "n":
perNight = ptr.GetGuestInfo();
NightsStay = ptr.GetDate();
TotalCharge = ptr.calculateCharge(NightsStay, perNight);
ptr.displayGuestBill(NightsStay, TotalCharge);
break;
default:
JOptionPane.showMessageDialog(null, "Thank you very much and haave a wonderful day!");
}
public void displayMenu(String Name[], Integer ItemId[], double Price[]) {
System.out.printf("Item Name Item ID ItemPrice\n");
for (int i = 0; i < Name.length; i++) {
System.out.printf("%s\t %d\t %.2f\t\n", Name[i], ItemId[i], Price[i]);
}
}
public String getDataSetA() {
Scanner keyboard = new Scanner(System.in);
String[] personalInformation = new String[2];
String NameRoomNumber = null;
System.out.printf("Please Enter Room Number\n");
personalInformation[1] = keyboard.next();
System.out.printf("Enter your Surname\n");
personalInformation[0] = keyboard.next();
String NameNumber = Arrays.toString(personalInformation);
return NameRoomNumber;
}
private Double getDataSetB(Integer[] ItemId) {
Scanner keyboard = new Scanner(System.in);
String temp = null;
for (int i = 0; i < ItemId.length; i++) {
double cashPaid = 0.0;
Integer[] Items = new Integer[ItemId.length];
System.out.println("Please Enter the Item ID for the item");
ItemId[i] = keyboard.nextInt();
}
System.out.printf("Please Enter Cash Paid\n");
double cashPaid = keyboard.nextDouble();
return cashPaid;
}
private double performCalc(double cashPaid, double[] Price) {
Scanner keyboard = new Scanner(System.in);
double Cost = 0.0;
double change = 0.0;
double moneyOwe = 0.0;
for (int i = 0; i < Price.length; i++) {
Cost = Cost + Price[i];
}
if (cashPaid < Cost) {
moneyOwe = Cost - cashPaid;
JOptionPane.showMessageDialog(null, "You are $" + moneyOwe + " short");
} else {
change = cashPaid - Cost;
}
return change;
}
private void displayResults(Double Change, Double cashPaid) {
JOptionPane.showMessageDialog(null, "Cash Paid:$ " + cashPaid + "Change:$ " + Change + ".");
}
private Integer GetGuestInfo() {
String temp = null;
String Guestname = null;
Integer FloorNum = 0;
Integer NoofPer = 0;
Guestname = JOptionPane.showInputDialog("Please Enter Guest Name");
temp = JOptionPane.showInputDialog("Enter Floor Required");
FloorNum = Integer.parseInt(temp);
temp = JOptionPane.showInputDialog("Please Enter The Number of Persons Staying with you");
NoofPer = Integer.parseInt(temp);
return NoofPer;
}
private Integer GetDate() {
String temp = null;
Integer day = 0;
Integer month = 0;
Integer year = 0;
Integer Nights = 0;
temp = JOptionPane.showInputDialog("Please enter the Day: ");
day = Integer.parseInt(temp);
temp = JOptionPane.showInputDialog("Please enter the Month: ");
month = Integer.parseInt(temp);
temp = JOptionPane.showInputDialog("Please enter the Year: ");
year = Integer.parseInt(temp);
temp = JOptionPane.showInputDialog("Please enter the number of Nights staying:");
Nights = Integer.parseInt(temp);
return Nights;
}
private Double calculateCharge(Integer amtofNights, Integer perNight) {
Integer floor = 0;
double totalCharge = 0.0;
if (floor > 4) {
// int perNight = 0;
int Numofnights = 0;
totalCharge = 150 * perNight * amtofNights;
} else {
totalCharge = 100 * perNight * amtofNights;
}
return totalCharge;
}
private void displayGuestBill(Integer NightsStayed, Integer totalCharge) {
System.out.printf("The total number of nights:%d\n", NightsStayed);
System.out.printf("The total Charge:%d", totalCharge);
}
}
答案 0 :(得分:0)
您无法将双精度转换为整数,因为您正在失去精度。想象一下,将3.7
转换为整数,您将获得3
,但会丢失大量信息。
如果您对此很好,您可以使用(int)some_double
进行显式演员。
您的代码示例不包含任何类型,因此很难说您的转换发生在哪个位置。
编辑:由于您的代码示例现在包含类型,因此您的calculateCharge可能会返回一个double,但您将其存储为整数。
您应该通过执行
强制执行显式转换TotalCharge = (int)ptr.calculateCharge(NightsStay, perNight);
答案 1 :(得分:0)
Double
是原始double之上的包装类。它可以强制转换为double,但不能直接转换为int。
如果您使用double
代替Double
,则会编译:
double d = 10.9;
int i = (int)(d);