gpt4 book ai didi

java - 不兼容的类型可能会从 double 到 int 进行有损转换

转载 作者:行者123 更新时间:2023-12-01 11:39:29 26 4
gpt4 key购买 nike

我试图将总计添加到我的对话框中,但我收到一条错误消息,而且我似乎无法修复它。如果我尝试将总计放入 for 循环中,它只会在对话框中打印 5 次。我确实需要在对话框中最后打印总计

import javax.swing. JOptionPane;

public class BookTest
{
public static void main(String args[])
{
double charge;
double grandTotal= 0;


String dataArray[][] = {{"Abraham Lincoln Vampire Hunter","Grahame-Smith","978-0446563079","13.99", "Haper", "NY"},
{"Frankenstein","Shelley","978-0486282114","7.99","Pearson", "TX"},
{"Dracula","Stoker","978-0486411095","5.99","Double Day", "CA"},
{"Curse of the Wolfman"," Hageman","B00381AKHG","10.59","Harper", "NY"},
{"The Mummy","Rice","978-0345369949","7.99","Nelson", "GA"}};




Book bookArray[] = new Book[dataArray.length];

int quantityArray[] = {12, 3, 7, 23, 5};

for (int i = 0; i < dataArray.length; i++)
{
bookArray[i] = new Book(dataArray[i][0], dataArray[i][1], dataArray[i][2],
Double.parseDouble(dataArray[i][3]), new Publisher(dataArray[i][4], dataArray[i][5]));
}

String msg = " ";



for (int i = 0; i < bookArray.length; i++)
{

charge = bookArray[i].calculateTotal(quantityArray[i]);

grandTotal = charge + grandTotal;

msg += String.format(" %s, %s, $%.2f\n", bookArray[i].getTitle(), bookArray[i].getIsbn(), charge);


}


JOptionPane.showMessageDialog(null, msg, "Grand Total $%.2f ", grandTotal); //

}

}

最佳答案

您正在尝试将 grandTotal 作为 showMessageDialog 的消息类型传递.

我怀疑你的意思是这样的:

JOptionPane.showMessageDialog(
null, msg,
String.format("Grand Total $%.2f", grandTotal),
JOptionPane.INFORMATION_MESSAGE
);

另请参阅"How to Make Dialogs" .

关于java - 不兼容的类型可能会从 double 到 int 进行有损转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29661975/

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