gpt4 book ai didi

java - 打印数组和 toString 时的输出问题

转载 作者:行者123 更新时间:2023-12-01 18:03:35 24 4
gpt4 key购买 nike

enter image description here 我有三个问题。

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 -----");

}
}

最佳答案

我相信问题出在 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
}

关于java - 打印数组和 toString 时的输出问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38682441/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com