我正在做一款名为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,因为它给了我错误......或者我可以不输入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:...
任何人都可以帮我显示数组的现有值吗?
答案 0 :(得分: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 + " !!!");
}
}
对另一个阵列做同样的事情。你甚至可以为此做一个私人方法,以防止重复自己。
答案 1 :(得分: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)+" !!!");
}
}