0

我正在做一款名为 Craps 的电脑版游戏。我在显示数组中的一些数字时遇到了一些问题,谁能帮我请教如何在下一个数组中显示所有值:

int SideBetNumber = Console.readInt("How many side bets would you like to make ? (Introduce a number, minimum 1, maximum 6.)");

int[] SBNArray = new int[SideBetNumber];
int[] sbArray = new int[SideBetNumber];

int s = 0;

for (s = 0; s <= (SideBetNumber -1) ; s++) {
    SBNArray[s] = Console.readInt("On which number would you like to put a side bet ?");
    sbArray[s] = Console.readInt("Currently you have " + money + " chips, how much would you like to bet ?");
    money = money - sbArray[s];
    System.out.println("Thank you for your " +sbArray[s]+ " chip side bet on number " +SBNArray[s]+".");
    System.out.println();
}

PointEstablisher(numberDice ,bet ,money, SBNArray, sbArray, s);

我的困难是这是我将它传递给另一个公共静态无效的方法......这是我的传递:

PointEstablisher(numberDice ,bet ,money, SBNArray, sbArray, s);

并接收:

public static void PointEstablisher(int numberDice,int bet,
    int money,int[] SBNArray,int[] sbArray, int s)

我知道,这很长,但是:如何显示所有 SBNArray 和 sbArray 值,因为索引号取决于用户引入的 SideBetNumber ...我不能只输入 SBNArray(s) 因为它给了我错误...或者我无法输入 SBNArray(3) 因为它可能存在,也可能不存在,这取决于用户的选择...

我试过了,但它没有用:

    if (SBNArray[0] != 0) {
     System.out.println("SBN 1 is " +SBNArray[0]);
    }else {
       System.out.println("NO SBN 1 !!!");
    }
    if (sbArray[0] != 0) {
     System.out.println("sb 1 is " + sbArray[0]);
    }else {
       System.out.println("NO sb 1 !!!");
    }

    if (SBNArray[1] != 0) {
     System.out.println("SBN 2 is " +SBNArray[0]);
    }else {
       System.out.println("NO SBN 2 !!!");
    }
    if (sbArray[1] != 0) {
     System.out.println("sb 2 is " + sbArray[0]);
    }else {
       System.out.println("NO sb 2 !!!");
    }

    if (SBNArray[2] != 0) {
     System.out.println("SBN 3 is " +SBNArray[0]);
    } else {
       System.out.println("NO SBN 3 !!!");
    }

    if (sbArray[2] != 0) {
     System.out.println("sb 3 is " + sbArray[0]);
    }else {
       System.out.println("NO sb 3 !!!");
    }
enter code here

但它没有成功:java.lang.ArrayIndexOutOfBoundException: ...

谁能帮我只显示数组的现有值?

4

2 回答 2

1

您应该循环遍历数组,而不是一一比较它们。

for (int i = 0; i < SBNArray.length; i++){
    if (sbArray[i] != 0) {
        System.out.println("sb " + i + " is " + sbArray[i]);
    } else {
        System.out.println("NO sb " + i + " !!!");
    }
}

并对另一个数组执行相同的操作。您甚至可以为此创建一个私有方法以防止重复自己。

于 2013-11-03T06:25:40.890 回答
0

如果 SBNArray[] 和 sbArray[] 的值是正确的,并且它们的大小是“SideBetNumber”,也许这会起作用:(我是这里的新手,这是我的第一个答案。如果不能帮助你,我只能说“对不起!”)

for (int i=0;i<SideBetNumber;i++) {
if (SBNArray[i] != 0) {
    System.out.println("SBN "+(i+1)+" is " +SBNArray[i]);
} else {
    System.out.println("NO SBN "+(i+1)+" !!!");
}
if (sbArray[i] != 0) {
    System.out.println("sb "+(i+1)+" is " + sbArray[i]);
} else {
    System.out.println("NO sb "+(i+1)+" !!!");
}
}
于 2013-11-03T06:53:48.417 回答