意外的类型;必需的:变量;发现:价值

时间:2014-10-08 19:36:11

标签: java variables types

我很清楚这个编码有错误(只在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";
        }
    }
}

1 个答案:

答案 0 :(得分:0)

更改此行。

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

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

我想你现在正在获得第二个无法访问的返回错误,因为你在这个代码块之上有一个else语句,当你在if-else if循环中的条件为false时调用。如果你的条件一旦达到它就是假的,那么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接受。