3

我是一个完整的 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 在循环外不可见?如果我想创建 10 或 20 张卡片,我会想在一个循环中完成,所以我一定会遗漏一些关于实例化新对象和在程序的其他地方使用它们的东西。

谢谢你的帮助。

**更新:感谢最初的反馈。我现在看到,如果我将我的实例化语句移到 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);


}
4

3 回答 3

5

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

于 2012-05-04T18:39:43.747 回答
2

card1和都card2for循环范围内,而不是main(). 将您的初始化移到for.

于 2012-05-04T18:40:15.460 回答
0

好吧,就目前而言,它将继续尝试覆盖 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 循环之外,因此它只会显示分配给每个循环的 LAST 值。如果您想向他们展示他们获得的每张卡片,请将输出调用放在 for 循环中,并且可能使用 Thread.Sleep() 将程序暂停半秒左右,以便他们可以看到他们获得的每张卡片。那,或者写一个接受卡片数组列表的输出过载,并同时打印所有卡片。再一次,如果你需要帮助,问我。

顺便说一句,我不确定 cardcompare() 在幕后到底在做什么,但你可能想让它返回一个表示它们是否不同的布尔值。就像是:

bool areCardsDistinct = cardcompare(card1,card2);

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

于 2012-05-04T20:35:03.780 回答