2015-04-15 122 views
0

即時嘗試添加一個總計到我的對話框,但我收到一條錯誤消息,我似乎無法修復它。如果我嘗試將Grand Total放在我的for循環中,它只會在對話框中打印出5次。我確實需要最後有總計打印對話框不兼容的類型可能有損從雙轉換爲int

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

您提到了一條錯誤消息:消息是什麼,它表示哪條線? – deltab

回答

1

你試圖通過grandTotalshowMessageDialog消息類型。

我懷疑你的意思是像下面這樣:

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

另見"How to Make Dialogs"

+0

我收到一個錯誤sayng沒有找到合適的方法 – Student214

+0

JOptionPane.showMessageDialog(null,msg,String.format(「Grand Total $%。2f」,grandTotal));我不認爲它是我的逗號位置? – Student214

+0

是的,對不起,請參閱我的編輯。 – Radiodef

相關問題