有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

使用“for”循环时出现java“找不到符号”错误

我是Java的完全初学者,在理解如何在类和方法之间传递对象时遇到困难。我已经取得了一些进展,但是当我尝试在for循环中创建扑克牌时,我的应用程序现在失败了。如果我去掉这个循环,效果很好。下面是包含错误的类的第一部分:

public class Testing
{
    public static void main(String[] args)
    {

   int Deal = 1;

   for(int Hand = 0; Hand < Deal; ++Hand)
   {
     //instantiate and derive values for Player
     Card card1 = new Card();
     card1.setSuit();  //assign card 1's suit
     card1.setValue(); //asign card 1's value

     //instantiate and derive values for Computer
     Card card2 = new Card();
     card2.setSuit();  //assign card 2's suit
     card2.setValue(); //assign card 2's suit

     //compare the two cards and make sure they are different
     cardCompare(card1,card2);
   }

   //output the two cards to the screen
   output(card1,card2);

}

这是我得到的错误:

Testing.java:26: error: cannot find symbol
   output(card1,card2);
          ^
  symbol:   variable card1
  location: class Testing

Testing.java:26: error: cannot find symbol
   output(card1,card2);
                ^
  symbol:   variable card2
  location: class Testing
2 errors

因为如果我删除for循环,代码就可以工作,所以我假设card1和card2的名称在循环外是不可见的?如果我想创建十张或二十张卡片,我想在一个循环中创建,因此我必须缺少一些关于实例化新对象和在程序中其他地方使用它们的内容

谢谢你的帮助

**更新:感谢您的初步反馈。现在我明白了,如果我将实例化语句移到for循环之外,理论上我可以使用循环一次又一次地为这些对象分配新值,这就是我完成这个特定任务所需要的

不过我还是很好奇,是否不可能在循环内实例化新对象,但仍在循环外使用它们?看来这一定是有可能的

public class Testing
   {
    public static void main(String[] args)
   {

int Deal = 1;

   //instantiate and derive values for Player
     Card card1 = new Card();

      //instantiate and derive values for Computer
     Card card2 = new Card();

   for(int Hand = 0; Hand < Deal; ++Hand)
   {
     card1.setSuit();  //assign card 1's suit
     card1.setValue(); //asign card 1's value

     card2.setSuit();  //assign card 2's suit
     card2.setValue(); //assign card 2's value

    //compare the two cards and make sure they are different
     cardCompare(card1,card2);
   }


   //output the two cards to the screen
   output(card1,card2);


}

共 (3) 个答案

  1. # 1 楼答案

    card1和card变量在for循环内声明,因此仅在循环内可见。要在循环之外使用它,必须在循环之前声明它。请阅读Java范围规则

  2. # 2 楼答案

    好吧,就目前而言,它将继续尝试覆盖card1和card2,因为您将同时声明和初始化它们“交易”时间。此外,更重要的是,它们将超出范围。相反,事先声明它,并且只在循环中初始化它们

    您可能想要的是:

    public class Testing
       {
        public static void main(String[] args)
       {
    
     int Deal = 1;
    
     ArrayList<Card> playerCards = new ArrayList<Card>();
     ArrayList<Card> computerCards = new ArrayList<Card>();
    
      //instantiate and derive values for Player
     Card card1;
    
     //instantiate and derive values for Computer
     Card card2;
    
     for(int Hand = 0; Hand < Deal; ++Hand)
     {
        card1 = new Card();
        card1.setSuit();  //assign card 1's suit
        card1.setValue(); //asign card 1's value
    
        card2 = new Card();
        card2.setSuit();  //assign card 2's suit
        card2.setValue(); //assign card 2's value
    
    
    
       //compare the two cards and make sure they are different
       cardCompare(card1,card2);
    
       playerCards.Add(card1);
       computerCards.Add(card2);
    

    }

      //output the two cards to the screen
    
      output(card1,card2);
    

    }

    还没有测试过,但应该可以用

    您还需要重新考虑使用输出方法。既然每人将拥有约20张卡片,您认为何时需要向用户展示它们?目前,它位于for循环之外,因此它只显示分配给每个循环的最后一个值。如果你想让他们看到他们得到的每一张卡,把输出调用放在for循环中,也许可以使用Thread。Sleep()将程序暂停半秒钟左右,以便他们可以看到收到的每张卡。或者写一个重载输出,接受一个卡片数组列表,并同时打印所有卡片。再说一次,如果你需要帮助,问我

    顺便说一下,我不确定cardcompare()在幕后到底在做什么,但您可能希望它返回一个bool,表示它们是否不同。比如:

    bool areCardsDistinct = cardcompare(card1,card2);
    

    这样,您可以使用结果来决定是否再次随机获得新卡

  3. # 3 楼答案

    card1card2都在for循环的范围内,而不是main()的其余部分。将初始化移动到for之前