2016-07-31 49 views
0

enter image description here我有三個問題。打印陣列時出現輸出問題toString

1)我有一組電子書,我試圖打印。該陣列有25個元素,我已經放置了6個電子書。當我打印時,它會打印每個電子書25次,而不是一次。

2)我有我的計劃是我想要的打印語句

3)如何實現的JOptionPane,或者一些其他的課前打印大量的十進制數年底輸出,打印所有輸出到一個對話框?

import javax.swing.JOptionPane; // dialog box 

public class Ebook 
{ 
    private String author = ""; 
    private String title = ""; 
    private double price = 0.0; 
    private String isbn = ""; 


     public Ebook(String author, String title, double price, String isbn) // ebook constructor 
     { 
      this.author = author; 
      this.title = title; 

      if (price > 0) // validate non-negative price 
       this.price = price; 

      else 
      { 
       this.price = 0.0; 
        System.out.println("Invalid price"); 
      } 

      if (isbn.length() == 10 || isbn.length() == 13) // isbn length must be exactly 10 or 13 
       this.isbn = isbn; 

      else 
       this.isbn = "None"; 
     } 

     public void setPrice(double price) 
     { 
      if (price < 0) // vallidate 
      { 
       System.out.println("Invalid price"); 
      } 

      else 
       this.price = price; 
     } 

     public double getPrice() 
     { 
      return price; 
     } 

     public void setAuthor(String theAuthor) 
     { 
      this.author = theAuthor; 
     } 

     public String getAuthor() 
     { 
      return author; 
     } 

     public void setIsbn(String isbn) 
     { 
      if (isbn.length() == 10 || isbn.length() == 13) // validate 
      { 
       this.isbn = isbn; 
      } 
      else 
       isbn = "None"; 
     } 

     public String getIsbn() 
     { 
      return isbn; 
     } 

     public void setTitle(String title) 
     { 
      this.title = title; 
     } 

     public String getTitle() 
     { 
      return title; 
     } 

     public String toString() 
     { 
      return String.format("Author: %s%nTitle: %s%nPrice: $%.1f%nISBN: %s%n", 
       author,title,price,isbn); 
     } 
} // This was made by ------ 

import javax.swing.JOptionPane; // dialog box 

public class EbookLibrary 
{ 
    private int count = 0; 
    private double total_cost = 0.0; 


    Ebook[] ebooks = new Ebook[25]; // array of ebook objects 

    public EbookLibrary() // no argument constructor for ebooklibrary object in library test 
    { 

    } 
    public int getCount() // total number of ebooks 
    { 
     return count; 
    } 
    public double getCost() // sum of all ebooks 
    { 
     return total_cost; 
    } 
    public String toString() // formatted string with the number and cost of all ebooks 
    { 
     return String.format("Ebook count: %d%nTotal Cost: $%.1f", count, total_cost); 
    } 
    public void addEbook(String author, String title, double price, String isbn) // adds ebooks to the array 
    { 
     Ebook anEbook = new Ebook(author,title,price,isbn); // not sure if this is a "constructor", but I think it is 

     for (int counter = 0; counter < ebooks.length; counter++) // for the length of the array, add ebook 
     { 
      ebooks[counter] = anEbook; // for each counter, add the ebook 
       total_cost += price; 
        count++; // used to find the total number of ebooks 
         System.out.printf("%s%n", ebooks[counter]); 

     } 


    } 



} // This was made by ----- 

import javax.swing.JOptionPane; // dialog box 

public class EbookLibraryTest 
{ 
    public static void main(String[] args) 
    { 

     EbookLibrary aLibrary = new EbookLibrary(); // EbookLibrary object for calling addEbook 

     //ebook objects, more can be added to test set, get methods 
     aLibrary.addEbook("Blah", "What", 88.8, "1234567891"); 
     aLibrary.addEbook("Thing Do", "What What", 45.0, "1234567891111"); 
     aLibrary.addEbook("Stephen King","The Thing",1.1, "1234567891"); 
     aLibrary.addEbook("Robert","A Title", -1.0, "1234567891"); // test invalid price, should return 0.0 and "invalid price" 
     aLibrary.addEbook("Tom","Bad Title", 33.1, "1234567891111"); 
     aLibrary.addEbook("Bob", "FML and Other Acronyms", 25.0, "1"); // test ISBN value, should return "None" 




     System.out.printf("%d%f%s%n", aLibrary.getCount(), // call methods, print with toString 
      aLibrary.getCost(), aLibrary.toString()); 

     System.out.println("Programmed by -----"); 

    } 
} 
+0

我試圖把它放到它所在的三個單獨的類文件中,但我不確定如何。有一個Ebook類,EbookLibrary類,EbookLibraryTest類。 – srmjr

+0

什麼打印出來? 'aLibrary.toString()'應該僅輸出count和total_cost。 – c0der

+0

我添加了我的輸出的截圖,但我認爲它已被刪除。現在已經開始。 – srmjr

回答

2

我相信這個問題是在你的EbookLibrary類的addEbook方法。每次添加新的Ebook時,都會用它填充整個ebooks陣列。在每次迭代中,您還將增加total_costcount。我假設你只想將它添加一次到數組中,並在這樣做之前確認數組未滿。嘗試這個。

public void addEbook(String author, String title, double price, String isbn) // adds ebooks to the array 
{ 
    if(count==ebooks.length-1) { 
     return; 
    } 
    Ebook anEbook = new Ebook(author,title,price,isbn); 

    ebooks[count] = anEbook; 
    total_cost += price; 
    System.out.printf("%s%n", anEbook); 
    count++; // used to find the total number of ebooks 
} 
+0

謝謝你的幫助。你能解釋爲什麼這段代碼打印空值而不是書本值? – srmjr

+2

@srmjr是的,這是因爲我在打印前增加了「count」。那是我忘了修復的另一個錯誤。我已經更新了我的答案。 – kamoroso94

+0

謝謝。很有幫助。我真的沒有考慮增量的順序及其對印刷的影響。仍在學習。謝謝。 – srmjr

2

的十進制數是由於這一行: System.out.printf( 「%d%F%S%N」,aLibrary.getCount(),aLibrary.getCost(),aLibrary.toString( ));

%d顯示一個整數,但%f顯示一個浮點數;刪除它,這部分將工作。

你的addEbook函數也是錯誤的;它使用單個電子書填充整個陣列,並每次顯示(導致顯示25次)。

+0

啊,我不知道爲什麼我認爲我需要在一個打印聲明中調用所有內容。我調用getCount和getCost,然後分別調用toString。有效。謝謝。至於第二部分,我已經解決了這個問題,一遍又一遍地打印,但現在它打印的是「空值」的帳面價值。 '電子書anEbook =新的電子書(作者,標題,價格,isbn); \t \t \t \t ebooks [count] = anEbook; \t \t total_cost + = price; \t \t count ++; \t \t System.out.printf( 「%s%N」,電子書[計數]);' – srmjr

+0

你能提供你的代碼的更新版本?我建議使用[Pastebin](http://pastebin.com/)將其鏈接(免費且無需註冊)。 – pie3636

+0

'電子書anEbook =新電子書(「blah」,「blah」,1。0 「123」); \t \t \t \t ebooks [count] = anEbook; \t \t total_cost + = price; \t \t count ++; \t \t System.out.printf(「%s%n」,ebooks [count]);'這樣可以輸出6次空白,然後輸出正確的計數和價格。對於代碼格式抱歉。我嘗試過使用pastebin,並被告知不要。 – srmjr