【问题标题】:Variable declaration not allowed in JavaJava中不允许变量声明
【发布时间】:2014-12-04 18:45:05
【问题描述】:

这是我学校项目中的一个 Java 扑克项目。

一开始就定义了 Card 类。

class Card {

/* constant suits and ranks */
static final String[] Suit = {"Clubs", "Diamonds", "Hearts", "Spades" };
static final String[] Rank = {"","A","2","3","4","5","6","7","8","9","10","J","Q","K"};

/* Data field of a card: rank and suit */
private int cardRank;  /* values: 1-13 (see Rank[] above) */
private int cardSuit;  /* values: 0-3  (see Suit[] above) */

/* Constructor to create a card */
/* throw MyPlayingCardException if rank or suit is invalid */
public Card(int rank, int suit) throws MyPlayingCardException { 
if ((rank < 1) || (rank > 13))
    throw new MyPlayingCardException("Invalid rank:"+rank);
else
        cardRank = rank;
if ((suit < 0) || (suit > 3))
    throw new MyPlayingCardException("Invalid suit:"+suit);
else
        cardSuit = suit;
}

/* Accessor and toString */
/* You may impelemnt equals(), but it will not be used */
public int getRank() { return cardRank; }
public int getSuit() { return cardSuit; }
public String toString() { return Rank[cardRank] + " " + Suit[cardSuit]; }

然后,我尝试定义 Deck 类。但我有一些错误。

class Decks {

/* this is used to keep track of original n*52 cards */
private List<Card> originalDecks;   

/* this starts with n*52 cards deck from original deck   */
/* it is used to keep track of remaining cards to deal */
/* see reset(): it resets dealDecks to a full deck      */
private List<Card> dealDecks;

/* number of decks in this object */
private int numberDecks;

  public Decks()
{
    ArrayList<Card> originalDecks = new ArrayList<Card>(52);
    ArrayList<Card> dealDecks = new ArrayList<Card>(52); 

     Card card = new Card(j,i);   //Error

    for (int i=0; i<=3; i++)  
        for (int j=0; j<= 13; j++)              

              originalDecks.add(card);   //Error

    dealDecks.addAll(originalDecks); 


}

公共甲板(int n) {

    int numberDecks=n ; 

    Decks originalDecks = new Decks();

    for (int m=0; m< n; m++){

         originalDecks += originalDecks ; 

    }


}

这个想法是:首先,我尝试用 52 张牌创建一副牌;然后我将它捆绑循环 n 次以创建 n 套牌。但是,我遇到了一些未解决的错误,告诉我

找不到符号 i、j。

这是为什么呢?

【问题讨论】:

    标签: java constructor


    【解决方案1】:

    因为您在定义ij 之前尝试创建Card。另外,请使用大括号。

    // Card card = new Card(j,i);
    for (int i=0; i<=3; i++) {  
        for (int j=0; j<= 13; j++) {
            Card card = new Card(j,i);
            originalDecks.add(card); 
        }
    }
    

    【讨论】:

    • 我尝试将其移入,但它显示新错误:“此处不允许变量声明”。
    • @LijieZhou 大括号加了吗?
    • 解决了这个问题。非常感谢您的快速回复!
    【解决方案2】:

    对于这个声明

    Card card = new Card(j,i);
    

    范围内没有变量ij,因此是合法的编译错误

    你想将它们移动到内部循环中

     for (int i=0; i<=3; i++)  {
            for (int j=0; j<= 13; j++) {              
                Card card = new Card(j,i);
                originalDecks.add(card); 
    
            }
      }
    

    【讨论】:

      【解决方案3】:

      大部分 RuntimeError “这里不允许变量声明”是因为缺少大括号。

      例子:-

      public class Creator
      {
          public static void main(String[] args)
          {
              for(int i=0;i<100;i++)
              Creature creature=new Creature(); // Error 
      
              System.out.println(Creature.numCreated());
      
          }
      }
      

      编译此代码时出现错误:此处不允许变量声明。

      但是,

          for(int i=0;i<100;i++)
          {Creature creature=new Creature();} //no error
      

      这将解决错误。

      【讨论】:

        猜你喜欢
        • 2015-09-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-07-21
        • 2015-02-20
        • 2011-09-20
        相关资源
        最近更新 更多