猜数游戏(Java)

一、效果图

二、源代码

package com.tang;

import java.util.Scanner;

public class CaiShu {

	public static void main(String[] args) {
		System.out.println("欢迎来到数字 猜一猜");
		System.out.println("给你 10次 机会");
		System.out.println("你能猜出我心里的数字吗?");
		System.out.println("范围0—99的整数,开始吧!!哈哈,");

		Scanner input = new Scanner(System.in);// 获取从键盘输入的数字

		int number = (int) (Math.random() * 100); // 产生随机数

		int guess;// 用户猜的数字

		int count = 0;// 猜测次数

		// 用户猜测随机数

		do {

			guess = input.nextInt();

			if (number < guess) {

				System.out.println("真不好意思,你猜大了!!哈哈");

				count++;

			} else if (number > guess) {

				System.out.println("Sorr!猜小了哦!继续加油!!");

				count++;

			} else {

				count++;

				break;

			}

		} while (true);

		System.out.println("这个数字是" + number);

		System.out.println("您猜的次数是" + count);

		// 根据猜测次数给出评价

		if (count == 1) {// 第一次就猜对

			System.out.println("你太聪明了!居然能猜透我的心思!!");

		} else if (count >= 2 && count <= 5) {// 五次及以内猜对

			System.out.println("还蛮聪明的嘛,敢不敢再来挑战一次??");

		} else if (count >= 6 && count <= 10) {// 十次及以内猜对

			System.out.println("恭喜你,猜对啦!!");

		} else {// 十次都猜错了
			System.out.println("一点都不了解我,不和你玩了");
		}

	}

}

 三、本人QQ:768946914,欢迎添加!! 

猜你喜欢

转载自blog.csdn.net/weixin_42449711/article/details/81124457