我希望這種稱爲「roll」的方法在每次調用它時統計我的int「spacePoints」。它可以工作,但每次從100開始。我需要它在每次調用方法時都停止點的地方開始。循環和保持計數
int spacePoints = 100;
public void roll(){
pointsRolled = ((int) (Math.random() * 10)+ 1);
if (pointsRolled % 3 ==0){
System.out.println("You must fight!!!");
} else {
spacePoints = spacePoints- pointsRolled;
System.out.println("You have " + spacePoints + " to go.");
}
}
您尚未看到足夠的驗證碼。每次調用'roll()'時,沒有任何顯示會使它從100開始。 – John3136
你打電話的方法如何?從你的片段中,你可以將「spacePoints」作爲課程的一個領域,當它不能被3整除時,它將被「pointsRolled」遞減。除非您每次調用「roll」方法時都創建類的新實例,否則它將跟蹤該類的該字段的「spacePoints」。 – GameSalutes