这是我的代码:
import java.util.Arrays;
public class BookClasss {
private String name;
public AuthorClass[] author ;
private double price ;
private int qty =0;
public BookClasss(String names, AuthorClass[] authors, double price2,
int qtyes)
{
this.name = names;
this.author = authors;
this.price = price2;
this.qty = qtyes;
}
public void SetBookDetails (String names, AuthorClass[] authors,double price2 , int qtyes)
{
this.name = names;
this.author = authors;
this.price = price2;
this.qty = qtyes;
}
public String Getbookname()
{
return name;
}
public double getprice()
{
return price;
}
public void setprice (double prices)
{
price = prices;
}
public void setQty (int qtnties)
{
qty = qtnties;
}
public int getQty ()
{
return qty;
}
public void GetAll ()
{
for (int i=0; i<author.length;i++)
{
System.out.println("Book name : "+name+", Authors : "+Arrays.asList(author[i].ToAll())+" , Price : "+price+"");
}
}
}
这是主要方法:
public class TestBook
{
public static void main(String[] args)
{
AuthorClass[] authors = new AuthorClass[2];
authors[0] = new AuthorClass( "John" , "John@yahoo.com" , 'M');
authors[1] = new AuthorClass( "Sam" , "Sam@yahoo.com" , 'M');
BookClasss book = new BookClasss("Java OOP",authors, 200.5, 20);
book.GetAll();
}
}
输出为:
Book name : Java OOP, Authors : [Author Name=John , Mail = John@yahoo.com] , Price : 120.2
Book name : Java OOP, Authors : [Author Name=Sam , Mail = Sam@yahoo.com] , Price : 120.2
我使用forloop获取所有作者数组结果,但我想打印结果如:
Book name : Java OOP, Authors : [Author Name=John , Mail = John@yahoo.com] , [Author Name=Sam , Mail = Sam@yahoo.com] , Price : 120.2
在一个结果中没有重复书名和价格..我怎么能这样做? 谁能帮帮我???
答案 0 :(得分:0)
public void GetAll ()
{
System.out.println("Book name : "+name+", Authors : ");
for (int i=0; i<author.length;i++)
{
System.out.print(Arrays.asList(author[i].ToAll())+" ,");
}
System.out.println("Price : "+price+"");
}