我试图在我的对话框中添加一个Grand Total,但是我收到一条错误消息,我似乎无法修复它。如果我尝试将Grand Total放入我的for循环中,它只会在对话框中打印出5次。我需要在对话框中将Grand Total打印最后一次
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); //
}
}
答案 0 :(得分:1)
您尝试将grandTotal
作为showMessageDialog
的邮件类型传递。
我怀疑你的意思是如下:
JOptionPane.showMessageDialog(
null, msg,
String.format("Grand Total $%.2f", grandTotal),
JOptionPane.INFORMATION_MESSAGE
);