2014-10-08 29 views
-6

我很清楚,這種編碼有錯誤(只在java 4周),但我不知道如何解決它。意外類型;要求:變量;發現:值

EDIT2:與現在的代碼的唯一錯誤(它告訴我至少)是所需的類型是可變的,它發現在以下行的值: if(yearPublished = 0 & monthPublished = 0)

/** 
* This class describes a book. 
* @author Tess Robertson 
* @version 10/06/2014 
*/ 
class Book 
{ 
    /** 
    * Instance variables 
    */ 
    private String title; 
    private int bookNumber; 
    private String lastName; 
    private String firstName; 
    private int yearPublished; 
    private int monthPublished; 
    private String monthName; 
    /** 
    * Default constructor 
    */ 
    public Book() 
    { 
    } 
    /** 
    * Another constructor 
    * @param initialTitle   - the book's title 
    * @param initialBookNumber  - the book's ISBN 
    * @param initialLastName  - the author's last name 
    * @param initialFirstName  - the author's first name 
    * @param initialYearPublished - the book's publication year 
    * @param initialMonthPublished - the book's publication month number 
    * @param initialMonthName  - the book's publicaton month name 
    */ 
    public Book(String initialTitle, int initialBookNumber, String initialLastName, String initialFirstName, int initialYearPublished, int initialMonthPublished, String initialMonthName) 

    { 
     title   = initialTitle; 
     bookNumber  = initialBookNumber; 
     lastName  = initialLastName; 
     firstName  = initialFirstName; 
     yearPublished = initialYearPublished; 
     monthPublished = initialMonthPublished; 
     monthName  = initialMonthName; 
    } 
    /** 
    * @return the book's title 
    */ 
    public String getTitle() 
    { 
     return title; 
    } 
    /** 
    * @return the book's ISBN 
    */ 
    public int getBookNumber() 
    { 
     return bookNumber; 
    } 
    /** 
    * @return the author's last name 
    */ 
    public String getLastName() 
    { 
     return lastName; 
    } 
    /** 
    * @return the author's first name 
    */ 
    public String getFirstName() 
    { 
     return firstName; 
    } 
    /** 
    * @return the book's publication year 
    */ 
    public int getYearPublished() 
    { 
     return yearPublished; 
    } 
    /** 
    * @return the book's publication month 
    */ 
    public int getMonthPublished() 
    { 
     return monthPublished; 
    } 
    /** 
    * @return the book's publication month name 
    */ 
    public String getMonthName() 
    { 
     return monthName; 
    } 
    /** 
    * @return the author's full name 
    */ 
    public String getFullName() 
    { 
     return firstName+lastName; 
    } 
    /** 
    * Prints the title, ISBN, Author full name, and publication year and date 
    */ 
    public String printDetails() 
    { 
     if(title!=null||title.length()>3) 
     { 
      return ("Title: "+title); 
     } 
     else if(title==null||title.length()<=3) 
     { 
      return ("Title: "+"invalid text"); 
     } 
     if(bookNumber>=10000&&bookNumber<=20000) 
     { 
      return ("ISBN: "+bookNumber); 
     } 
     else if(bookNumber==0) 
     { 
      return ("ISBN: "+"invalid number"); 
     } 
     if(lastName==null||firstName==null) 
     { 
      return ("Author: "+"invalid text");         
     } 
     else 
     { 
      return ("Author: "+firstName+" "+lastName); 
     } 
     if(yearPublished = 0 & monthPublished = 0) 
     { 
      return ("Published: "+"invalid number"); 

     } 
     else 
     { 
      return ("Published: "+monthName+" "+yearPublished); 
     } 
    } 
    /** 
    * Recieve a book title 
    * @param newTitle - the title entered by the user 
    */ 
    public void setTitle(String newTitle) 
    { 
     if(newTitle.length()>3) 
     { 
      newTitle=title; 
     } 
     else 
     { 
      System.out.println("Book title must have more than 3 characters."); 
     } 
    } 
    /** 
    * Recieve an ISBN 
    * @param newBookNumber - the ISBN entered by the user 
    */ 
    public void setBookNumber(int newBookNumber) 
    { 
     if(newBookNumber>=10000&&newBookNumber<=20000) 
     { 
      newBookNumber=bookNumber; 
     } 

     else 
     { 
      System.out.println("ISBN must be a number between 10000 and 20000 inclusive."); 
     } 
    } 
    /** 
    * Recieve author's last name 
    * @param newLastName - the last name entered by the user 
    */ 
    public void setLastName(String newLastName) 
    { 
     if(newLastName != "null") 
     { 
      lastName=newLastName; 
     } 
     else 
     { 
      System.out.println("Author's last name cannot be blank."); 
     } 
    } 
    /** 
    * Recieve author's first name 
    * @param newFirstName - the first name entered by the user 
    */ 
    public void setFirstName(String newFirstName) 
    { 
     if(newFirstName != null) 
     { 
      newFirstName=firstName; 
     } 
     else 
     { 
      System.out.println("Author's first name cannot be blank."); 
     } 
    } 
    /** 
    * Recieve a publication year 
    * @param newYearPublished - the year of publication entered by the user 
    */  
    public void setYearPublished(int newYearPublished) 
    { 
     if(newYearPublished<=2013&&newYearPublished>=1870) 
     { 
      newYearPublished=yearPublished; 
     } 
     else 
     { 
      System.out.println("Year published must be between 1870 and 2013 inclusive."); 
     } 
    } 
    /** 
    * Recieve a publication month 
    * @param newMonthPublished - the month of publication entered by the user 
    */ 
    public void setMonthPublished(int newMonthPublished) 
    { 
     if(newMonthPublished>=1&&newMonthPublished<=12) 
     { 
      newMonthPublished=monthPublished; 
     } 
     else 
     { 
      System.out.println("Month published must be between 1 and 12 inclusive."); 
     } 
    } 
    /** 
    * Set monthName given monthPublished 
    */ 
    public void setMonthName(String monthName) 
    { 
     if(monthPublished==1) 
     { 
      monthName="January"; 
     } 
     else if(monthPublished==2) 
     { 
      monthName="February"; 
     } 
     else if(monthPublished==3) 
     { 
      monthName="March"; 
     } 
     else if(monthPublished==4) 
     { 
      monthName="April"; 
     } 
     else if(monthPublished==5) 
     { 
      monthName="May"; 
     } 
     else if(monthPublished==6) 
     { 
      monthName="June"; 
     } 
     else if(monthPublished==7) 
     { 
      monthName="July"; 
     } 
     else if(monthPublished==8) 
     { 
      monthName="August"; 
     } 
     else if(monthPublished==9) 
     { 
      monthName="September"; 
     } 
     else if(monthPublished==10) 
     { 
      monthName="October"; 
     } 
     else if(monthPublished==11) 
     { 
      monthName="November"; 
     } 
     else if(monthPublished==12) 
     { 
      monthName="December"; 
     } 
    } 
} 
+3

