我有两个文件,一个是驱动程序,我在使用 setter 时遇到问题。看起来确实设置了值。
public class Movie {
private String name;
private int minutes;
protected int tomatoScore;
public Movie(String name, int minutes, int tomatoScore)
{
this.name=name;
this.minutes=minutes;
this.tomatoScore=tomatoScore;
}
public String getName() {return name;}
public void setName(String name) {this.name=name;}
public int getMinutes() {return minutes;}
public boolean setMinutes(int minutes) {return minutes>=0;}
public int getTomatoScore() {return tomatoScore;};
public boolean setTomatoScore(int tomatoScore) {return tomatoScore>=0 &&tomatoScore<=100;};
public boolean isFresh() {return tomatoScore>=60;};
public void display()
{
//this.name = name;
//this.minutes = minutes;
//this.tomatoScore =tomatoScore;
System.out.println("Movie: "+ getName());
System.out.println("Length: "+ getMinutes() +"min.");
if(tomatoScore>=60)
{
System.out.println("TomatoScore: Fresh");
}
else
{
System.out.println("TomatoScore: Rotten");
}
}
}
下面是驱动程序文件,如果您注意到设置器确实完成了应该做的工作我相信问题是电影类,如果您运行驱动程序来测试程序,您会看到是否将值设置为负值 if 语句无法正常运行。( setMinutes 和 setTomatoScore 是错误的。它们根本没有设置类字段)
弑天下
德玛西亚99
慕桂英3389331
相关分类