【发布时间】:2015-12-13 14:49:18
【问题描述】:
我正在开发一个程序,该程序允许用户在四支球队中选择主队和客队。我创建了一个通用的超类团队,定义了每个安全/射门得分/触地得分。生成一个随机数,然后基于该数字,程序通过条件 if/else 语句逐步确定操作和点。
这是在超类中:
public void possessionPoints()
{
if(points<lowNopoints){
score = noPoints;
totalScore = totalScore + score;
System.out.println("No points, plus " + score);
}
else if(points<lowSafetypoint){
score = safetyPoint;
totalScore = totalScore + score;
System.out.println("Safety, plus" + score);
}
else if(points<lowFieldgoal){
score = fieldGoal;
totalScore = totalScore + fieldGoal;
System.out.println("Field goal, plus" + score);
}
else{
score = touchDown;
totalScore = totalScore + touchDown;
System.out.println("Touchdown, plus" + score);
}
ArrayList<Integer> totalScore;
totalScore = new ArrayList<>();
totalScore.add(score);
//the sum score
int sum = totalScore.stream().mapToInt(Integer::intValue).sum();
System.out.println("Current score is: " + sum);
}
注意:以上totalScore初始化为public static int totalScore = 0;
在整个过程中,我想跟踪totalScore。我在我的超类中有这个设置,但是,当程序运行时,它会在整个游戏中累加分数,并且不会区分团队。
输出:
主队行动。 没有积分,加0 当前得分:0
客队行动。 射门得分,plus3 当前得分:3
主队行动。 射门得分,plus3 当前得分:6
客队行动。 射门得分,plus3 当前得分:9
主队行动。 安全,加 2 当前得分:11
另外,如果有帮助,这就是我在每个子类中为下面的其他团队设置的全部内容。我不会对 totalScore 做任何事情。
public class PackersSub extends GenericSuper{
public PackersSub()
{
lowNopoints = 4;
lowSafetypoint = 5;
lowFieldgoal = 7;
}
关于如何解决此问题的任何想法?我想跟踪每个团队的totalScore。谢谢!
【问题讨论】:
-
@SURESH ATTA 我已经查看了“Java 静态与实例”问题。它帮助我理解静态变量是错误的类型。但是,它并不能帮助我回答我解决此问题的具体问题。 // 我不确定我是否应该在超类中使用
totalScore并只是修改设置还是应该将其放置在其他地方?
标签: java class inheritance subclass superclass