手记

猜字游戏,您能加入对象小镇吗?在DOS里试一下吧

class Player{

   int x=0;  //保存玩家猜测的数字
   void guess(){
       x=(int)(Math.random()*10);   //产生随机数字
       System.out.println(x);

}
}

class GuessGame{

Player p1;
Player p2;
Player p3;
void startGame(){
     //创建Player对象
     p1=new Player();
     p2=new Player();
     p3=new Player();
     //系统产生的随机数字
     int sysGuess; //定义变量保存系统产生的随机数字
     sysGuess=(int)(Math.random()*10);
     System.out.println("system has guessed:"+sysGuess);

     /*System.out.println("Player1 is guessing:");
     p1.guess();
     System.out.println("Player2 is guessing:");
     p2.guess();
     System.out.println("Player3 is guessing:");
     p3.guess();*/

     //定义每个人猜测的最大次数,初始化为1
     int max=1;
     /*boolean p1IsRight=false; 
     boolean p2IsRight=false;
     boolean p3IsRight=false;*/
     int count=0; //计算猜测的回合数

/System.out.println(p1.x);
System.out.println(p2.x); //未调用guess方法之前,x为0
System.out.println(p3.x);
/

     while(max<=5){
           count+=1;
           System.out.println(count+"round:");
        //将玩家猜测的数字和系统产生的比较
        if(p1.x!=sysGuess){
        System.out.print("Player1 is guessing:");
        p1.guess();
        if(p1.x==sysGuess){
        System.out.println("Congratulations player1 can be a member of objectTown!"); 
        break;

}

}else{

        //p1IsRight=true;
        System.out.println("Congratulations player1 can be a member of objectTown!"); 
        break;

}

       if(p2.x!=sysGuess){
         System.out.print("Player2 is guessing:");
         p2.guess();
          if(p2.x==sysGuess){
        System.out.println("Congratulations player2 can be a member of objectTown!"); 
        break;

}

}else{

        //p2IsRight=true;
        System.out.println("Congratulations player2 can be a member of objectTown!"); 
        break;

}

         if(p3.x!=sysGuess){
         System.out.print("Player3 is guessing:");
         p3.guess();
          if(p1.x==sysGuess){
        System.out.println("Congratulations player1 can be a member of objectTown!"); 
        break;

}

}else{

        //p3IsRight=true;
        System.out.println("Congratulations player3 can be a member of objectTown!");
        break; 

}

      //猜测回合超过5,则终止猜测
       max=max+1;
       if(max>5)  System.out.println("oh my God! No Pass!");

}

}
}

class GameLaugher{
//程序启动器

public static void main(String[] args){
   GuessGame s=new GuessGame();
   s.startGame();

}

}

1人推荐
随时随地看视频
慕课网APP