Java 1D数组未在for循环中打印,始终等于0
我正在制作一个21点程序。在cardDraw方法中,在返回之前的末尾有一个for循环,它应该打印每个数组值,但在运行程序时没有显示任何内容。另外,当我在主方法中打印数组时,每个值都是0,即使我正在方法中设置值。有人能解释一下为什么会发生这些事情吗?这可能是一个逻辑错误,但我在程序中的其他方法中遇到了这个问题。我们只允许使用1D数组。提前谢谢 最后一件事,你能得到这个网站的电子邮件通知吗Java 1D数组未在for循环中打印,始终等于0,java,arrays,methods,Java,Arrays,Methods,我正在制作一个21点程序。在cardDraw方法中,在返回之前的末尾有一个for循环,它应该打印每个数组值,但在运行程序时没有显示任何内容。另外,当我在主方法中打印数组时,每个值都是0,即使我正在方法中设置值。有人能解释一下为什么会发生这些事情吗?这可能是一个逻辑错误,但我在程序中的其他方法中遇到了这个问题。我们只允许使用1D数组。提前谢谢 最后一件事,你能得到这个网站的电子邮件通知吗 public class blackjack { public static int [] draw
public class blackjack {
public static int [] drawCard(int [] cardValues, int [] otherUserCardValues) {
int duplicates = 0;
//fills array with cards
for (int count = 0; count == cardValues.length; count++) {
duplicates = 0;
cardValues [count] = (int)(Math.random() * 12) + 1;
//Checks for duplicate cards in one of the hands
for (int count2 = 0; count2 == cardValues.length; count2++) {
if (cardValues [count] == cardValues [count2]) {
duplicates++;
} //end of if (cardValues [count] == cardValues [count2])
} //end of for (int count2 = 0; count != 7; count++)
//Checks for duplicate cards in the other hand
for (int count2 = 0; count2 == cardValues.length; count2++) {
if (cardValues [count] == otherUserCardValues [count2]) {
duplicates++;
} //end of if (cardValues [count] == cardValues [count2])
} //end of for (int count2 = 0; count != 7; count++)
//makes it loop again if there are duplicates
if (duplicates > 4) {
count--;
} //end of if (duplicates > 4)
} //end of for (int count = 0; count != 7; count++)
for (int count = 0; count == cardValues.length; count++) {
System.out.println(cardValues[count]);
}
return cardValues;
} //end of cardDraw
public static void main(String[] args) {
//user variables
double money = 0;
int userCardTotal = 0;
int userCardCount = 0;
int [] lowerUserCards = {0, 0, 0, 0, 0, 0};
int [] higherUserCards = {0, 0, 0, 0, 0, 0};
int [] userCardValues = {0, 0, 0, 0, 0, 0};
String [] userCardNames = {"a", "a", "a", "a", "a", "a"};
String [] userCardSuits = {"a", "a", "a", "a", "a", "a"};
//dealer variables
int dealerCardTotal = 0;
int dealerCardCount = 0;
int [] lowerDealerCards = {0, 0, 0, 0, 0, 0};
int [] higherDealerCards = {0, 0, 0, 0, 0, 0};
int [] dealerCardValues = {0, 0, 0, 0, 0, 0};
String [] dealerCardNames = {"a", "a", "a", "a", "a", "a"};
String [] dealerCardSuits = {"a", "a", "a", "a", "a", "a"};
///////////////////////////////////////////////////////////////////////
//methods
userCardValues = drawCard(userCardValues, dealerCardValues);
dealerCardValues = drawCard(dealerCardValues, userCardValues);
} //end of main
} //end of class
当您想使用for循环对数组中的每个元素执行某些操作时,应该这样编写:
for (int i = 0; i < array.length; i++) {
doSomething(array[i]);
}
for (int i = 0; i == array.length; i++) {
doSomething(array[i]);
}
所以,若数组不是空的,那个么这个循环甚至不会进行一次迭代。如果数组为空,它甚至会抛出一个异常,因为您将尝试访问数组[0]。当您想使用for循环对数组中的每个元素执行一些操作时,您应该这样编写它:
for (int i = 0; i < array.length; i++) {
doSomething(array[i]);
}
for (int i = 0; i == array.length; i++) {
doSomething(array[i]);
}
所以,若数组不是空的,那个么这个循环甚至不会进行一次迭代。如果数组为空,它甚至会抛出异常,因为您将尝试访问数组[0]。for的
循环只是一个稍微修饰的while
循环
循环:
for (initial; condition; increment) {
// do something
}
相当于:
initial;
while(condition) {
// do something
increment;
}
除了在初始状态中描述的变量仅具有循环的范围之外,还有一个轻微的例外
当你写作时:
for (int i = 0; i == array.length; i++) {
doSomething(array[i]);
}
程序首先声明一个名为i
的变量,并将其设置为0
。然后它进入一个while
循环,条件是:i==array.length
,它立即返回false
,因此循环在完成第一次迭代之前退出
循环迭代的标准语法为:
for (int i = 0; i < array.length; i++) {
doSomething(array[i]);
}
更好的是,使用Java 8功能样式/流:
Arrays.asList(array).forEach(blackjack::doSomething);
// or using streams
Arrays.stream(array).forEach(blackjack::doSomething);
for
循环只是一个稍微修饰一下的while
循环
循环:
for (initial; condition; increment) {
// do something
}
相当于:
initial;
while(condition) {
// do something
increment;
}
除了在初始状态中描述的变量仅具有循环的范围之外,还有一个轻微的例外
当你写作时:
for (int i = 0; i == array.length; i++) {
doSomething(array[i]);
}
程序首先声明一个名为i
的变量,并将其设置为0
。然后它进入一个while
循环,条件是:i==array.length
,它立即返回false
,因此循环在完成第一次迭代之前退出
循环迭代的标准语法为:
for (int i = 0; i < array.length; i++) {
doSomething(array[i]);
}
更好的是,使用Java 8功能样式/流:
Arrays.asList(array).forEach(blackjack::doSomething);
// or using streams
Arrays.stream(array).forEach(blackjack::doSomething);
我想您需要count
也用于count2
和if(cardValues[count]==cardValues[count2])
的逻辑应该在count==count2
时更改为exclue,谢谢idk,我怎么没听清楚。老实说,哇。爱你的名字B你是谁结束这个问题的?看,我想你想要count
也用于count2
和if(cardValues[count]==cardValues[count2])
的逻辑应该在count==count2
时更改为exclue,谢谢idk,我怎么没听清楚。老实说,哇。爱你的名字,顺便问一下,你是如何结束这个问题的