Java 打印数组和toString时出现输出问题
我有三个问题 1) 我有一系列的电子书要打印。这个数组有25个元素长,我在里面放了6本电子书。当我打印时,它会将每本电子书打印25次,而不是一次 2) 我在程序的末尾有一个输出,在我想要的打印语句之前打印一个大的十进制数 3) 如何实现JOptionPane或其他类,将所有输出打印到一个对话框中Java 打印数组和toString时出现输出问题,java,arrays,loops,dialog,Java,Arrays,Loops,Dialog,我有三个问题 1) 我有一系列的电子书要打印。这个数组有25个元素长,我在里面放了6本电子书。当我打印时,它会将每本电子书打印25次,而不是一次 2) 我在程序的末尾有一个输出,在我想要的打印语句之前打印一个大的十进制数 3) 如何实现JOptionPane或其他类,将所有输出打印到一个对话框中 import javax.swing.JOptionPane; // dialog box public class Ebook { private String author = "";
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 -----");
}
}
import javax.swing.JOptionPane;//对话框
公共类电子书
{
私有字符串author=“”;
私有字符串title=“”;
私人双价=0.0;
私有字符串isbn=“”;
公共电子书(字符串作者、字符串标题、双倍价格、字符串isbn)//电子书构造函数
{
this.author=作者;
this.title=标题;
if(price>0)//验证非负价格
这个价格=价格;
其他的
{
这个价格=0.0;
系统输出打印项次(“无效价格”);
}
如果(isbn.length()==10 | | isbn.length()==13)//isbn长度必须正好是10或13
这是isbn=isbn;
其他的
this.isbn=“无”;
}
公共定价(双倍价格)
{
if(price<0)//vallidate
{
系统输出打印项次(“无效价格”);
}
其他的
这个价格=价格;
}
公开双价
{
退货价格;
}
public void setAuthor(字符串author)
{
this.author=作者;
}
公共字符串getAuthor()
{
返回作者;
}
公共无效集合isbn(字符串isbn)
{
如果(isbn.length()==10 | | isbn.length()==13)//验证
{
这是isbn=isbn;
}
其他的
isbn=“无”;
}
公共字符串getIsbn()
{
返回isbn;
}
公共无效集合标题(字符串标题)
{
this.title=标题;
}
公共字符串getTitle()
{
返回标题;
}
公共字符串toString()
{
返回String.format(“作者:%s%n主题:%s%n价格:$%.1f%nISBN:%s%n”,
作者、标题、价格、isbn);
}
}//这是由------
导入javax.swing.JOptionPane;//对话框
公共类电子书图书馆
{
私有整数计数=0;
私人双倍总成本=0.0;
电子书[]电子书=新电子书[25];//电子书对象数组
public EbookLibrary()//库测试中没有EbookLibrary对象的参数构造函数
{
}
public int getCount()//电子书总数
{
返回计数;
}
public double getCost()//所有电子书的总和
{
返回总成本;
}
public String toString()//包含所有电子书数量和成本的格式化字符串
{
返回字符串.format(“电子书计数:%d%n总成本:$%.1f”,计数,总成本);
}
public void addbook(字符串作者、字符串标题、双倍价格、字符串isbn)//将电子书添加到数组中
{
Ebook anEbook=new Ebook(作者、标题、价格、isbn);//不确定这是否是“构造器”,但我认为是
对于(int counter=0;counter
我认为问题在于电子书库
类中的addEbook
方法。每次添加一本新的电子书
,您都会用它填充整个电子书
数组。在每次迭代中,您还将增加总成本
和计数
。我假设您只想将其添加到数组中一次,并在执行此操作之前验证数组是否已满。试试这个
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
}
我相信问题在于
电子书库
类中的addEbook
方法。每次添加一本新的电子书
,您都会用它填充整个电子书
数组。在每次迭代中,您还将增加总成本
和计数
。我假设您只想将其添加到数组中一次,并在执行此操作之前验证数组是否已满。试试这个
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
}
小数是由于这一行: System.out.printf(“%d%f%s%n”、aLibrary.getCount()、aLibrary.getCost()、aLibrary.toString())
%d
显示一个整数,而%f
显示一个浮点;拆下它,此零件将