"Cannot find character" error when using "for" loop

I'm a complete newbie with Java, and it's hard for me to figure out how to pass objects between classes and methods. I made some progress, however my application now fails when I try to create game cards inside the for loop. It works fine if I delete 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 suit
     card1.setValue(); //asign card 1 value

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

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

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

}

This is the error I get:

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

Since the code works, if I delete the for loop, I assume that the names of cards 1 and card2 are not visible outside the loop? If I wanted to create ten or twenty cards, I would like to do it in a loop, so I must be missing something about creating new objects and using them elsewhere in the program.

Thank you for your help.

** : . , , - 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 suit
     card1.setValue(); //asign card 1 value

     card2.setSuit();  //assign card 2 suit
     card2.setValue(); //assign card 2 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
3

card1 card for , , . , . , Java.

+5

card1 card2 for, main(). for.

+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 suit
    card1.setValue(); //asign card 1 value

    card2 = new Card();
    card2.setSuit();  //assign card 2 suit
    card2.setValue(); //assign card 2 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(), , . , , ArrayList , . , , .

By the way, I'm not sure what cardcompare () really does behind the scenes, but you probably want to make it return a bool, imagining if they were different. sort of:

bool areCardsDistinct = cardcompare(card1,card2);

Thus, you can use the result to decide whether to receive random new cards again.

0
source

All Articles