Get the App
SLTechnology News&Howtos  ›  Development  › 

How to realize word-guessing Mini Game by Java

Shulou Source: shulou.com Published: 2022-06-03 09:47:28 09月11日 Update

This article mainly introduces Java how to achieve word-guessing Mini Game, the article is very detailed, has a certain reference value, interested friends must read it!

The specific code is as follows:

Package test07;import java.util.Scanner;// guessing character game public class Guessing {/ / main method public static void main (String [] args) {Scanner scan = new Scanner (System.in); char [] chs = generate (); / / get random character array System.out.println (chs); / / cheat int count = 0; / / number of guesses while (true) {/ / self-made loop System.out.println ("guess!") String str = scan.nextLine (). ToUpperCase (); / / receive the string entered by the user and convert it to uppercase if (str.equals ("EXIT")) {/ / determine whether the string content is EXITSystem.out.println ("come again!"); break;} char [] input = str.toCharArray (); / / convert the string to a character array int [] result = check (chs,input) / / comparison: the random character array and the character array entered by the user if (result [0] = = chs.length) {/ / a pair of int score = 100 characters, deducting 10 points for each wrong guess ("Congratulations on your guess right, score:" + score); break;} else {/ / wrong count++ / / increase the number of error guesses by 1System.out.println ("the number of character pairs is" + result [1] + ", the number of position pairs is" + result [0]);} / / generate a random character array chspublic static char [] generate () {char [] chs = new char [5] / / Random character array char [] letters = {'A','B','C','D','E','F','G','Ha','I','J'C','L','M','N','O','P','Q','R','S','T','U','V' 'Xray, 'Yee,' Z'} / / Random character range array boolean [] flags = new boolean [letters.length]; / / tagged array for (int iCharacter I

Tags: Character array subscript number position tag user input content string Mini Game element number article loop generation right between code value Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi Shulou Technology Shulou Tech Info Docker MariaDB