所以我有一个方法应该创建一个数组列表来保存骰子的随机掷骰子值。创建卷后,它会将它们循环添加到数组列表的元素中。添加它们的行给了我错误"int 无法引用"
然后,当我尝试在另一个循环中获取这些滚动的总和时,我得到同样的错误。下面的代码。我对 java 很陌生,我认为问题在于数据类型,arraylist 是什么,骰子、面和总和是静态整数。 谢谢!我从我的开头就包含了变量声明 类以防万一。
public class dice {
static int dice, faces, max, min, mid, x,rolls,sum;
public static void rollValues( int dice ) {
//arraylist holding roll values
ArrayList<Integer> rolls = new ArrayList<Integer>(dice);
}
public static void rollRandomizer(int dice, int faces, int sum){
//randomizing roll values
Random r = new Random();
for(x = 0; x < dice; x++){
int roll = r.nextInt(faces)+1;
rolls.add(x, roll); **<<<error here**
}
System.out.println( "The roll values are: " + rolls);
//summing roll values aka all the numbers in the rolls arraylist
for(x=0; x <dice; x++){
sum += rolls.get(x); **<<<error here**
}
System.out.println("The sum of the rolls is: " + sum);
}
ArrayList<Integer> rolls = new ArrayList<Integer>(dice); // this is method variable, it's available only inside rollValues()
而这个:
rolls.add(x, roll);
呼叫滚入
static int dice, faces, max, min, mid, x,rolls,sum;