2015-02-07 7 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 missing field

나는 그것이해야한다고 생각하는 것을 만들었지 만 그것은 나에게 중복을 주었다. 출력 잘

출력해야 나오는되지 않습니다

자바 ID 번호 1234 재미있는 것은 $ 55.00 각 원가 계산 주식에 200 권의 책을 가지고있다.

ID 번호가 5555 인 데이터베이스에는 50 권의 책이 있으며, 가격은 $ 35.00입니다.

데이터베이스 R ID 번호가 5555 인 회사에는 50 권의 책이 있으며, 책은 $ 55.00입니다.

+0

변화 가격은 단지 필요로'비용 = 55.00; '(앞의'double'없이). 다른 "출력이 올바르지 않다"는 경우,'Book'의 코드가 필요합니다. 예상되는 결과는 무엇입니까? – tofi9

+0

우리가 수업 도서를 제공 할 수 있다면 u를 도울 수 있습니다. : D 감사합니다 ... –

+0

내 주요 응용 프로그램에서 [setter 메서드 호출] 가능한 복제본 (http://stackoverflow.com/questions/4603353/calling-setter-methods-in-my-main-application) – Joe

답변

1

중복 로컬 변수가 있습니다. double cost = 55.00;은 구문 오류가 발생합니다.

프로그램의 주석으로 판단하면 값을 변경하려는 것처럼 보이며 변수 이름 앞에 데이터 유형을 추가하지 마십시오. 다른 사용자가 제안한대로 .. 그냥 내가 당신의 예약 클래스에 당신이 setter 메소드를 가지고 당신이 있으리라 믿고있어 book1.setCost/Title/Whatever(...)를 이동, 특정 도서 객체와 관련된 비용을 변경하는 것을 의미하는 경우

cost = 55.00;

변수는 공개되지 않습니다.

+0

만약 그렇다면 비용 = 55.00; 그것은 오류 날 변수를 – adrianhmartinez

+0

@adrianhmartinez로 해결할 수 없다는 것입니다, 왜냐하면 당신은'cost'라는 이름의 어떤 변수도 지우지 않았기 때문입니다. 내 대답을보고 반원들의 값을 어떻게 바꿀 수 있는지보십시오. –

+0

동일한 범위에있는 로컬 변수의 값만 변경하면됩니다. 다른 것을 변경하지 않은 경우 오류가 발생하는 이유가 없습니다. newbook2.setCost (...)와 같은 것을 사용하고 싶습니까? 귀하의 Book 클래스에 있다고 가정하는 set 메소드를 사용하여 객체 값을 설정하십시오 – 121c

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

위의 코드는 책 객체에 지정한 속성을 가질 것, 다음 코드처럼 그렇게 할 수 있습니다 변수의 값은 아닙니다. 클래스 멤버에 대한 자세한 내용은이 자바 개발자의 링크를 통해 이동하시기 바랍니다 : http://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html

+0

내가 myBook으로 설정하면.title = "데이터베이스 R Us"; 그것은 제목의 가시성을 사적인 것에서 꾸러미 – adrianhmartinez

+0

@adrianhmartinez로 바꾸라고 말합니다. 미안하지만 당신이하고 싶은 말을 이해하지 못했습니다. 당신의 의도를 자세히 설명해 주시겠습니까? 여기에 "아이템"이 무엇입니까? 클래스는 이중 qout으로 싸인 멤버를 가질 수 없습니다. –

+0

아, 이제 알았습니다. myBook.settitle = "Databases R Us"; – adrianhmartinez

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