猜字游戏java_Java 猜字谜游戏

package fundmental_excise6;

import java.util.Arrays;

import java.util.Scanner;

/**

* @author : jeasion

* @date 创建时间:2019年4月10日 上午9:35:57

* @name 猜字母游戏——实现游戏等级

* @comment 为猜字母游戏添加游戏等级。游戏等级设为三等:5、7和9,代表所需要猜测的字母个数。 游戏 开始时,由玩家选择游戏等级(5,7,9)。

* 如果选择7,则会随机产生7个字符, 然后玩家输入一个字符串包含7个字符,看这7个字符和随机产生的7个字符比较, 看是否正确,并统计分数。

* 另外,如果输入其它,重新提示输入游戏等级

* @return * A-Z 65-90 a-z 97-122

*/

public class GuessingWord {

// 主函数

public static void main(String[] args) {

int degree = 5;// 难度

int[] statistics = new int[2];

GuessingWord guessingWord = new GuessingWord();

degree = guessingWord.degree();// 获取难度

char[] ch = guessingWord.generateString(degree);// 生成字符串,并将字符串存储

System.out.println("字符串:" + Arrays.toString(ch));

statistics = guessingWord.regx(ch, degree, statistics);

while (statistics[1] != degree) {

System.out.println("再猜");

statistics = guessingWord.regx(ch, degree, statistics);

}

guessingWord.score(statistics[0]);

}

// 游戏等级

public int degree() {

int degree;

Scanner scanner = new Scanner(System.in);

System.out.print("请选择游戏等级5 7 9:");

degree = scanner.nextInt();

while (degree != 5 && degree != 7 && degree != 9) {

System.out.println("抱歉,你输入的游戏等级有误,请重新输入");

degree = scanner.nextInt();

}

return degree;

}

// 生成字符

public char[] generateString(int degree) {

char[] ch = new char[degree];

/*

* 构建一个从0-25的数组, 然后将其中的值加65赋值给char

* 里面的数只要被用过就将其赋值为1000 保证char不会取到重复的值

*/

int[] nums = new int[26];

for (int i = 0; i < nums.length; i++) {

nums[i] = i;

}

for (int i = 0; i < degree; i++) {

int temp = 0;

do {

temp = (int) (Math.random() * 26);

ch[i] = (char) (65 + temp);

} while (nums[temp] == 1000);

nums[temp] = 1000;

}

return ch;

}

// 字符匹配

public int[] regx(char ch[], int degree, int[] statistics) {

Scanner scanner = new Scanner(System.in);

char[] inputChar = new char[ch.length];

int pos = 0;

int num = 0;

// 获取用户输入的字符串,并将全部字符转换成大写字母,方便统计

System.out.println("请输入你的字符串");

String string = scanner.nextLine().toUpperCase();

// 用户输入的字符串长度和难度不同,要求用户重新输入

while (string.length() != degree) {

System.out.println("字符长度不正确,请重新输入");

string = scanner.next().toUpperCase();

}

// 将用户输入的字符串拆分成字符数组

for (int i = 0; i < inputChar.length; i++) {

inputChar[i] = string.charAt(i);

}

//System.out.println("你输入的字符数组:" + Arrays.toString(inputChar));

//System.out.println("原字符数组:" + Arrays.toString(ch));

// 进行字符串匹配

for (int i = 0; i < inputChar.length; i++) {

for (int j = 0; j < inputChar.length; j++) {

if (ch[i] == inputChar[j]) {

num++;

if (i == j) {

pos++;

break;

}

}

}

}

statistics[0]++; // 获取次数

statistics[1] = num;// 获取猜对的个数

System.out.println("你猜对了" + num + "个字符," + "其中" + pos + "个字符位置正确,总次数=" + statistics[0] + "\t");

return statistics;

}

// 分数统计

public void score(int count) {

System.out.println("你一共猜了" + count + "次,得分:" + (500 - count * 10));

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值