为了账号安全,请及时绑定邮箱和手机立即绑定

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

标签:
Java

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人点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消