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-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article will explain in detail how to achieve word-guessing Mini Game with Java. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.

Train of thought

Syntax fundamentals: StringBuilder is similar to a list and can change elements.

Package Practice;public class tt {public static void main (String [] args) {String str = "banana"; System.out.println (str.indexOf ('z')); / /-1 System.out.println (str.indexOf ('ajar, 2)); / / 3 StringBuilder words = new StringBuilder (); for (int I = 0; I

< 5; i++) words.append('*'); // "*****" System.out.println(words.length()); // 5 System.out.println(words.indexOf("a"));// -1 System.out.println(words.indexOf("*", 1)); // 1 words.setCharAt(3, 'a'); // "***a*" System.out.println(words); }}代码package Practice;import java.util.Scanner;public class Guess { public static String words[] = {"banana", "telecommunication", "programming", "bupt"}; public static boolean[] guessed = new boolean[words.length]; // 判断猜过 public static int num_guessed = 0; // 猜过的单词数量 public static char keep; // 是否继续y or n public static void main(String[] args) {// for(int i = 0; i < guessed.length; i ++ ) System.out.println(guessed[i]); Scanner scanner = new Scanner(System.in); do{ // 随机产生要猜测的单词 ans int index = (int) (Math.random() * words.length); String ans = words[index]; // 再来一次时的重复检测 while(guessed[index] == true) { index = (int) (Math.random() * words.length); ans = words[index]; } // 初始化,StringBuilder类似list StringBuilder guessedWord = new StringBuilder(); for (int i = 0; i < ans.length(); i++) guessedWord.append('*'); int numberOfCorrectLettersGuessed = 0, numberOfMisses = 0; // 模拟过程 while (numberOfCorrectLettersGuessed < ans.length()) { System.out.print("(Guess) Enter a letter in word " + guessedWord + " >

"); String s = scanner.nextLine (); char letter = s.charAt (0); if (guessedWord.indexOf (letter +") > = 0) {/ / correct guess, but repeated, not counting the number of errors System.out.println ("" + letter + "is already in the word");} else if (ans.indexOf (letter))

< 0) { // 猜错 System.out.println(" " + letter + " is not in the word"); numberOfMisses++; } else { // 猜中,进行标记与赋值 int k = ans.indexOf(letter); while (k >

= 0) {guessedWord.setCharAt (k, letter); numberOfCorrectLettersGuessed++; k = ans.indexOf (letter, k + 1);} System.out.println ("The word is" + ans + ". You missed "+ numberOfMisses + ((numberOfMisses

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