小游戏项目

小游戏项目说明:

这是一个猜数字的小游戏。本案例使用了多级菜单的相互调用、正则表达式的简单使用、循环判断语句的使用。以及返回菜单的使用。

本案例是使用jdk1.8实现的。

1.系统启动类(StartSMS)

2.游戏菜单类(GameMenu)

3.游戏主程序类(GuessGame)


StartSMS类

package cn.jason;

/**
 * 这是游戏的启动菜单
 * 
 * @blog http://blog.csdn.net/yan_yuhan
 * @author Jason
 * @2016年9月10日 下午9:34:00
 */
public class StartSMS {
	public static void main(String[] args) {
		// 创建菜单对象
		GameMenu gm = new GameMenu();
		// 调用主菜单
		gm.mainMenu();
	}
}

GameMenu类

package cn.jason;

import java.io.IOException;
import java.util.Scanner;

/**
 * 这是游戏的菜单
 * @blog http://blog.csdn.net/yan_yuhan
 * @author Jason
 * @2016年9月10日 下午9:06:43
 */
public class GameMenu {

	/**
	 * 1.初级模式可以玩50次 
	 * 2.中级模式可以玩25次 
	 * 3.终极模式只能玩5次,而且没有任何提示
	 */
	public void mainMenu() {
		System.out.println("------Welcom enter the guess number of game------\n");
		System.out.println("\t1:Primary Model\n");
		System.out.println("\t2:Middle Model\n");
		System.out.println("\t3:Ultimate Model\n");
		System.out.println("\t4:Exist");
		System.out.println("请选择:");
		secondMenu();
	}
	/**
	 * 这是游戏的二级菜单
	 */
	public void secondMenu() {
		Scanner sc = new Scanner(System.in);
		// 定义选择标记
		boolean flag = false;
		// 对选择进行判断,并进行相应操作
		do {
			System.out.println("please input your choice: ");
			String choice = sc.nextLine();
			
			switch (choice) {
			case "1":
				GuessGame.gameRules();
				GuessGame.game(50);
				break;
			case "2":
				GuessGame.gameRules();
				GuessGame.game(25);
				break;
			case "3":
				GuessGame.gameRules();
				GuessGame.game(5);
				break;
			case "4":
				System.out.println("thank you for vist.");
				break;
			default:
				System.out.println("Your input doesn't conform to the requirements, please try again.");
				flag = true;
				break;
			}

		} while (flag);

	}

	/**
	 * 这是返回游戏的方法
	 * @param count
	 * 			  游戏次数
	 */
	public void returnGame(int count) {
		Scanner sc = new Scanner(System.in);
		// 输入提示
		System.out.println("continue or exist?y/n");
		//接收键盘录入的字符串
		String receivedData = sc.nextLine();
		// 对字符串进行判断
		if (receivedData.equalsIgnoreCase("y"))
			GuessGame.game(count);
		else
			System.out.println("the game is over.thank your participation.");
	}
	/**
	 * 这是返回主菜单的方法
	 */
	public void returnMainMenu() {
		Scanner sc = new Scanner(System.in);
		//输入提示
		System.out.println("Do you want to return main menu? y/n");
		//接收键盘录入的字符串
		String receivedData = sc.nextLine();
		// 对字符串进行判断
		if (receivedData.equalsIgnoreCase("y"))
			mainMenu();
		else
			System.out.println("thank your participation.");
	}
}

GuessGame类

package cn.jason;

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

/**
 * @blog http://blog.csdn.net/yan_yuhan
 * @author Jason
 * @2016年9月10日 下午9:06:43
 */
public class GuessGame {
	/**
	 * 游戏规则
	 * 本游戏是一个猜数字的小游戏。
	 * 游戏次数是count次。 当你被要求输入数据时,你可以随便输入你想要输入的数字。
	 * 游戏知道你猜中时结束。控制台会立刻显示你输入后的结果,游戏结果有三种:
	 * 猜大了,猜小了,猜对了。
	 * 
	 */
	public static void gameRules() {
		System.out.println("\t\t\tThis is a game that you can guess number。");
		System.out.println("Game rules: ");
		System.out.println("The total of times is 'count times'.when you are required to input a data,");
		System.out.println(
				"you can input a random number between 1~count.the game cannot be over until you guess right .");
		System.out.println(
				"the console can prompt your result,the game's result has three ,guess big,guess small,guess right.\n");

	}

