我正在做一款名为 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: ...
谁能帮我只显示数组的现有值?