我正在制作一个二十一点程序。在 cardDraw 方法中,我在返回之前的末尾有一个 for 循环,应该 println 每个数组值,但是当我运行程序时没有显示任何内容。此外,当我在 main 方法中打印数组时,即使我在方法中设置值,每个值都是 0。有人可以解释一下为什么会发生这些事情吗?这可能是一个逻辑错误,但我在程序中的其他方法中遇到了这个问题。我们只允许使用一维数组。提前致谢。
最后一件事,您能否收到此网站的电子邮件通知
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
循环只是一个稍微修饰过的 while
循环。
循环:
for (initial; condition; increment) {
// do something
}
相当于:
initial;
while(condition) {
// do something
increment;
}
有一点异常(exception),初始语句中描述的变量仅具有循环范围。
当你写下:
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]);
}
(可以说)更易读的方法是使用“for-each”循环:
for (int value : array) {
doSomething(value); // loops over all values in the array, creating a variable value in each iteration
}
更好的是,使用 Java 8 函数样式/流:
Arrays.asList(array).forEach(blackjack::doSomething);
// or using streams
Arrays.stream(array).forEach(blackjack::doSomething);
关于java - 一维数组不在 for 循环中打印,始终等于 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59851432/