Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize word-guessing Mini Game by Java

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

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

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report