我试图让类方法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,则第一个是第一个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 ("");
}
}
答案 0 :(得分:0)
您忘记在T
方法中为{}
添加else block
。
PlayerTurn()
如果您希望同时执行多条指令,请务必在代码的其余部分添加else
{ // add this
System.out.println("The number was "+comp1+". "+player2+", will go first.");
return true;
} // and this
{}
和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()
。
{}