简体   繁体   English

编写二十一点方法时应使用']'

[英]']' expected when writing Blackjack method

 while (hitOrStay.equalsIgnoreCase("hit"))
      {

        Card temp[hitOrStayCounter] = cardDeck.deal(); // issue occurs on this line
        System.out.println("You are delt a " + temp[hitOrStayCounter].getBJ()); 
        hitOrStayCounter++;
        System.out.println("Would you like to hit again?"); 
        hitOrStay = ketboard.next(); 
      }

Here is the segment of my code with the problem. 这是我的问题代码段。 I'm getting a problem where it says that it is expecting a ']', and I've tried to fix it to no avail. 我遇到了一个问题,它说它期望一个']',并且我试图将其修复为无济于事。 Any suggestions? 有什么建议么?

 public Card deal()
 {
    nextCard++;
    return deck[nextCard-1];
 }  
public static void main(String[] args)  
  {
   int hitOrStayCounter=5;
   Bankroll money = new Bankroll();
   Deck cardDeck = new Deck(); 
   Scanner keyboard = new Scanner(System.in); 
   System.out.println("Would you like to play?"); 
   String playGame = keyboard.next(); 
   while (playGame.equalsIgnoreCase("yes")) 
   }

Also here is the specific error: 这也是特定的错误:

Error: ']' expected 错误:预期为']'

The problem is that you're trying to declare an array size in the array definition. 问题是您试图在数组定义中声明数组大小。 The array size should be initialized on the right side of the equal sign. 数组大小应在等号的右侧初始化。 Based on my knowledge of blackjack, I can provide these suggestions: 根据我对二十一点的了解,我可以提供以下建议:

  • Use an ArrayList instead of an array. 使用ArrayList而不是数组。 This should be done because an ArrayList can continue to expand while an array must be initialized to a specific size. 这样做是因为ArrayList可以继续扩展,而必须将数组初始化为特定大小。 This is needed since someone can hit as much as they want. 这是必需的,因为某人可以击中想要的东西。
  • Put the ArrayList outside of your while loop. 将ArrayList放在while循环之外。 By defining the variable inside the while loop, it gets overwritten with each iteration of the loop. 通过在while循环内定义变量,循环的每次迭代都会覆盖该变量。

You can continue to use an array, but you should still declare and initialize it outside of the while loop. 您可以继续使用数组,但仍应在while循环之外声明并初始化它。 If you use an array, be sure to set a size that is large enough to handle multiple hits. 如果您使用数组,请确保将大小设置为足以处理多个匹配。 You should use the following format if you continue to use an array: 如果继续使用数组,则应使用以下格式:

Card[] temp = new Card[x]

try this. 尝试这个。 your loop is meaning less. 您的循环意义不大。

  public static void main(String[] args)  
    {
        do{   
          int hitOrStayCounter=5;
          Bankroll money = new Bankroll();
          Deck cardDeck = new Deck(); 
          Scanner keyboard = new Scanner(System.in); 
          System.out.println("Would you like to play?"); 
          String playGame = keyboard.next(); 
        }while (playGame.equalsIgnoreCase("yes")); 
   }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM