简单的猜数字小游戏 (java编程)

package cn;

import java.util.InputMismatchException;
import java.util.Scanner;

public class test2 {
	public static void main(String[] args) {
		//生成一个随机数
		int number = (int) (Math.random()*100)+1;
		//记录输入的次数
		int count=0;
		//记录随机数最大值和最小值 (随机数范围)
		int min=1;
		int max=100;
		while(true) {
			//键盘录入数据
			System.out.println("请输入你要猜的数据:("+min+"~"+max+")");
			Scanner s=new Scanner(System.in);
			int GuessNumber = s.nextInt();
			count++;
			try {		
				//判断
				if (GuessNumber > number) {
					max = GuessNumber;
					System.out.println("你猜大了");
				} else if(GuessNumber < number) {
					min = GuessNumber;
					System.out.println("你猜小了");
				} else {
					System.out.println("恭喜你,花了"+count+"就猜中了"+"\n"+"请问还要继续吗?(yes)");
					s=new Scanner(System.in);
					String str=s.nextLine();
					if ("yes".equals(str)) {
						//重新赋值随机数
						number = (int) (Math.random()*100)+1;
						count=0;
						min=1;
						max=100;
					} else {
						break;
					}
				}
			}catch (InputMismatchException e) {
				System.out.println("你输入的数据有误");
			}
			
		}
	}
}

你可能感兴趣的:(每天都要练,java)