黑马程序员技术交流社区

标题: 今天学习的一个代码小游戏,可以复制运行玩一下. [打印本页]

作者: 淡忘、悲年华    时间: 2015-9-13 21:19
标题: 今天学习的一个代码小游戏,可以复制运行玩一下.
/*
猜数字小游戏(数据在1-100之间) .   你们猜了几次猜中的,回复.
*/
import java.util.Scanner;
class Test1_GuessNum {
        public static void main(String[] args) {
                Scanner sc = new Scanner(System.in);                                //创建键盘录入对象
                System.out.println("请输入一个整数,范围在1-100之间");
                int guessNum = (int)(Math.random() * 100) + 1;                //心里想的随机数
                while (true) {                                                        //因为需要猜很多次,所以用无限循环
                        int result = sc.nextInt();                                //大家猜的数
                        if (result > guessNum) {                                //如果你们猜的数大于了我心里想的数
                                System.out.println("大了");                        //提示大了
                        } else if (result < guessNum) {                        //如果你们猜的数小于了我心里想的数
                                System.out.println("小了");                        //提示小了
                        } else {                                                        //如果既不大也不小
                                System.out.println("中了");                                        //中了
                                break;
                        }
                }
        }
}
作者: silencea    时间: 2015-9-13 21:51
真好玩!
作者: a804876583    时间: 2015-9-13 22:19
import java.util.Scanner;
class Day07_Math2 {
        public static void main(String[] args) {
                Scanner sc = new Scanner(System.in);
                int x = (int)(Math.random() * 100 + 1 );
                System.out.println("输入你猜的数字");
                int a = sc.nextInt();

                while (true) {
                        //int a = sc.nextInt();
                        int b = a;
                        if (a > x) {
                                System.out.println("大了");
                                while (true) {
                                a = sc.nextInt();
                                if (a >= b) {
                                        System.out.println("你是不是傻?都说大了");
                                }else {
                                        break;
                                }
                                }
                        }else if (a < x){
                                System.out.println("小了");
                                while (true) {
                                a = sc.nextInt();
                                if (a <= b) {
                                        System.out.println("你是不是傻?都说小了");
                                }else {
                                        break;
                                }
                                }
                        }else {
                                System.out.println("中了");
                                break;
                        }
                }

        }
}
/* 需求:猜数字小游戏(数据在1-100之间)*/
我改了
作者: 然后呢8908    时间: 2015-9-13 22:35
写得不错哦
作者: purgatory    时间: 2015-9-13 23:05
哇 好有趣 我玩了好几天都上瘾了




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2