【发布时间】:2014-02-22 23:56:39
【问题描述】:
我对编程很陌生,不太了解。我一直在尝试构建一个简单的游戏,用户和计算机通过掷骰子来获得积分。我的方法贴在下面。电脑每回合只能获得20分。
我的问题是,在调用并完成方法后,我需要记住变量 computerTotal 的值。我想确保无论何时完成 computerTurn 方法,我都可以在该方法之外使用计算出的变量 computerTotal。
我尝试在 .java 文件类中建立一个新的 int(但在方法之外),然后在方法中使用该 int 来保存该值,但是我收到关于整数需要为静态的错误?
这一切都让我很困惑。谁能帮帮我?
public static void computerTurn() {
System.out.println("Passed to Computer.");
Die computerDie1, computerDie2;
int computerRound, computerTotal;
computerRound = 0;
computerTotal = 0;
while (computerTotal < 21){
computerDie1 = new Die();
computerDie2 = new Die();
computerDie1.roll();
computerDie2.roll();
System.out.println("\n" + "CPU Die One: " + computerDie1 + ", CPU Die Two: " + computerDie2 + "\n");
computerRound = computerDie1.getFaceValue() + computerDie2.getFaceValue();
int cpuDie1Value;
int cpuDie2Value;
cpuDie1Value = computerDie1.getFaceValue();
cpuDie2Value = computerDie2.getFaceValue();
System.out.println ("Points rolled this round for the Computer: " + computerRound);
computerTotal = computerTotal + computerRound;
System.out.println ("Total points for the Computer: " + computerTotal + "\n");
}
【问题讨论】: