zoukankan      html  css  js  c++  java
  • java使用for循环做猜数字游戏

    package org.llh.test;

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

    /**
     * 猜数字游戏
     *
     * @author llh
     *
    public class Car {
        public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);
            int num = new Random().nextInt(100);
            System.out.print("系统已产生一个随机数字(0~99),请问是多少?");
            int num1 = scanner.nextInt();
            if (num1 >= 0 && num1 <= 99) {
                int i;
                int count = 0;
                for (i = 0; i < 100; i++) {
                    count++;
                    if (num1 > num) {
                        System.out.println("您猜的数字太大了!再猜!");
                        num1 = scanner.nextInt();
                    } else if (num1 < num) {
                        System.out.println("您猜的数字太小了!再猜!");
                        num1 = scanner.nextInt();
                    } else {
                        System.out.println("恭喜你,猜对了!");
                        System.out.println("你一共猜了" + count + "次");
                        int count1 = count + 1;
                        if (count1 == 1) {
                            System.out.println("你太厉害了,一次就猜对!");
                        } else if (count1 >= 2 && count1 <= 6) {
                            System.out.println("你很聪明!");
                        } else if (count1 > 6) {
                            System.out.println("sb,滚!猜这么多次!");
                            break;
                        }
                    }
                }

            } else {
                System.out.println("zz,请输入0~99的整数");
            }
        }

    }

    效果如图所示:


    llh
  • 相关阅读:
    在线编辑器fckeditor的使用和配置
    Oracle CHAR,VARCHAR,VARCHAR2类型的区别与使用
    where having 的区别
    软件架构师之我见
    在线编辑器fckeditor的使用和配置
    where having 的区别
    必须掌握的八个【cmd 命令行】
    浅析PHP实现同步远程MysqlPHP编程教程详解
    MSSQL智能感应
    SQL统计表行数
  • 原文地址:https://www.cnblogs.com/javallh/p/7242836.html
Copyright © 2011-2022 走看看