Hangman Java游戏打印错误和正确的猜测
我正在使用我的第一种编程语言Java,但我很难正确地猜出要正确打印的内容。当输入错误的字母时,它会打印“错误!”,但当猜到正确的字母时,它会同时打印“正确!”和“错误!”……任何帮助都将受到感谢。谢谢。:)Hangman Java游戏打印错误和正确的猜测,java,arrays,loops,for-loop,while-loop,Java,Arrays,Loops,For Loop,While Loop,我正在使用我的第一种编程语言Java,但我很难正确地猜出要正确打印的内容。当输入错误的字母时,它会打印“错误!”,但当猜到正确的字母时,它会同时打印“正确!”和“错误!”……任何帮助都将受到感谢。谢谢。:) 这些单词是从数组中提取出来的 import java.util.Scanner; import java.util.Random; public static void main(String[] args) { System.out.println("Welcome to H
这些单词是从数组中提取出来的
import java.util.Scanner;
import java.util.Random;
public static void main(String[] args) {
System.out.println("Welcome to Hangman! Below you can find the rules of the game.");
//Welcome message
System.out.println();
//Spacing
System.out.println("How to play:");
System.out.println();
//Spacing
System.out.println("Hangman is a word guessing game. Guess the word by suggesting letters within a certain number of guesses.");
System.out.println("The word to guess is represented by a row of dashes, representing each letter of the word.");
System.out.println("If you guess a letter correctly, it replaces a dash and you can continue to guess until the word is uncovered.");
System.out.println("However, if you guess wrong you lose a try. You have a maximum of 5 tries. After guessing 10 words, you win! Good luck!");
//Rules of game
System.out.println();
//Spacing
Scanner in = new Scanner (System.in);
//Scanner created
String[] intro = {"after", "think", "could", "thank", "round", "clump", "plane", "beach", "towel", "tiger", "goat", "monkey", "house"};
String[] beginner = {"around", "amount", "grown", "focus", "cedar", "flute", "unicorn", "fruit", "basket", "burger", "chips", "juice"};
String[] intermediate = {"about", "bring", "clean", "mother", "locker", "hunter", "drink", "eight", "spray", "untie", "cents"};
String name;
int age;
//Variables initialized
System.out.println("Please enter your name:");
//Prompt for name
name = in.next();
//Input for name
System.out.println("Please enter your age:");
//Prompt for age
age = in.nextInt();
//Input for age
if (age >= 4 && age < 7){
System.out.println("You have been placed in the Intro level!");
Random rand = new Random();
//Random created
int randomWord = rand.nextInt(12)+0;
//Stores the position of array
String word2guess = intro[randomWord];
//String is the value stored in the generated position of array
char[] word2Char = word2guess.toCharArray();
//Stores the word in a char array
char[] wordLength = new char [word2Char.length];
//Creates an array for word length
for (int i = 0; i < word2Char.length; i++){
wordLength[i] = '_';
}
//Replaces each letter of the word with an underscore
for (int i = 0; i < word2Char.length; i++){
System.out.print(wordLength[i] + " ");
}
//Prints the word in underscore format with a space between each letter
int wrongGuesses = 0;
int correctGuesses = 0;
boolean letterFound = false;
boolean nextWord = true;
while (nextWord){
while(correctGuesses != word2Char.length){
System.out.println();
//Spacing
System.out.println("Please enter a letter:");
char guess = in.next(".").charAt(0);
//Input for only ONE letter
for (int i = 0; i < word2Char.length; i++){
if (guess == word2Char[i]){
wordLength[i] = word2Char[i];
letterFound = true;
break;
}else{
letterFound = false;
}
}
if(letterFound == false && wrongGuesses <= 4){
wrongGuesses++;
System.out.println("Wrong! You have "+ (5 - wrongGuesses)+" attempts remaining. Please try again.");
}else{
correctGuesses++;
System.out.println("Correct!");
}
for (int i = 0; i < word2Char.length; i++){
System.out.print(wordLength[i] + " ");
}
System.out.println();
//Spacing
if (correctGuesses == word2Char.length){
System.out.println("Congratulations! You guessed the word. The word was: "+word2guess);
nextWord = true;
}
if (wrongGuesses == 5){
System.out.println();
//Spacing
System.out.println("Game Over! You have no attempts remaining. The word was: "+word2guess);
nextWord = false;
break;
}
}
import java.util.Scanner;
导入java.util.Random;
公共静态void main(字符串[]args){
System.out.println(“欢迎来到刽子手!下面你可以找到游戏规则。”);
//欢迎辞
System.out.println();
//间距
System.out.println(“如何播放:”);
System.out.println();
//间距
System.out.println(“Hangman是一种猜字游戏。通过在一定的猜测次数内建议字母来猜单词。”);
System.out.println(“要猜测的单词由一行破折号表示,表示单词的每个字母。”);
System.out.println(“如果你猜对了一个字母,它会取代破折号,你可以继续猜,直到单词被揭开为止。”);
System.out.println(“但是,如果你猜错了,你会失败一次尝试。你最多有5次尝试。在猜了10个单词后,你赢了!祝你好运!”);
//游戏规则
System.out.println();
//间距
扫描仪输入=新扫描仪(系统输入);
//扫描仪创建
String[]intro={“after”,“think”,“can”,“thank”,“round”,“clump”,“plane”,“beach”,“tob”,“tiger”,“goat”,“monkey”,“house”};
字符串[]初学者={“周围”、“数量”、“成长”、“专注”、“雪松”、“长笛”、“独角兽”、“水果”、“篮子”、“汉堡”、“薯条”、“果汁”};
字符串[]中间={“关于”、“带来”、“干净”、“母亲”、“储物柜”、“猎人”、“饮料”、“八”、“喷雾”、“解开”、“美分”};
字符串名;
智力年龄;
//变量初始化
System.out.println(“请输入您的姓名:”);
//提示输入姓名
name=in.next();
//输入名称
System.out.println(“请输入您的年龄:”;
//提示年龄
年龄=in.nextInt();
//年龄投入
如果(年龄>=4岁和年龄<7岁){
System.out.println(“您已进入入门级!”);
Random rand=新的Random();
//随机创建
int randomWord=rand.nextInt(12)+0;
//存储数组的位置
字符串word2guess=intro[randomWord];
//字符串是存储在数组生成位置的值
char[]word2Char=word2guess.toCharArray();
//将单词存储在字符数组中
char[]wordLength=新字符[word2Char.length];
//创建字长数组
for(int i=0;i
for (int i = 0; i < word2Char.length; i++){
if (guess == word2Char[i]){
wordLength[i] = word2Char[i];
letterFound = true;
correctGuesses++;
System.out.println("Correct!");
}
}
if(!letterFound){
wrongGuesses++;
System.out.println("Wrong!");
}
for(int i=0;i
需要这样做:
for (int i = 0; i < word2Char.length; i++){
if (guess == word2Char[i]){
wordLength[i] = word2Char[i];
letterFound = true;
break;
}else{
letterFound = false;
}
}
if(letterFound == false){
wrongGuesses++;
System.out.println("Wrong!");
}else{
correctGuesses++;
System.out.println("Correct!");
}
for(int i=0;i
问题是,在for循环中,即使letterFound
为真,循环也会继续检查每个字符。只有有人猜对了最后一个字符,循环才会起作用。因此,我所做的是添加一个break
语句,这样每当letterFound
为真时,for循环就会退出。这段代码:
for (int i = 0; i < word2Char.length; i++){
if (guess == word2Char[i]){
wordLength[i] = word2Char[i];
letterFound = true;
correctGuesses++;
System.out.println("Correct!");
}
}
if(!letterFound){
wrongGuesses++;
System.out.println("Wrong!");
}
for(int i=0;i
需要这样做:
for (int i = 0; i < word2Char.length; i++){
if (guess == word2Char[i]){
wordLength[i] = word2Char[i];
letterFound = true;
break;
}else{
letterFound = false;
}
}
if(letterFound == false){
wrongGuesses++;
System.out.println("Wrong!");
}else{
correctGuesses++;
System.out.println("Correct!");
}
for(int i=0;i
问题是,在for循环中,即使letterfind
为真,循环也会继续检查每个字符