案例十二:模拟三个人进行猜数字的游戏,直到其中至少有一个人猜中了系统随机给出的一个0~9的数字,游戏结束。

2018/10/16 18:15
阅读数 20
package 猜数字;

/**
 * @author Sauron
 *2018-10-16 18:09:32  XIAMEN
 */
public class GameLauncher {
    int i=0;
    public void guess(){
        i=(int)(Math.random()*10);
        System.out.println("i'm guessing\t"+i);
    }
}


public class TestGameLauncher {
    public static void main(String[] args) {
        System.out.println("i'm thinking of a number betweer 0 and 9...");
        boolean flag=true;
        int j=(int)(Math.random()*10);        
        GameLauncher Player1=new GameLauncher();
        GameLauncher Player2=new GameLauncher();
        GameLauncher Player3=new GameLauncher();        
        do{
            System.out.println("Number to guess is\t"+j);
            Player1.guess();
            Player2.guess();
            Player3.guess();
            System.out.println("Player one guessed\t"+Player1.i);
            System.out.println("Player two guessed\t"+Player2.i);
            System.out.println("Player three guessed\t"+Player3.i);
            if(Player1.i==j||Player2.i==j||Player3.i==j){
                flag=false;
            }
        }while(flag);
        System.out.println("We have a winner!");
        boolean ans1=Player1.i==j;
        boolean ans2=Player2.i==j;
        boolean ans3=Player3.i==j;
        System.out.println("Player one got it right?\t"+ans1);
        System.out.println("Player two got it right?\t"+ans2);
        System.out.println("Player three got it right?\t"+ans3);
        System.out.println("Game is over!");
    }
}

结果如下:

展开阅读全文
got
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部