天天看点

Java版 猜数字小游戏

猜数字小游戏是一定是大家学习C语言编程的第一个或者是前几个小小项目了吧,今天,我们使用Java进行猜数字小游戏的实现

有对猜数字小游戏的基本思想不清楚的可以看这篇博客,里面是用C语言进行的实现以及相关讲解

c语言猜数字小游戏

话不多说,上代码

import java.util.Random;
import java.util.Scanner;

class Game {
    // 开始菜单
    public void menu() {
        System.out.println("*******************************************************************");
        System.out.println("************************* 猜数字小游戏 *****************************");
        System.out.println("******************** 0. 退出游戏   1. 开始游戏 *********************");
        System.out.println("*******************************************************************");
    }
    // 生成随机数
    public int random() {
        Random random = new Random();
        return random.nextInt()+;
    }
    // 判断输入数字与随机数之间的关系,大于返回大于0,小于返回小于0,等于返回0
    public int compare(int num, int random) {
        return num-random;
    }
    // 根绝 compare 返回的值输出提示语句,并返回接下来是否继续进行, 猜对了返回1,游戏结束,猜错了返回0,游戏继续
    public int message(int compare) {
        if(compare > ) {
            System.out.println("太大了");
            return ;
        } else if(compare < ) {
            System.out.println("太小了");
            return ;
        } else {
            System.out.println("猜对了");
            return ;
        }
    }
    // 接受stdin输入的字符,返回为对应数字
    public int scanf() {
        System.out.print("请输入:>");
        Scanner input = new Scanner(System.in);
        String str = input.next();
        int num = Integer.parseInt(str);
        return num;
    }

    // 逻辑函数
    public void realGame() {

        int num = ;
        int flag = ; 
        do {
            menu();
            flag = scanf();
            switch(flag) {
                case : {// 开始游戏
                    int random = this.random();
                    System.out.println(random);
                    num = this.scanf();
                    while(message(compare(num, random)) == ) {
                        num = this.scanf();
                    }
                    flag = ; 
                }
                break;
                case : {
                    flag = ;
                    System.out.println("游戏退出");
                }
                break;
                default: {
                    System.out.println("请输入正确选项");
                    flag = ;
                }
                break;
            }
        }while(flag == );
    }
}

class PlayGame {
    // 在构造方法里打印游戏开始菜单

    private Game game = new Game();
    // 调用Game类中的游戏方法
    public void start() {
        game.realGame();
    }


}

public class Day5 {
    public static void main(String[] args) {
        /* System.out.println("input");

        if( str.equals("0")) {
            System.out.println(str);
        } else {
            System.out.println("hah");
        } */
        PlayGame game = new PlayGame();
        game.start();
    }
}