【问题标题】:Infinite loop? Eclipse stops answering when I test run无限循环?当我测试运行时 Eclipse 停止响应
【发布时间】: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


    【解决方案1】:

    问题是 indexPosition 没有设置。由于此变量用作索引,因此将使用仅添加一次的同一张卡片。

    实际上我希望 indexPosition 由于上面的迭代循环会导致超出最后一张卡片,但可能这不是真的。

    【讨论】:

    • 谢谢!我怎么会忘记,哈哈。现在我遇到的问题是它只输出 bust ,但希望我能解决这个问题......再次感谢。 ^^
    • 不客气......不要忘记投票/接受答案(这将确保下次更愿意回答您的问题)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-28
    • 2010-12-17
    相关资源
    最近更新 更多