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