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