English 中文(简体)
在使用假体时,不得提出错误
原标题: Cannot Find Symbol error when using a for loop
  • 时间:2012-05-04 18:36:51
  •  标签:
  • java

I am a complete beginner with Java and am having trouble understanding how to pass objects around between classes and methods. I have made some progress, however my app now fails when I try to create playing cards inside a for loop. It works fine if I remove the loop. Here is the first part of the class that contains the error:

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

由于该法典如果我搬走,那么我就认为,某些名字卡1和卡2在册外看不见? 如果我想要打10或20张卡,那么我就想在一间 lo中这样做,那么,我就不得不在节目中找不到有关新物体和在其他地方使用新物体的内容。

感谢您的帮助。

** 最新情况:感谢初步反馈。 我现在看到,如果我把我的即时发言移到空局之外,我就可以从理论上为这些物体分配新的价值,再一次使用一种假体,而这正是我完成这一具体任务所需要的。

然而,我仍然很奇怪的是,难道不可能在一片 lo子内出现新的物体,但还是将其置之不理? 这似乎有可能。

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);


}
问题回答

这些卡片和卡片变量在 lo体内申报,因此只在 lo体内可见。 为了在休息室之外使用,你必须宣布休息时间为之>。 请在 Java上读到,其中载列规则。

。 将其初始化至<条码>>。

而且,正如现在一样,它会经常试图超额使用卡1和卡2,因为你同时宣布和开始使用这些卡片的“假日”。 此外,更为重要的是,它们超出了范围。 相反,在座右边事先宣布,只有 初始> > 。

你们可能希望的是:

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张卡, <> >? 目前,你在休息室之外,只能显示分配给每个人的低地轨道值。 如果你要出示他们重新获得的每张卡,将产出放在坡道内,并可能使用透镜。 睡觉,将方案暂停半年,这样他们就可以看到他们获得的每张卡。 或者写出一个超负荷的产出,接收一张卡片,同时印制所有卡片。 如果你需要帮助,我再次问我。

By the way, im not sure what cardcompare() is really doing behind the scenes, but you probably want to make it return a bool representing whether or not they were different. something like:

bool areCardsDistinct = cardcompare(card1,card2);

That way, you can use the result to decide whether or not to get random new cards again





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...