热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

猜字小游戏(JAVA)

原标题:猜字小游戏(JAVA)2021.04.19题目描述:

原标题:猜字小游戏(JAVA)

2021.04.19
题目描述:
在这里插入图片描述
思路:
语法基础:StringBuilder 类似列表,可以更改元素。

package Practice;
public class tt {
public static void main(String[] args) {
String str = "banana";
System.out.println(str.indexOf文章来源站点https://www.yii666.com/('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[] guwww.yii666.comessed = 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
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 " +www.yii666.com 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 = scann文章来源地址40679.htmler.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_gue文章来源地址40679.htmlssed <= 1) ? " word~" : " words~"));
}
}

效果图:
全部猜完:
在这里插入图片描述
在这里插入图片描述
中途退出:在这里插入图片描述

来源于:猜字小游戏(JAVA)


推荐阅读
author-avatar
手机用户2502905891
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有