你能不能更清楚一點,你想要什麼?什麼是錯誤?我們無法修復此代碼的完整語法錯誤。 – 2014-10-08 19:38:13

+3

告訴我們*出現了哪些錯誤,*它們是哪裏,以及*刪除了與該問題無關的代碼。這實際上毫不費力地縮小問題範圍併發佈一個連貫的問題。 – tnw 2014-10-08 19:38:42

+0

另外,幾乎在每一個你的方法中,你都將你的參數與你的類屬性混合在一起。對於前者,setBookNumber將*參數*設置爲*類屬性*,否則應該是相反的。 – tnw 2014-10-08 19:43:15

回答

0

更改此線。

if(yearPublished=0&monthPublished=0){ 
    return ("Published: "+"invalid number"); 

if(yearPublished == 0 && monthPublished == 0){ 
    return ("Published: "+"invalid number"); 

我想現在你得到你的第二個,不可達的回報,錯誤是由於你有這個代碼塊,被稱爲在你的,如果當你的條件之上的else語句 - 如果循環是假的。如果你的條件一旦到達它,那麼else部分將一直執行,從而導致該塊無法訪問。

這是一個簡單的方法來返回所需的字符串。只需聲明要返回的字符串,然後繼續添加並最終在評估完所有條件後返回。

public String printDetails() 
{ 
    String returnString = ""; //declaring a string to build upon to return once finished all conditions. 

    if(title !=null) 
    { 
     returnString += "Title: " + title + " "; 
    } 
    else if(title == null || title.length() <= 3) 
    { 
     returnString += "Title: " + "invalid text "; 
    } 

    if(bookNumber >= 10000 && bookNumber <= 20000) 
    { 
     returnString += "ISBN: " + bookNumber + " "; 
    } 
    else if(bookNumber == 0) 
    { 
     returnString += "ISBN: " + "invalid number "; 
    } 

    if(lastName == null || firstName == null) 
    { 
     returnString += "Author: " + "invalid text ";         
    } 
    else if(firstName != null || lastName != null) // changed to not= null. 
    { 
     returnString += "Author: " + firstName + " " + lastName + " "; 
    } 

    if(yearPublished == 0 && monthPublished == 0) 
    { 
     returnString += "Published: " + "invalid number "; 

    } 
    else if(yearPublished != 0 && monthPublished != 0) 
    { 
     returnString += "Published: " + monthName + " " + yearPublished; 
    } 

    return returnString; // now after the string has been built, we will return it. 
} 

最後:

setMonthName()方法是完美的switch statement,給出的例子是實際上正是你所想要做什麼。您可能還想接受monthPublished作爲參數而不是monthName。

+0

如果我這樣做,它告訴我,第一個類型是布爾值,第二個類型是int。它們都是int數據類型。 – mikotochan 2014-10-08 19:39:58

+1

monthPublished = 0必須是monthPublished == 0如果你正在檢查零 – 2014-10-08 19:40:38

+0

嘗試修改後的代碼..sorry,正在衝。我沒有注意到其他錯誤。 – MarGar 2014-10-08 19:41:18

相關問題