2015-02-07 57 views
-1
public static void main(String[] args) { 
    // create a book named "Java is Fun" with 200 in stock costing 55.00 each. The stock number is 1234 
    Book javafun = new Book("Java is Fun", 200, 55.00, 1234); 
    //print out the book using the toString() method 
    System.out.println (javafun.toString()); 

    //create a book calling the empty constructor. 
    Book newbook2 = new Book(); 
    //set the title to "Databases R Us" 
    String title = "Databases R Us"; 
    //set the number in stock to 50 
    int numInStock = 50; 
    // set the cost each to $35.00 
    double cost = 35.00; <------ this 
    // set the stock number to 5555 
    int stockNum = 5555; 
    //print out the book. 
    System.out.println(newbook2.toString()); 
    //change the price to $55.00 
    double cost = 55.00; <-- and this gives me duplicate error 
    //print out the book 
    System.out.println (newbook2.toString()); 

} 

我該如何做空間下面的步驟?java get missing field

我有點做了我認爲應該但它給了我我有重複。並且輸出不出來正確的

輸出應該是:

Java是有趣的ID號1234在股票200本書的成本每$ 55,00。

ID爲5555的數據庫R Us有50本書,每本價值35美元。

ID爲5555的數據庫R Us有50本書,每本55美元。

+0

變化的價格將只需要'成本= 55.00;'(不包括前面的'double')。對於其他「輸出不正確」,「書」的代碼是必需的。預期產出是多少? – tofi9 2015-02-07 06:56:30

+0

它會幫助我們幫助你,如果你可以提供我們班書。 :D謝謝... – 2015-02-07 07:30:38

+0

[在我的主應用程序中調用setter方法]的可能的重複(http://stackoverflow.com/questions/4603353/calling-setter-methods-in-my-main-application) – Joe 2015-02-07 09:51:48

回答

1

您有一個重複的局部變量。 double cost = 55.00;將導致語法錯誤。

通過程序中的註釋來判斷,它看起來像只是想要更改值,不要在數據類型前面加上變量名稱。

cost = 55.00;

如果你的意思是要改變與特定Book對象相關的成本,作爲另一個用戶建議..只是去book1.setCost/Title/Whatever(...)我假設在你的書類,你有setter方法和你變量不公開。

+0

如果我做成本= 55.00;它給了我錯誤不能解決變量 – adrianhmartinez 2015-02-07 06:58:36

+0

@adrianhmartinez,這是因爲你清除名稱爲「成本」的任何變量。請看我的答案,看看你如何改變班級成員的價值觀。 – 2015-02-07 07:05:15

+0

您只需更改同一範圍內的局部變量的值。如果你還沒有改變任何其他的東西,那麼沒有理由讓你發現這個錯誤。你的意思是要像這樣:newbook2.setCost(...)?使用我設想的設置方法在您的Book類中設置對象值 – 121c 2015-02-07 07:05:52

1

除了重複的變量創建,你需要在這裏將成員cost的值更改爲55或50,這將是這樣的。

// change the value of the cost member 
newBook2.cost = 55; 

現在,一旦你輸出這個(以字符串符號),你將在書中得到成本。到目前爲止,您只是初始化新變量,而不是更改Book對象成員的值。比方說,你得到一本書,你想改變這些對象的屬性,你可以這樣做類似下面的代碼,

// create the object 
Book myBook = new Book(); 

// now we will change the values for the members 
myBook.title = "Databases R Us"; 
myBook.numInStock = 50; 
myBook.cost = 50; 
myBook.stockNum = 5555; 

// Above were the settings for the Book object 
System.out.print(myBook.toString()); 

上面的代碼就會有你的書對象指定的屬性而不是變量的值。欲瞭解更多關於類成員,請通過這個Java開發人員的鏈接:http://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html

+0

。title =「數據庫R Us」;它說要改變標題的可見性從私人到包 – adrianhmartinez 2015-02-07 07:11:06

+0

@adrianhmartinez,我很抱歉,但我不明白你想說什麼。你能否詳細說明你的意圖?什麼是「項目」在這裏?類不能有包含在兩個qoutes中的成員。 – 2015-02-07 07:11:59

+0

哦,我現在知道了它是myBook.settitle =「數據庫R Us」; – adrianhmartinez 2015-02-07 07:21:35

0
public class BookDriver { 

public static void main(String[] args) { 
    // create a book named "Java is Fun" with 200 in stock costing 55.00 each. The stock number is 1234 
    Book javafun = new Book("Java is Fun", 200, 55.00, 1234); 
    //print out the book using the toString() method 
    System.out.println (javafun.toString()); 
    //create a book calling the empty constructor. 
    Book newbook2 = new Book(); 

    //set the title to "Databases R Us" 
    newbook2.setTitle("Databases R Us"); 
    //set the number in stock to 50 
    newbook2.setNumInStock(50); 
    // set the cost each to $35.00 
    newbook2.setCost(35.00); 
    // set the stock number to 5555 
    newbook2.setStockNum(5555); 
    //print out the book. 
    System.out.println(newbook2.toString()); 
    //change the price to $55.00 
    newbook2.setCost(55.00); 
    //print out the book 
    System.out.println (newbook2.toString());