我知道这个游戏在这里很受欢迎,因为对于像我这样的新手来说这是一个很好的开始,但我真的遇到了问题。
这是我的程序在这一刻(主程序)看起来的样子: -
public class Proiect1 {
public static void main(String [] args){
System.out.println("Enter the number of throws: ");
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
game p1=new game();
game p2=new game();
int score1=0,score2=0;
int coin;
System.out.println("The game rules:"+"\n-if coin =1 that means
HEAD"+"\n-if coin =2 that means TAILS"+"\nEnjoy the game!");
System.out.println("The first player will throw the coin!
Let's see if the second player can quess the coin side!");
for(int k=0;k<n;k++){
p2.Player2Choice();
coin=(int)((Math.random()*2)+1);
if(coin!=p2.getDecisionPlayer2()){
System.out.println("You lost!"+"Coin side was: "+coin);
score1++;
} //close if
else{
System.out.println("You win! "+" Coin side was: "+coin);
score2++;
} //close else
System.out.println("Player1 won: "+scor1+" times");
System.out.println("Player2 won: "+scor2+" times");
}close for
System.out.println("Win rate for player1: "+(int)((scor1*100.0f)/n)+"%");
System.out.println("Win rate for player2: "+(int)((scor2*100.0f)/n)+"%");
} //close main
} //close class
我的问题是,如果Player2猜测硬币面和反面,我想用Player2切换Player1。我有一个想法,但结果是失败了!
我真的需要一些帮助,谢谢你的时间!
答案 0 :(得分:0)
这只是2名玩家的游戏。因此,您可以简单地设置布尔值以在玩家之间切换。
例如,将布尔值初始化为
布尔值isPlayer1Chance = true;
当有任何机会改变值作为循环内部时,
如果(isPlayer1Choice){
//玩家1玩耍
}其他{
//播放器2播放
}
isPlayer1Choice =!isPlayer1Choice;