这是我的第一篇文章,所以我希望我做得对。我正在研究一个非常简单的黑色插孔。
当我测试我的程序时(如下所示),没有任何结果,但是eclipse只是一直工作,除非我终止。我查了一下这个问题,看起来它可能是一个无限循环。在更多地处理代码之后,我仍然遇到了问题。
虽然我99%确定“while(sum< 16)”循环导致问题,但我不明白为什么它会永远持续下去,因为总和应该越来越大。
感谢您的帮助。
public class DrawCardFromDeck
{
public static double GetRandomNumber (double Range)
//generates a random number from 0 to range-1
{
double Number;
Number = Math.random()*Range;
Number = Math.floor(Number);
return Number;
}
public static int value(int cardID)
//stores values for each "card ID number"
{
int [] cardValue = {2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11};
return cardValue[cardID];
}
public static String suit(int cardID)
//stores suits for each "card ID number"
{
String [] suit = {"of hearts", "of spades", "of Diamonds", "of Clubs", "of hearts", "of spades", "of Diamonds", "of Clubs", "of hearts", "of spades", "of Diamonds", "of Clubs", "of hearts", "of spades", "of Diamonds", "of Clubs", "of hearts", "of spades", "of Diamonds", "of Clubs", "of hearts", "of spades", "of Diamonds", "of Clubs", "of hearts", "of spades", "of Diamonds", "of Clubs", "of hearts", "of spades", "of Diamonds", "of Clubs", "of hearts", "of spades", "of Diamonds", "of Clubs", "of hearts", "of spades", "of Diamonds", "of Clubs", "of hearts", "of spades", "of Diamonds", "of Clubs", "of hearts", "of spades", "of Diamonds", "of Clubs", "of hearts", "of spades", "of Diamonds", "of Clubs"};
return suit[cardID];
}
public static void main(String[] args)
{
int sum = 0;
int indexPosition;
int cardsDrawn = 0;
boolean [] cardCounter = new boolean [52];
for (indexPosition = 0; indexPosition < cardCounter.length-1; indexPosition++)
cardCounter[indexPosition] = true;
//keeps track of which "card ID numbers" have been generated
int [] cardID = new int [10];
//creates a space in which to save the card ID's
while (sum < 16)
//Generates random cards while sum of their values is less than 16.
//Also makes sure each card is only drawn once by referencing the cardCounter array above.
{
if (cardCounter[indexPosition] == true)
{
cardID[indexPosition] = (int) GetRandomNumber(52);
cardCounter[indexPosition] = false; //set equal to false so its not drawn again
sum += value(cardID[indexPosition]); //add to the sum
cardsDrawn++; //count cards drawn (will be used later)
}
}
if (sum > 21) //if bust, reduce values of aces by 10
{
for (indexPosition = 0; indexPosition < cardsDrawn; indexPosition++)
{
if(cardID[indexPosition] % 14 == 0)
{
sum -= 10;
}
}
if (sum>21) //if still bust, output bust
System.out.println("Bust");
}
if (sum<=21)
//Else, print the cards that were drawn and the sum.
{
System.out.println("Your hand: ");
for(indexPosition = 0; indexPosition < cardsDrawn; indexPosition++)
{
System.out.println(value(cardID[indexPosition]) + suit(cardID[indexPosition]));
}
System.out.println();
System.out.println("The sum is: " + sum);
}
}
}
答案 0 :(得分:0)
问题是没有设置indexPosition。由于此变量用作索引,因此将使用相同的卡,该卡仅添加一次。
实际上我希望indexPosition由于上面的迭代循环而导致超出最后一张卡,但可能这不是真的。