当前位置: 首页 > 知识库问答 >
问题:

不兼容的类型可能从双精度转换为整数

裴俊智
2023-03-14

我试图在我的对话框中添加一个总计,但是我得到了一个错误信息,我似乎不能修复它。如果我试图将总计放入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); //

    }

}

共有1个答案

屈健柏
2023-03-14

您正在尝试将< code>grandTotal作为< code>showMessageDialog的消息类型进行传递。

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

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

另请参阅“如何制作对话框”。

 类似资料: