1
這是我的第一篇文章,所以我希望我這樣做是正確的。我目前正在研究一個非常簡單的黑色插孔。無限循環? Eclipse停止回答當我測試運行
當我測試我的程序(如下圖所示)時,沒有任何東西出來,但eclipse只是一直工作,除非我終止。我查了一下這個問題,看起來它可能是一個無限循環。在弄亂代碼之後,我仍然有這個問題。
儘管我99%確定「while(sum < 16)」循環導致了這個問題,但我不明白爲什麼它會永遠持續下去,因爲總和應該越來越大。
感謝您的幫助。
public class DrawCardFromDeck
{
\t
\t public static double GetRandomNumber (double Range)
\t //generates a random number from 0 to range-1
\t {
\t \t double Number;
\t \t Number = Math.random()*Range;
\t \t Number = Math.floor(Number);
\t \t return Number;
\t
\t }
\t public static int value(int cardID)
\t //stores values for each "card ID number"
\t {
\t \t 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};
\t \t return cardValue[cardID];
\t \t
\t }
\t public static String suit(int cardID)
\t //stores suits for each "card ID number"
\t {
\t \t 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"};
\t \t return suit[cardID];
\t }
\t
\t public static void main(String[] args)
\t {
\t \t int sum = 0;
\t \t int indexPosition;
\t \t int cardsDrawn = 0;
\t \t
\t \t boolean [] cardCounter = new boolean [52];
\t \t for (indexPosition = 0; indexPosition < cardCounter.length-1; indexPosition++)
\t \t \t cardCounter[indexPosition] = true;
\t \t //keeps track of which "card ID numbers" have been generated
\t \t int [] cardID = new int [10];
\t \t //creates a space in which to save the card ID's
\t \t
\t \t while (sum < 16)
\t \t //Generates random cards while sum of their values is less than 16.
\t \t //Also makes sure each card is only drawn once by referencing the cardCounter array above.
\t \t {
\t \t \t
\t \t \t if (cardCounter[indexPosition] == true)
\t \t \t {
\t \t \t \t cardID[indexPosition] = (int) GetRandomNumber(52);
\t \t \t \t cardCounter[indexPosition] = false; //set equal to false so its not drawn again
\t \t \t \t sum += value(cardID[indexPosition]); //add to the sum
\t \t \t \t cardsDrawn++; //count cards drawn (will be used later)
\t \t \t \t
\t \t \t } \t
\t \t \t \t
\t \t }
\t \t
\t \t
\t \t if (sum > 21) //if bust, reduce values of aces by 10
\t \t {
\t \t \t for (indexPosition = 0; indexPosition < cardsDrawn; indexPosition++)
\t \t \t {
\t \t \t \t
\t \t \t \t if(cardID[indexPosition] % 14 == 0)
\t \t \t \t {
\t \t \t \t \t sum -= 10;
\t \t \t \t } \t
\t \t \t \t
\t \t \t }
\t \t \t
\t \t \t if (sum>21) //if still bust, output bust
\t \t \t \t System.out.println("Bust");
\t \t \t
\t \t }
\t \t
\t \t
\t \t if (sum<=21)
\t \t //Else, print the cards that were drawn and the sum.
\t \t {
\t \t \t
\t \t \t System.out.println("Your hand: ");
\t \t \t for(indexPosition = 0; indexPosition < cardsDrawn; indexPosition++)
\t \t \t {
\t \t \t \t System.out.println(value(cardID[indexPosition]) + suit(cardID[indexPosition]));
\t \t \t }
\t \t \t System.out.println();
\t \t \t System.out.println("The sum is: " + sum);
\t \t \t
\t \t }
\t \t
\t }
\t
}
\t
\t \t
謝謝!我怎麼能忘記,哈哈。 現在我有問題,它只輸出胸圍,但是,但希望我會弄清楚......再次感謝。 ^^ – Jojo
不客氣...不要忘記upvote /接受答案(這將確保下次更願意回答你的問題)。 –