小遊戲項目說明:
這是一個猜數字的小遊戲。本案例使用了多級菜單的相互調用、正則表達式的簡單使用、循環判斷語句的使用。以及返回菜單的使用。
本案例是使用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);
}
}
}