0

这是一个超出我想象的想法,但我认为这里有人可以正确地说明它。基本上,我运行了一个递增对象数组的 for 循环。我希望每个对象都保存记录的数组。

static RollDice p1,p2,p3,p4;
static RollDice[] Players = new RollDice[]{p1,p2,p3,p4};
for (int a=0;a<4;a++){

  for(int b =0;b<4;b++){
        roll = Math.random()*5;
        roll = Math.round(roll);
        roll = roll+1;
        Players[a].Numbers[b]=(int)roll;
        System.out.println("You have rolled a: "+roll);
  }
        //This prints four numbers in an array for each value of a.
        System.out.println(Arrays.toString(Players[a].Numbers));
       //This is SUPPOSED to call the numbers recorded for that object. I had it as Players[#].Numbers before, but of course that didn't work either.
        System.out.println(Arrays.toString(p2.Numbers));

所以我想我是在问我是否应该继续,或者只是不浪费我的时间。也请像我是个白痴一样拼写出来,因为我是。

4

2 回答 2

0
class RollDice
{
    public int [] Numbers = new int [4];
}

RollDice p1,p2,p3,p4;
// Do not foget to initialize p1, p2, p3, p4 here
RollDice [] Players = new RollDice [] {p1, p2, p3, p4};
Players [2].Numbers [3] = 4;
于 2013-02-27T04:22:13.353 回答
0

我认为这应该可以解决问题:-

RollDice p1 = new RollDice(), p2 = new RollDice(), p3 = new RollDice(), p4 = new RollDice();
RollDice[] players = new RollDice[] { p1, p2, p3, p4 };

for (int a = 0; a < 4; a++) {
    int roll = 0;
    for (int b = 0; b < 4; b++) {
        roll = (int) Math.round(Math.random() * 5);
        roll++;
        players[a].numbers[b] = roll;
        System.out.println("You have rolled a: " + roll);
    }
    System.out.println(Arrays.toString(players[a].numbers));
}

class RollDice{
    public int [] numbers = new int [4];
}

注意:-为了更好的命名约定,我已将变量名称从uppercase更改为 。lowercase

于 2013-02-27T04:41:06.607 回答