我希望程序为循环的每次迭代不断增加局。 当我运行该程序时,它正在这样做,但它显示的值不正确。Java:while循环和数组递增
例如: 你滚... 4 你总该安监局至今6
第二行应该显示,” ......至今4"
这是我现在的代码如下:
import java.util.Random;
import javax.swing.*;
public class shortSix {
public static void main(String[] args) {
diceGame();
}//ENDS MAIN
public static void diceGame()
{
final int[] innings = new int[1];
innings[0] = 0;
Random dice = new Random();
int diceRoll = dice.nextInt(6) + 1;
while (diceRoll != 5)
{
System.out.println("You rolled..." + diceRoll);
diceRoll = dice.nextInt(6) + 1;
innings[0] =+ diceRoll;
System.out.println("Your total for this innings so far is " + innings[0]);
String userDeclare = JOptionPane.showInputDialog(null, "Do you wish to declare?");
if (userDeclare.equals("yes"))
{
System.exit(0);
}
}
}//ENDS diceGame
}//ENDS class shortSix
为什么'局'被定义为'final'? –
我应该使用最后一个变量来存储这个问题的值 – AbbenGabben
'innings [0] = + diceRoll'中'= +'的用途是什么? – Pshemo