2016-07-31 51 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