2016-07-31 10 views
0

enter image description here Ich habe drei Probleme.Ausgabeprobleme beim Drucken eines Arrays und toString

1) Ich habe eine Reihe von Ebooks, die ich versuche zu drucken. Das Array ist 25 Elemente lang und ich habe 6 Ebooks darin platziert. Wenn ich drucke, druckt es jedes E-Book 25 Mal statt nur einmal.

2) Ich habe Ausgabe am Ende meines Programms, das eine große Dezimalzahl vor meiner gewünschten Druck Anweisung druckt

3) Wie kann ich JOptionPane oder eine andere Klasse implementieren, die gesamte Ausgabe auf einem drucken Dialogbox?

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

Ich habe versucht, dies in die drei separaten Klassen-Dateien, in denen es ist, zu setzen, aber ich bin mir nicht sicher, wie. Es gibt eine Ebook-Klasse, EbookLibrary-Klasse, EbookLibraryTest-Klasse. – srmjr

+0

Was ist ausgedruckt? 'aLibrary.toString()' sollte nur count und total_cost ausgeben. – c0der

+0

Ich hatte einen Screenshot meiner Ausgabe hinzugefügt, aber ich denke, dass er gelöscht wurde. Es ist jetzt auf. – srmjr

Antwort

2

Ich glaube, das Problem mit Ihrer addEbook Methode in Ihrer EbookLibrary Klasse. Jedes Mal, wenn Sie eine neue Ebook hinzufügen, füllen Sie das gesamte ebooks Array damit. Bei jeder Iteration erhöhen Sie auch die Werte total_cost und count. Ich gehe davon aus, dass Sie es nur einmal zum Array hinzufügen möchten und verifizieren Sie, dass das Array vorher nicht voll ist. Versuche dies.

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

Danke für die Hilfe. Können Sie erklären, warum dieser Code NULL und nicht die Buchwerte ausgibt? – srmjr

+2

@srmjr Ja, das wäre, weil ich die "Anzahl" vor dem Drucken inkrementiert habe. Das war ein weiterer Fehler, den ich vergessen habe zu beheben. Ich habe meine Antwort aktualisiert. – kamoroso94

+0

Danke. Sehr hilfreich. Ich habe ehrlich gesagt nicht einmal die Reihenfolge des Inkrements und seine Auswirkung auf den Druck berücksichtigt. Ich lerne immernoch. Vielen Dank. – srmjr

2

Die Dezimalzahl ist auf dieser Linie: System.out.printf ("% d% f% s% n", aLibrary.getCount(), aLibrary.getCost(), aLibrary.toString ());

Die %d zeigt eine Ganzzahl an, aber die %f zeigt eine Gleitkommazahl an; entfernen Sie es und dieser Teil wird funktionieren.

Ihre addEbook Funktion ist auch falsch; Es füllt das gesamte Array mit einem einzigen Ebook und zeigt es jedes Mal an (was dazu führt, dass es 25 Mal angezeigt wird).

+0

Ah, ich bin mir nicht sicher, warum ich dachte, dass ich alles in einer Druckanweisung aufrufen müsste. Ich habe getCount und getCost aufgerufen und dann separat toString aufgerufen. Es funktionierte. Vielen Dank. Was den zweiten Teil anbelangt, habe ich das Problem behoben, dass immer wieder gedruckt wird, aber jetzt wird "Null" für die Buchwerte gedruckt. 'Ebook anEbook = neues Ebook (Autor, Titel, Preis, isbn); \t \t \t \t ebooks [Anzahl] = anEbook; \t \t total_kosten + = preis; \t \t zählen ++; \t \t System.out.printf ("% s% n", E-Books [Anzahl]); ' – srmjr

+0

Können Sie eine aktualisierte Version Ihres Codes bereitstellen? Ich empfehle, [Pastebin] (http://pastebin.com/) zu verwenden, um es zu verlinken (kostenlos und keine Registrierung erforderlich). – pie3636

+0

'Ebook anEbook = neues Ebook ("blah", "blah", 1.0, "123"); \t \t \t \t ebooks [Anzahl] = anEbook; \t \t total_kosten + = preis; \t \t zählen ++; \t \t System.out.printf ("% s% n", E-Books [Anzahl]); 'So dass Null 6 Mal gedruckt und dann die richtige Anzahl und Preis. Entschuldigung für das Code-Format. Ich habe es früher mit Pastebin versucht und mir wurde gesagt, ich solle es nicht tun. – srmjr