java Realization of Alphabet Guessing Game

  • 2021-09-05 00:02:21
  • OfStack

This article example for everyone to share java to achieve the specific code of the letter game, for your reference, the specific content is as follows

Case requirements:

StepOne:

The system randomly generates a set of random character arrays (no repetition)

StepTwo:

The console enters 1 set of strings to compare with the system-generated character array

StepThree:

Prompt the correct number of characters and the correct number of alignment if the console input is inconsistent with the random generated by the system

StepFour:

If the console input and the system randomly generated complete 1 to prompt "Congratulations you guessed right, the end of the game" program exit. If it is not completely 1, you need 1 direct guess

StepFive:

If EXIT is entered in the console, the "Game Termination" program will be prompted to exit

StepSix:

If the length of the array input by the console does not match, it will prompt "young people don't talk about martial arts, the length does not match, and the mouse tail juice!"

Specific code implementation:


package game;

import java.util.Scanner;

public class GuessLetters {
 /**
 * 将随机生成1个字母数组的逻辑封装成1个方法
 **/
 public static char[] Nonce(){
 char[] random=new char[5];//不重复的随机字母数组,长度5
// 存储可能出现的字母的char数组(库)
 char[] warehouse={
 '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'
 };
// 创建1个boolean类型的变量(长度是warehouse的长度)来记录使用过的warehouse数组的元素
 boolean[] warehouseOne=new boolean[warehouse.length];
 for (int i=0;i<random.length;i++){//遍历random数组中的每个元素
 int index;
 do {//随机warehouse长度的下标
// 创建1个index变量随机生成warehouse数组长度的下标
 index=(int)(Math.random()*warehouse.length);
 }while (warehouseOne[index]==true);
// 将warehouse[index]中的元素赋值给random[i]元素
 random[i]=warehouse[index];
// 在boolean类型的warehouseOne数组中对应的index元素改为true(以做标记)
 warehouseOne[index]=true;
 }
 return random;
 }
 /**
 *将(判断系统随机的char类型字母数组和用户输入的字符串对比,并输出字母正确个数和对位正确个数)的逻辑封装成方法
 **/
 public static int[] contrast(char[] random, char[] input){
 int[] result=new int[2];//result[0]字母对个数,result[1]对位对个数
 for (int i=0;i<input.length;i++){//遍历input数组
 for (int j=0;j<random.length;j++){//遍历random数组
 if (input[i]==random[j]){//对比字符对对错
 result[0]++;//字符正确个数+1
 if (i==j){//对比对位对错
 result[1]++;
 }
 break;//当字符对时 无需继续对比后面的元素,跳出循环。
 }
 }
 }
 return result;
 }
 /*主方法(main)中整理且运行封装好的方法*/
 public static void main(String[] args) {
// 调用random方法,来产生1个随机的char字符数组
 char[] chs=Nonce();
 System.out.println(chs);//作弊
// 控制台输入
 Scanner sc = new Scanner(System.in);
// 创建1个变量result数组存储字符正确,对位正确个数
// 错误次数
 int mistake=0;
 do {//需求当没有猜对时,1直猜
// 友好提示
 System.out.println("请输入5个不重复的字母");
// 接受输入的字符串inputOne将inputOne转换成大写
 String inputOne = sc.next().toUpperCase();
// 需求:控制台输入EXIT,则程序退出
 if ("EXIT".equals(inputOne)){//判断接收的字符是否是EXIT
 break;//跳出循环,程序结束
 }
 // 将字符串类型数组转换成char类型数组
 char[] input = inputOne.toCharArray();
// 需求:控制台如若输入的字符长度与随机字符长度不符则友好提示
 if (result[2]!=chs.length){//判断长度是否1致
// 调用对比逻辑的方法contrast
 int[] result = contrast(chs, input);
// 需求:如果猜对了,则友好提示,且程序终止
 if (result[1]==chs.length) {//判断对位正确个数是否与随机的数组长度1致
// 友好提示
 System.out.println("恭喜你猜对了,游戏结束!");
 break;//条数循环,程序结束
 }else{
// 输出结果(字符正确个数,对位正确个数)
 System.out.println("字符正确个数:" + result[0] + "\t对位正确个数:" + result[1]+"\n继续猜");
// 需求:记录错误次数
 mistake++;//错误次数+1
 }
 }else{
// 友好提示
 System.out.println("年轻人不讲武德,长度不符,耗子尾汁");
 }
 }while (true);
 }
}

Related articles: