[英]How do I connect the return from one method in one class, to another method in another class?
我正在尝试使类方法playerTurn与另一个类交互。
import java.util.*;
public boolean PlayerTurn(){
System.out.println("To determine who goes first, I am thinking of a number between 1 and 5, including 1 and 5.");
int comp1 = rand.nextInt(5)+1;
System.out.println(player1+", input guess: ");
int g1 = scan.nextInt();
System.out.println(player2+", input guess: ");
int g2 = scan.nextInt();
int p1 = Math.abs (comp1 -g1);
int p2 = Math.abs(comp1-g2);
if(p1<p2){
System.out.println("The number was "+comp1+". " +player1+", will go first.");
return true ;
}
else
System.out.println("The number was "+comp1+". "+player2+", will go first.");
return true;
}
它可以打印出正确的内容,但我需要与此相关的内容。 如果为true,则player1首先进入,那就是第一个if语句,但是它甚至没有运行,而是返回到PlayerTurn方法,即使它没有第二次被调用:
public class fullCard {
ArrayList<Integer>cardDraw1 = new ArrayList<Integer>(12);
ArrayList<Integer>cardDraw2 = new ArrayList<Integer>(12);
Random rand = new Random();
Scanner scan = new Scanner(System.in);
Intro test = new Intro();
Player p = new Player();
int MysteryCard, ans1, ans2;
int total1, total2, newHighCard, newLowCard;
public fullCard(){
int total1= 0, total2= 0;
int newHighCard=0;
int newLowCard=0;
MysteryCard = rand.nextInt(13)+1;
}
public String MysteryDeck(){
for(int i =0; i<12; i++){
System.out.println("Why");<--this prints
if(p.PlayerTurn())//DOES NOT WORK AT ALL
System.out.print(" Player 1 Type 1 to pick from a deck higher than the MysteryCard and Type 2 to draw from a deck lower.");
ans1 = scan.nextInt();
if(ans1 ==1)
System.out.println("You have chosen to pick from cards between"+MysteryCard+" and 13");
for(int k= 0; k < cardDraw1.size(); k++)
newHighCard = rand.nextInt(13-MysteryCard)+(MysteryCard+1);
cardDraw1.add(newHighCard);
for(int v=0; v<cardDraw1.size(); v++)
total1+=cardDraw1.get(v);
System.out.println(total1);
if(ans1== 2)
System.out.println("You will draw from a deck 1 to "+MysteryCard);
for(int k= 0; k<cardDraw1.size();k++)
newLowCard = rand.nextInt(MysteryCard+1)+1;
cardDraw1.add(newLowCard);
for (int v=0; v<cardDraw1.size(); v++)
total1+=cardDraw1.get(v);
System.out.println(total1);
if(!p.PlayerTurn())
System.out.println("Player 2 Type 1 to pick from a deck higher than the MysterCard and type 2 to draw from a deck lower than the MysteryCard.");
ans2=scan.nextInt();
if(ans2 ==1)
System.out.println("You have chosen to pick from cards between "+MysteryCard+" and 13");
for(int k= 0; k<cardDraw2.size(); k++)
newHighCard = rand.nextInt(13-MysteryCard)+(MysteryCard+1);
cardDraw2.add(newHighCard);
for(int d = 0; d<cardDraw2.size(); d++)
total2 +=cardDraw2.get(d);
System.out.println(total2);
if(ans2== 2)
System.out.println("You will draw from a deck 1 to "+MysteryCard);
for(int k= 0; k<cardDraw2.size(); k++)
newLowCard = rand.nextInt(MysteryCard+1)+1;
cardDraw2.add(newLowCard);
System.out.println(cardDraw2);
}
return ("");
}
}
您忘记在PlayerTurn()
方法中为else block
添加{}
。
else
{ // add this
System.out.println("The number was "+comp1+". "+player2+", will go first.");
return true;
} // and this
如果要一起执行多条指令,请确保在其余的代码中添加{}
for if block
, else block
和for loop block
。
例如,让我们获取这段代码
if(ans1 ==1)
System.out.println("You have chosen to pick from cards between"+MysteryCard+" and 13");
for(int k= 0; k < cardDraw1.size(); k++)
newHighCard = rand.nextInt(13-MysteryCard)+(MysteryCard+1);
cardDraw1.add(newHighCard);
for(int v=0; v<cardDraw1.size(); v++)
total1+=cardDraw1.get(v);
System.out.println(total1);
在这里,只有System.out.println("You have chosen to pick from cards between"+MysteryCard+" and 13");
每当ans1 ==1
时将执行。
我认为您想按以下步骤进行操作(添加{}
)
if(ans1 ==1)
{ //add this
System.out.println("You have chosen to pick from cards between"+MysteryCard+" and 13");
for(int k= 0; k < cardDraw1.size(); k++)
newHighCard = rand.nextInt(13-MysteryCard)+(MysteryCard+1);
cardDraw1.add(newHighCard);
for(int v=0; v<cardDraw1.size(); v++)
total1+=cardDraw1.get(v);
System.out.println(total1);
}// ans this
这就是整个MysteryDeck()
方法,其中添加了您错过的{}
。
public String MysteryDeck(){
for(int i =0; i<12; i++){
System.out.println("Why");<--this prints
if(p.PlayerTurn())
{ // here
System.out.print(" Player 1 Type 1 to pick from a deck higher than the MysteryCard and Type 2 to draw from a deck lower.");
ans1 = scan.nextInt();
if(ans1 ==1)
{ // here
System.out.println("You have chosen to pick from cards between"+MysteryCard+" and 13");
for(int k= 0; k < cardDraw1.size(); k++)
{ // here
newHighCard = rand.nextInt(13-MysteryCard)+(MysteryCard+1);
cardDraw1.add(newHighCard);
} // here
for(int v=0; v<cardDraw1.size(); v++)
{// here optional
total1+=cardDraw1.get(v);
} //here optional
System.out.println(total1);
} // here
if(ans1== 2)
{ // here
System.out.println("You will draw from a deck 1 to "+MysteryCard);
for(int k= 0; k<cardDraw1.size();k++)
{ // here
newLowCard = rand.nextInt(MysteryCard+1)+1;
cardDraw1.add(newLowCard);
} // here
for (int v=0; v<cardDraw1.size(); v++)
{ // here optional
total1+=cardDraw1.get(v);
} // here optional
System.out.println(total1);
} // here
} // here
if(!p.PlayerTurn())
{ // here
System.out.println("Player 2 Type 1 to pick from a deck higher than the MysterCard and type 2 to draw from a deck lower than the MysteryCard.");
ans2=scan.nextInt();
if(ans2 ==1)
{ // here
System.out.println("You have chosen to pick from cards between "+MysteryCard+" and 13");
for(int k= 0; k<cardDraw2.size(); k++)
{ // here
newHighCard = rand.nextInt(13-MysteryCard)+(MysteryCard+1);
cardDraw2.add(newHighCard);
} // here
for(int d = 0; d<cardDraw2.size(); d++)
{ // here optional
total2 +=cardDraw2.get(d);
} // here optional
System.out.println(total2);
} // here
if(ans2== 2)
{ // here
System.out.println("You will draw from a deck 1 to "+MysteryCard);
for(int k= 0; k<cardDraw2.size(); k++)
{ // here
newLowCard = rand.nextInt(MysteryCard+1)+1;
cardDraw2.add(newLowCard);
} // here
System.out.println(cardDraw2);
}//here
}// here
}
return ("");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.