	/**
	 * 游戏主程序1,有猜大猜小提示
	 * @param count 
	 * 			  游戏的次数
	 */
	public static void game(int count) {

		Scanner sc = new Scanner(System.in);
		// 创建随机数对象
		Random random = new Random();
		// 设置随机数范围
		int randomNumber = random.nextInt(count) + 1;
		/**
		 * x为次数,总共count次。
		 * y是你已经猜了多少次 。
		 * time为直到你猜对为止一共猜了多少次。
		 */
		for (int x = 1, y = count, time = 0; x <= count; x++) {
			System.out.println("please input a data between 1~" + count + " what you want:");
			// 接收键盘录入的字符串
			String receivedNumber1 = sc.nextLine();
			// 对输入的字符串进行校验
			checkInput(receivedNumber1, count);
			// 把数字字符串转换成int类型
			int receivedNumber = Integer.parseInt(receivedNumber1);
			time++;
			y--;
			
			//对录入的数字进行判断
			if (receivedNumber >= 1 && receivedNumber <= count) {
				if (receivedNumber > randomNumber) {
					if (y != 0)
						System.out.println("guess a little,please one more time. you have " + y + " chances.");
					else
						System.out.println("sorry,the game is over.");
				}

				else if (receivedNumber < randomNumber) {
					if (y != 0)
						System.out.println("guess a bigger,please one more time. you have " + y + " chances.");
					else
						System.out.println("guess a bigger,sorry,the game is over.");
				}

				else {
					System.out.println("congulations,you guess completely right.");
					System.out.println("you guess a total of the times is " + time + " times.");
					// 创建菜单对象
					GameMenu gm = new GameMenu();
					//返回
					gm.returnGame(count);
					gm.returnMainMenu();
					break;
				}
			} else
				System.out.println("Your input doesn't conform to the requirements, please try again.");
		}

	}

	// 游戏程序1,有猜大猜小提示
	public static void game2(int count) {

		Scanner sc = new Scanner(System.in);
		// 创建随机数对象
		Random random = new Random();
		// 设置随机数范围
		int randomNumber = random.nextInt(count) + 1;
		/**
		 * x为次数,总共count次。y是你已经猜了多少次 time为直到你猜对为止一共猜了多少次
		 */
		for (int x = 1, y = count, time = 0; x <= count; x++) {
			System.out.println("please input a data between 1~" + count + " what you want:");
			// 接收键盘录入的字符串
			String receivedNumber1 = sc.nextLine();
			// 对输入的字符串进行校验
			checkInput(receivedNumber1, count);
			// 把数字字符串转换成int类型
			int receivedNumber = Integer.parseInt(receivedNumber1);
			time++;
			y--;
			
			对录入的数字进行判断
			if (receivedNumber >= 1 && receivedNumber <= count) {
				if (receivedNumber > randomNumber) {
					if (y != 0)
						System.out.println("please one more time. you have " + y + " chances.");
					else
						System.out.println("sorry,the game is over.");
				}

				else if (receivedNumber < randomNumber) {
					if (y != 0)
						System.out.println("please one more time. you have " + y + " chances.");
					else
						System.out.println("sorry,the game is over.");
				}

				else {
					System.out.println("congulations,you guess completely right.");
					System.out.println("you guess a total of the times is " + time + " times.");
					// 创建菜单对象
					GameMenu gm = new GameMenu();
					gm.returnGame(count);
					gm.returnMainMenu();
					break;
				}
			} else
				System.out.println("Your input doesn't conform to the requirements, please try again.");
		}

	}

	/**
	 * 对输入的内容进行校验
	 */
	public static void checkInput(String data, int count) {
		// 定义正则表达式,匹配数字字符串
		String regex = "[1-9]?\\d{1,2}";
		// 对数字字符串进行判断
		if (!data.matches(regex)) {
			System.out.println("Your input doesn't conform to the requirements, please try again.");
			game(count);
		}
	}

}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值