This commit is contained in:
xiangwb
2018-04-16 14:17:57 +08:00
parent af284cc899
commit 806003cc07
3 changed files with 127 additions and 1 deletions
@@ -0,0 +1,88 @@
package com.xwbing.demo;
import java.util.Scanner;
/**
* 项目名称: boot-module-demo
* 创建时间: 2017/4/16 14:10
* 作者: xiangwb
* 说明: 猜字谜游戏
*/
public class GuessGame {
public static void main(String[] args) {
// 表示玩家猜测的次数
int count = 0;
// 用于保存判断的结果
int[] result = new int[2];
Scanner scanner = new Scanner(System.in);
System.out.println("GuessingGame>欢迎尝试猜字母游戏!");
// 表示猜测的字符串
char[] chs = generate();
System.out.println(chs);
System.out.println("GuessingGame>游戏开始,请输入你所猜的5个字母序列:(exit——退出)");
while (true) {//自造死循环
String inputStr = scanner.next().trim().toUpperCase();
if ("EXIT".equals(inputStr)) {
System.out.println("GuessingGame>谢谢你的尝试,再见!");
break;
}
char[] input = inputStr.toCharArray();
result = check(chs, input);
if (result[0] == chs.length) {// 完全猜对的情况
int score = 100 * chs.length - count * 10;
System.out.println("GuessingGame>恭喜你猜对了!你的得分是:" + score);
break;
} else {
count++;
System.out.println("GuessingGame>你猜对" + result[1] + "个字符,其中" + result[0] + "个字符的位置正确!(总次数=" + count + ",exit——退出)");
}
}
scanner.close();
}
/**
* 随机生成需要猜测的字母序列
*
* @return 存储随机字符的数组
*/
public static char[] generate() {
char[] letters = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V',
'W', 'X', 'Y', 'Z'};
boolean[] flags = new boolean[letters.length];
char[] chs = new char[5];
for (int i = 0; i < chs.length; i++) {
int index;
do {
index = (int) (Math.random() * (letters.length));
} while (flags[index]);// 判断生成的字符是否重复
chs[i] = letters[index];
flags[index] = true;
}
return chs;
}
/**
* 比较玩家输入的字母序列和程序所生成的字母序列,逐一比较字符及其位置,并记载比较结果
*
* @param chs 程序生成的字符序列
* @param input 玩家输入的字符序列
* @return 存储比较的结果。返回值int数组 的长度为2,其中,索引为0的位置
* 用于存放完全猜对的字母个数(字符和位置均正确),索引为1的位置用于存放猜对的字母个数(字符正确,但是位置不正确)。
*/
public static int[] check(char[] chs, char[] input) {
int[] result = new int[2];
for (int i = 0; i < input.length; i++) {
for (int j = 0; j < chs.length; j++) {
if (input[i] == chs[j]) {// 判断字符是否正确
result[1]++;
if (i == j) {// 判断位置是否正确
result[0]++;
}
break;
}
}
}
return result;
}
}
@@ -8,7 +8,7 @@ import java.util.*;
*/
public class PageUtil {
public static Map<String, Object> page(List list, int currentPage, int pageSize) {
Map<String, Object> pageMap = new HashMap<>();
Map<String, Object> pageMap = new HashMap<>(5);
int size = list.size();
pageMap.put("count", size);
int totalPage;
@@ -0,0 +1,38 @@
package com.xwbing.util;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
/**
* 说明: PropertiesUtil
* 创建时间: 2017/5/12 16:50
* 作者: xiangwb
*/
public class PropertiesUtil {
/**
* 根据Key读取Value
*
* @param key key
* @param target 地址
* @return
*/
public static String getValueByKey(String key, String target) {
Properties pps = new Properties();
String filePath = PropertiesUtil.class.getClassLoader().getResource(target).getPath();
try {
InputStream in = new BufferedInputStream(new FileInputStream(filePath));
pps.load(in);
String value = pps.getProperty(key);
in.close();
return value;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
}