【问题标题】:incompatible types possible lossy conversion from double to int不兼容的类型可能有损从 double 到 int 的转换
【发布时间】:2015-04-15 22:23:09
【问题描述】:

我试图将总计添加到我的对话框中,但我收到一条错误消息,我似乎无法修复它。如果我尝试将 Grand Total 放入我的 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); //

    }

}

【问题讨论】:

  • 您提到了一条错误消息:该消息是什么,它表示的是哪一行?

标签: java


【解决方案1】:

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

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

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

另见"How to Make Dialogs"

【讨论】:

  • JOptionPane.showMessageDialog(null, msg, String.format("总计 $%.2f", grandTotal));我不认为这是我的逗号位置??
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-10-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-21
  • 1970-01-01
  • 2014-12-12
相关资源
最近更新 更多