我正在研究一個電子書程序,該程序會將Ebooks添加到庫中,然後顯示各種信息。還有一些我仍在使用的程序部分,比如創建一個有效的語句來驗證ISBN,但是我稍後再保存它。現在我只是試圖創建一個Ebook對象,並將其添加到我的電子書數組中。但是,當我嘗試在EbookLibraryTest中調用addEbook時,我得到了ebook1.addEbook(...)行的「無法找到符號」。我很困惑,因爲其他兩個類都編譯了。我是否正確調用該方法?如果是這樣,還有什麼其他問題導致此錯誤?可能的範圍問題,多個類
public class Ebook
{
private String author = "";
private String title = "";
private double price = 0;
private int isbn = 0;
public Ebook(String author, String title, double price, int isbn)
{
this.author = author;
this.title = title;
this.price = price;
if (isbn > 0)
this.isbn = isbn;
else
isbn = 0;
}
public void setPrice(double price)
{
if (price < 0)
{
System.out.println("Invalid price");
}
else
this.price = price;
}
public double getPrice()
{
if (price < 0)
{
System.out.println("Invalid price");
price = 0.0;
return price;
}
else
this.price = price;
return price;
}
public void setAuthor(String theAuthor)
{
this.author = theAuthor;
}
public String getAuthor()
{
return author;
}
public void setIsbn(int isbn)
{
if (isbn > 0)
{
this.isbn = isbn;
}
else
isbn = 0;
}
public int getIsbn()
{
if (isbn > 0)
{
this.isbn = isbn;
return isbn;
}
else
System.out.println("Invalid isbn");
isbn = 0;
return isbn;
}
public void setTitle(String title)
{
this.title = title;
}
public String getTitle()
{
return title;
}
public String toString()
{
return String.format("The author is %s, the title is %s, the price is %f, the isbn is%d,",
author,title,price,isbn);
}
}
public class EbookLibrary
{
private int count = 0;
private double total_cost = 0.0;
Ebook[] ebooks = new Ebook[25];
public EbookLibrary()
{
}
public int getCount()
{
return count;
}
public double getCost()
{
return total_cost;
}
public String toString()
{
return String.format("The count is %d, the total cost is %f,", count, total_cost);
}
public void addEbook(String theAuthor, String aTitle, double thePrice, int theIsbn)
{
Ebook anEbook = new Ebook("blah", "thing", 1.0, 1);
for (int counter = 0; counter < ebooks.length; counter++)
{
ebooks[counter] = anEbook;
count++;
price += total_cost;
}
}
}
public class EbookLibraryTest
{
public static void main(String[] args)
{
Ebook ebook1 = new Ebook("Tom Sawyer", "The Title", 77.0, 33);
Ebook ebook2 = new Ebook("Thing Do", "What What", 45.0, 15);
Ebook ebook3 = new Ebook("Stephen King","The Thing",1.1, 7);
Ebook ebook4 = new Ebook("Robert","A Title", 1.0, 1);
Ebook ebook5 = new Ebook("Tom","Bad Title", 33.1, 17);
Ebook ebook6 = new Ebook("Bob", "lol", 25.0, 15);
ebook1.addEbook("Tom Sawyer", "The Title", 77.0, 33);
}
}
您添加一個電子書的圖書館,而不是其他的電子書。 .. –
所有這些類在上面顯示的一個文件中?請格式化您的代碼。 –
是的,他們是。抱歉。什麼是多個類的正確格式? – srmjr