【发布时间】:2015-11-13 10:06:01
【问题描述】:
这是我的第一篇文章,所以我希望我做得对。我目前正在研究一个非常简单的黑杰克。
当我测试我的程序时(如下所示),没有任何结果,但除非我终止,否则 Eclipse 只会一直工作。我查了一下这个问题,似乎它可能是一个无限循环。在把代码弄乱了一点之后,我仍然遇到了问题。
虽然我有 99% 的把握是“while (sum
感谢您的帮助。
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);
}
}
}
【问题讨论】:
标签: eclipse infinite-loop blackjack