Java 我正在做一个刽子手项目,我想知道一旦你完成了所有的尝试,如何让游戏停止?
这是我迄今为止编写的代码。问题是,当它问我试了多少次,我放了3次,不管怎样,它只让我试了3次,即使我猜错了或对了。尝试次数代表你猜错的次数。因此,如果我在3次尝试中没有得到单词,那么它将停止游戏。那是我的问题Java 我正在做一个刽子手项目,我想知道一旦你完成了所有的尝试,如何让游戏停止?,java,Java,这是我迄今为止编写的代码。问题是,当它问我试了多少次,我放了3次,不管怎样,它只让我试了3次,即使我猜错了或对了。尝试次数代表你猜错的次数。因此,如果我在3次尝试中没有得到单词,那么它将停止游戏。那是我的问题 import java.io.*; import java.util.*; public class Hangman { public static void main(String[] args) throws FileNotFoundException {
import java.io.*;
import java.util.*;
public class Hangman {
public static void main(String[] args) throws FileNotFoundException {
String[] dictionary = loadWords();
Scanner kb = new Scanner(System.in);
System.out.println("Welcome to Hangman!");
System.out.println("How many tries");
int tries = kb.nextInt();
String word = chooseWord(dictionary);
System.out.println("OK, I've choseen my word. Start guessing");
System.out.println(word);
boolean[] bool = new boolean[26];
for(int i = 0; i < tries; i++) {
String wordguess = kb.next();
char guess = wordguess.charAt(0);
processGuess(guess, word, bool);
printPattern(word, bool);
}
}
// Loads a list of words from a file and returns the lsit as a String[].
public static String[] loadWords() throws FileNotFoundException {
ArrayList<String> wordList = new ArrayList<String>();
Scanner wordFile = new Scanner(new File("dictionary.txt"));
String line;
while (wordFile.hasNextLine()) {
line = wordFile.nextLine();
if(!line.equals(""))
wordList.add(line);
}
String[] result = new String[1];
return wordList.toArray(result);
}
// Takes an array of strings that represents the valid words as a parameter.
// Chooses one such word randomly and returns it.
public static String chooseWord(String[] dict) {
Random r = new Random();
int numword = r.nextInt(dict.length);
String hangword = dict[numword];
return hangword;
}
// Checks if a player has won the game
// Returns true only if all letters in the word have been guessed
public static boolean hasWon(String word, boolean[] guesses) {
for(int i = 0; i < word.length(); i++) {
if (guesses[word.charAt(i)-'a'] == false){
return false;
}
}
return true;
}
// Prints out the pattern of letters in the secret word based on the word
// and the letters that have been guessed.
// Prints any letter that has already been guessed and a _ for a letter that
// has not been guessed.
public static void printPattern(String word, boolean[] guesses) {
StringBuilder pattern = new StringBuilder();
for(int i = 0; i < word.length(); i++) {
if (guesses[word.charAt(i) - 'a']) {
pattern.append(word.charAt(i));
}
else {
pattern.append("_");
}
pattern.append(" ");
}
System.out.println(pattern.toString());
}
// Handles a guess by marking the letter as guessed and returns the number of
// tries to be charged: 0 if the guessed letter is in the word and 1 otherwise.
public static int processGuess(char guess, String word, boolean[] guesses) {
if(guesses[(guess - 'a')] == false){
guesses[(guess - 'a')] = true;
}
else {
System.out.println("you already guessed that letter");
}
return guess;
}
}
import java.io.*;
导入java.util.*;
公共级刽子手{
公共静态void main(字符串[]args)引发FileNotFoundException{
String[]dictionary=loadWords();
扫描仪kb=新扫描仪(System.in);
System.out.println(“欢迎来到刽子手!”);
System.out.println(“尝试次数”);
int trys=kb.nextInt();
字符串单词=选择单词(字典);
System.out.println(“好的,我已经看到我的单词了,开始猜测”);
System.out.println(word);
布尔值[]布尔值=新布尔值[26];
for(int i=0;i
应该使用while循环,而不是for循环
我假设您可能希望尝试次数可变,而不仅仅是3次。因此,这个例子将适应这一点
大概是这样的:
int incorrectTries = 0;
while (incorrectTries <= tries)
{
String wordguess = kb.next();
char guess = wordguess.charAt(0);
processGuess(guess, word, bool);
printPattern(word, bool);
}
// Checks if a player has won the game
// Returns true only if all letters in the word have been guessed
public static boolean hasWon(String word, boolean[] guesses) {
for(int i = 0; i < word.length(); i++) {
if (guesses[word.charAt(i)-'a'] == false){
incorrectTries++;
return false;
}
}
return true;
}
int不正确=0;
while(incorrectTries使用while循环检查尝试是否等于零,而不是通过尝试进行迭代的for循环。当出现错误猜测时,从尝试中减去一。这应该可以得到您所需要的。for(int i=0;ifor(int i = 0; i < tries; i++) {
您正在迭代一个固定的次数。它不应该是这样的吗
while (!wordComplete && triesCount < tries) {
//...
while(!wordComplete&&triesunt
?这不是与user1067478相同的用户,就是与user1067478在同一个项目中工作。谢谢你。我应该在我的main方法或haswon方法中输入这段代码吗?谢谢,这段代码很棒,但它仍然不能真正解决我的问题,因为当它要求尝试多少次时,我说10次,我猜10次尝试之前的所有字母都没有i don’我不能阻止我。我怎样才能修正它,当我在游戏停止之前猜到我的单词时。我是否需要为wordComplete和triesCount单独创建一个变量,你能帮我完成这个过程吗。