【问题标题】:unexpected type; required: variable; found: value意外的类型;必需:变量;发现:值
【发布时间】:2014-12-03 14:11:53
【问题描述】:

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

【问题讨论】:

  • 你能不能说的清楚一点,你想要什么?错误是什么?我们无法修复这段代码的整个语法错误..
  • 告诉我们什么有错误,在哪里,并删除与问题无关的代码.这实际上表明没有努力缩小问题范围并提出连贯的问题。
  • 此外,几乎在您的每一个方法中,您都将参数与类属性混合在一起。例如,setBookNumberparameter 设置为 class 属性,而应该相反。
  • 这是无法访问的,因为您有一个 else 语句将在此之前执行并返回一些内容。那应该是其他的吗?如果不是,那么如果您的 if 语句中的条件为 false,它将始终默认为 else 语句。
  • 这一行 - if(title!=null||title.length()&gt;3) 需要用 &amp;&amp; 代替 || - 否则如果 titlenull 会发生坏事。

标签: java variables types


【解决方案1】:

改变这一行。

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

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

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

【讨论】:

  • 如果我这样做,它会告诉我第一个类型是布尔值,第二个是整数。它们都是 int 数据类型。
  • monthPublished=0 必须是 monthPublished==0 如果您检查零
  • 试试修改后的代码..对不起,赶时间。我没有注意到其他错误。
  • 间距可以帮助她消除一些错误,例如:) 它还可以让你的代码更容易阅读。
  • @MarGar 你的答案是正确的。不要强调否决票。有些人只是触发快乐。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-29
  • 1970-01-01
相关资源
最近更新 更多