如何调用和执行方法
How to call upon and execute a method
我有一个可能非常基本的问题,但我似乎无法在堆栈中找到它(而且我确实发现它非常复杂)所以如果它已经存在于某处,我感到非常抱歉。
我正在 java 中编写一个纸牌游戏,我想将绘制新纸牌的代码片段移到一个单独的方法中,这样我就可以随时调用它,而不是每当我需要时,同一段代码会一次又一次地出现。
唯一的问题是我不知道使用什么类型的方法或如何调用它,因为它不会 return 任何东西,只是做一堆代码。
这是我的代码片段,如果它有助于显示我的目标的话,可以抽一张牌。
if (event.getSource() == hit && endOfRound == false) {
Continue = true;
while (Continue == true) //If the random number has been drawn before, loop the random number generator again
{
cardID = (int) RandomNumber.GetRandomNumber(52);
if (cardsLeft[cardID] == true) //If a new card is drawn, continue normally
{
Continue = false; //Stop the loop that draws a new card
cardsLeft[cardID] = false; //save that the card was drawn
stay.setBackground(Color.green);
plyrSum += BlackJack.value(cardID); //add value to sum
plyrSumLabel.setText("Your sum: " + plyrSum); //display new sum
cardLabel[plyrCardCounter].setIcon(cardImage[cardID]); //Display card
plyrCardCounter++;
if (cardID >= 48) //record how many aces are in the hand so the program knows how many times it can reduce the sum.
plyrAceCounter++;
while (plyrSum > 21 && plyrAceCounter > 0) //If bust, reduce the value of an ace by 10 (from 11 to 1).
{
plyrSum -= 10;
plyrSumLabel.setText("Your sum: " + plyrSum);
plyrAceCounter--;
}
if (plyrSum > 21) //Automatically end the round if someone busts (player)
{
stay.doClick();
}
}
}
}
声明方法 drawCard() returns 无效。现在复制 if 中的块并将其粘贴到方法中。
然后重写if部分:
if (event.getSource() == hit && endOfRound == false) drawCard();
我有一个可能非常基本的问题,但我似乎无法在堆栈中找到它(而且我确实发现它非常复杂)所以如果它已经存在于某处,我感到非常抱歉。
我正在 java 中编写一个纸牌游戏,我想将绘制新纸牌的代码片段移到一个单独的方法中,这样我就可以随时调用它,而不是每当我需要时,同一段代码会一次又一次地出现。
唯一的问题是我不知道使用什么类型的方法或如何调用它,因为它不会 return 任何东西,只是做一堆代码。
这是我的代码片段,如果它有助于显示我的目标的话,可以抽一张牌。
if (event.getSource() == hit && endOfRound == false) {
Continue = true;
while (Continue == true) //If the random number has been drawn before, loop the random number generator again
{
cardID = (int) RandomNumber.GetRandomNumber(52);
if (cardsLeft[cardID] == true) //If a new card is drawn, continue normally
{
Continue = false; //Stop the loop that draws a new card
cardsLeft[cardID] = false; //save that the card was drawn
stay.setBackground(Color.green);
plyrSum += BlackJack.value(cardID); //add value to sum
plyrSumLabel.setText("Your sum: " + plyrSum); //display new sum
cardLabel[plyrCardCounter].setIcon(cardImage[cardID]); //Display card
plyrCardCounter++;
if (cardID >= 48) //record how many aces are in the hand so the program knows how many times it can reduce the sum.
plyrAceCounter++;
while (plyrSum > 21 && plyrAceCounter > 0) //If bust, reduce the value of an ace by 10 (from 11 to 1).
{
plyrSum -= 10;
plyrSumLabel.setText("Your sum: " + plyrSum);
plyrAceCounter--;
}
if (plyrSum > 21) //Automatically end the round if someone busts (player)
{
stay.doClick();
}
}
}
}
声明方法 drawCard() returns 无效。现在复制 if 中的块并将其粘贴到方法中。 然后重写if部分:
if (event.getSource() == hit && endOfRound == false) drawCard();