java获取缺少字段

java获取缺少字段,java,field,Java,Field,我将如何在空间下方执行步骤 我做了我认为应该做的,但它给了我一个复制品。而且输出结果也不正确 输出应为: Java很有趣,id号为1234,有200本书,每本55美元 id号为5555的R Us数据库有50本书,每本售价35美元 id号为5555的数据库R Us有50本库存书籍,每本售价55.00美元。您有一个重复的局部变量。双倍成本=55.00;将导致语法错误 从程序中的注释判断,看起来您只是想更改值,而不是在变量名前加上数据类型 i、 e.成本=55.00 如果您打算按照另一个用户的建议更改

我将如何在空间下方执行步骤

我做了我认为应该做的,但它给了我一个复制品。而且输出结果也不正确

输出应为:

Java很有趣,id号为1234,有200本书,每本55美元

id号为5555的R Us数据库有50本书,每本售价35美元


id号为5555的数据库R Us有50本库存书籍,每本售价55.00美元。

您有一个重复的局部变量。双倍成本=55.00;将导致语法错误

从程序中的注释判断,看起来您只是想更改值,而不是在变量名前加上数据类型

i、 e.成本=55.00


如果您打算按照另一个用户的建议更改与特定图书对象关联的成本。。只需去book1.设置成本/标题/无论什么。。。我假设在Book类中您有setter方法,并且您的变量不是公共的。

除了重复的变量创建之外,这里您需要的是将成员成本的值更改为55或50,类似于这样

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

}
现在,一旦你将输出这个字符串符号,你将在书中得到成本。到目前为止,您只是初始化新变量,而不是更改Book对象成员的值。假设你得到一本书,你想改变这些对象的属性,你可以像下面的代码那样做

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

上面的代码将具有您为Book对象指定的属性,而不是变量的值。有关类成员的更多信息,请浏览Java开发者的链接:

更改价格只需要成本=55.00;前面没有替身。由于其他输出不正确,因此需要本书的代码。那么预期的结果是什么?如果你能给我们提供教材的话,这将有助于我们帮助你D谢谢…如果我做成本=55.00,可能的副本;它给了我一个无法解析为variable@adrianhmartinez,这是因为您清除了任何名为cost的变量。请看一下我的答案,看看如何更改类成员的值。您只是在更改同一范围内的局部变量的值。如果您没有更改任何其他内容,那么您就没有理由会出现该错误。你的意思是说:newbook2.设置成本。。。?使用我假设在Book类中的set方法来设置对象values@AfzaalAhmadZeeshan回答得好。嗯+1给你。如果我将其设置为myBook.title=R Us;它说将标题的可见性从私有更改为package@adrianhmartinez,对不起,我不明白你想说什么。你能详细说明你的意图吗?这里有什么东西?类不能有用双qoutes包装的成员。哦,我现在知道了,它是myBook.settitle=Databases R Us;
// 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());
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());