专栏首页葫芦java 三个人猜数字 直到有人猜对为止

java 三个人猜数字 直到有人猜对为止

public class GuessGame{
Player p1;
Player p2;
Player p3;
public void startGame(){
p1=new Player();
p2=new Player();
p3=new Player();

int guessp1=0;
int guessp2=0;
int guessp3=0;

boolean p1isRight= false;
boolean p2isRight=false;
boolean p3isRight=false;

int targetNumber=(int)(Math.random()*10);
System.out.println("I'm thinking of a number between 0 and 9...");
while(true){
System.out.println("Number to guess is"+ targetNumber);
p1.guess();
p2.guess();
p3.guess();
guessp1=p1.number;
System.out.println("Player one guessed"+guessp1);
guessp2=p2.number;
System.out.println("Player two guessed"+guessp2);
guessp3=p3.number;
System.out.println("Player three guessed"+guessp3);

if(guessp1==targetNumber){
p1isRight= true;
}
if(guessp2==targetNumber){
p2isRight=true;
}
if(guessp3==targetNumber){
p3isRight=true;
}
if(p1isRight || p2isRight || p3isRight){
System.out.println("We have a winner!");
System.out.println("Player one got it right?"+p1isRight);
System.out.println("Player two got it right?"+p2isRight);
System.out.println("Player three got it right?"+p3isRight);
System.out.println("Game is over!");
break;
}else
{
System.out.println("Players will have to try again");
}

}
}
}


public class Player{
     int number=0;
     public void guess(){
     number=(int)(Math.random()*10);
          System.out.println("I'm guessing "+ number);
     }

} 

public class GameLauncher
{
     public static void main(String[] args)
     {
          GuessGame game=new GuessGame();
          game.startGame();
     //     System.out.println("Hello World!");
     }
}
 /*c:\java>java GameLauncher
I'm thinking of a number between 0 and 9...
Number to guess is3
I'm guessing 1
I'm guessing 7
I'm guessing 0
Player one guessed1
Player two guessed7
Player three guessed0
Players will have to try again
Number to guess is3
I'm guessing 5
I'm guessing 6
I'm guessing 5
Player one guessed5
Player two guessed6
Player three guessed5
Players will have to try again
Number to guess is3
I'm guessing 3
I'm guessing 3
I'm guessing 0
Player one guessed3
Player two guessed3
Player three guessed0
We have a winner!
Player one got it right?true
Player two got it right?true
Player three got it right?false
Game is over!*/

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

我来说两句

0 条评论
登录 后参与评论

相关文章

  • python 快排算法

    先说两句题外话,一般意义上的栈有两层含义,一层是后进先出的数据结构栈,一层是指函数的内存栈,归根结底,函数的内存栈的结构就是一个后进先出的栈。汇编代码中,调用一...

    葫芦
  • cssjshtml vue.js 时间格式化

    葫芦
  • cssjshtml vue.js 事件传值 子向父组件传值

    葫芦
  • Dart 笔记 4 - 运算符

    condition ? expr1 : expr2 如果条件为真,则计算并返回 expr1 的值,否则计算并返回 expr2 的值。

    七适散人
  • openlayers5中用iconfont作为图标

    前面有文章 openlayers4中用font设置图标样式 就iconfont在ol5中的使用做了一个尝试,后面有很多童鞋问我说实现不了,一直没有时间去做一个...

    lzugis
  • TypeScript学习第二篇 - 数据类型

    Typescript中为了使编写的代码更规范,更有利于维护,增加了类型校验,一个标识符一旦定义了类型,后续便不能再修改,主要有以下几种数据类型。

    越陌度阡
  • C#的DataTable操作方法

    1.将泛型集合类转换成DataTable(表中无数据时使用): public static DataTable NullListToDataTable(IL...

    彭泽0902
  • node运行js获得输出的三种方式

    小小咸鱼YwY
  • Java反射研究和实践

    本博文中项目代码已开源下载地址:GitHub Java反射研究和实践 概述 Java的反射机制是Java语言动态性的一种体现。反射机制是通过反射API来实现的,...

    CrazyCodeBoy
  • AI大事件 | WaveNet推出了谷歌助手,苹果发布CoreML转换器

    大数据文摘

扫码关注云+社区

领取腾讯云代金券