java实战之猜字小游戏
作者:buptwhq 时间:2022-03-31 14:48:01
一、题目描述
二、思路
语法基础:StringBuilder 类似列表,可以更改元素。
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('a', 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) { // 猜中,但重复,不算错误次数
System.out.println("\t" + letter + " is already in the word");
} else if (ans.indexOf(letter) < 0) { // 猜错
System.out.println("\t" + 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 <= 1) ? " time" : " times"));
guessed[index] = true;
num_guessed += 1;
if(num_guessed < words.length)
{
System.out.print("Do you want to guess for another word? Enter y or n> ");
keep = scanner.nextLine().charAt(0);
}
}while(keep == 'y' && num_guessed < words.length);
if(keep == 'y')
System.out.println("Perfect!!!");
else
System.out.println("You have guessed " + num_guessed + ((num_guessed <= 1) ? " word~" : " words~"));
}
}
四、效果图
全部猜完
中途退出
来源:https://blog.csdn.net/whq___/article/details/115861711
标签:java,猜字,游戏
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
java获取包下被指定注解的类过程解析
2023-08-08 11:12:27
Java将json对象转换为map键值对案例详解
2023-11-28 07:38:43
![](https://img.aspxhome.com/file/2023/9/60129_0s.png)
Springboot使用POI实现导出Excel文件示例
2021-09-22 08:18:31
![](https://img.aspxhome.com/file/2023/8/61398_0s.jpg)
Java设计模式之命令模式详解
2023-11-15 21:18:43
DUCC配置平台实现一个动态化线程池示例代码
2023-11-28 12:07:39
![](https://img.aspxhome.com/file/2023/4/60354_0s.png)
Java数据结构之链表、栈、队列、树的实现方法示例
2021-10-07 10:40:29
解决spring boot启动扫描不到自定义注解的问题
2023-10-29 14:31:48
简单记事本java源码实例
2023-11-26 02:03:17
![](https://img.aspxhome.com/file/2023/4/59744_0s.jpg)
java设计模式学习之工厂方法模式
2023-10-12 17:19:04
![](https://img.aspxhome.com/file/2023/7/58607_0s.jpg)
Java StringUtils字符串分割转数组的实现
2023-07-19 12:43:37
Java实现特定范围的完数输出算法示例
2023-11-29 00:43:45
![](https://img.aspxhome.com/file/2023/3/60163_0s.jpg)
eclipse中maven插件安装教程
2023-06-23 17:08:23
![](https://img.aspxhome.com/file/2023/2/62972_0s.png)
Java实现将每日新闻添加到自己博客中
2023-02-08 16:20:43
![](https://img.aspxhome.com/file/2023/7/62527_0s.jpg)
java语言实现猜数字游戏
2023-11-24 00:19:38
![](https://img.aspxhome.com/file/2023/6/59206_0s.jpg)
java中的String定义的字面量最大长度是多少
2023-09-01 08:35:31
![](https://img.aspxhome.com/file/2023/9/61959_0s.png)
servlet实现文件上传、预览、下载、删除功能
2023-11-16 21:52:48
![](https://img.aspxhome.com/file/2023/0/59070_0s.png)
轻松学习C#的方法
2023-06-21 13:22:32
![](https://img.aspxhome.com/file/2023/3/66573_0s.jpg)
Eclipse的Debug调试技巧大全(总结)
2023-11-25 06:14:06
![](https://img.aspxhome.com/file/2023/3/59993_0s.png)
Java实现简单文件过滤器功能
2021-09-13 23:27:58
![](https://img.aspxhome.com/file/2023/1/62111_0s.png)
详解java整合solr5.0之solrj的使用
2023-07-23 03:12:06
![](https://img.aspxhome.com/file/2023/3/57493_0s.png)