Java刽子手程序

Java刽子手程序,java,Java,我需要帮助找出代码中的以下错误:替换正确的 用户在神秘词中输入的字母。我最后一个方法的功能是这样做,但是 我不明白为什么它只会输入一个正确的字母,而不是所有正确的猜测。此外,用户的最大尝试次数为8次,但当我编译程序时,尝试次数将变成负数,我无法找出最大尝试次数循环的错误。提前感谢!我真的很感激**在这个程序中使用几种方法也是我的作业要求 import java.util.*; import java.io.*; public class Hangman { public stati

我需要帮助找出代码中的以下错误:替换正确的 用户在神秘词中输入的字母。我最后一个方法的功能是这样做,但是 我不明白为什么它只会输入一个正确的字母,而不是所有正确的猜测。此外,用户的最大尝试次数为8次,但当我编译程序时,尝试次数将变成负数,我无法找出最大尝试次数循环的错误。提前感谢!我真的很感激**在这个程序中使用几种方法也是我的作业要求

import java.util.*;
import java.io.*;

     public class Hangman {
 public static Scanner keyboard; 
 public static final int MAXSIZE = 15000;
 public static final int NUM_GUESS = 8; 
 public static final int WORD_LENGTH = 20; 
 public static void main(String[] args) {
 keyboard = new Scanner(System.in); 
 int random = pickrandom(MAXSIZE); 

    try {
        // Set up connection to the input file
        Scanner input = new Scanner(new FileReader("dictionary.txt"));
        String [] dictionaryWords = new String [MAXSIZE];
        int usedsize = 0;
        while ( usedsize < MAXSIZE && input.hasNextLine() ){
            dictionaryWords [usedsize] = input.nextLine();
            usedsize ++; 
            }
        System.out
        .println("              H A N G M A N. " +
                "\n This is a word guessing game. " +
                "\n A word will be selected at random and kept hidden. You will try to figure out the secret word by" +
                "\n guessing letters which you think are in the word. " +
                "\n You will guess one letter at a time. " +
                "\n If the letter you guess is correct, the position(s) of the letter in the secret word will be shown. " +
                "\n You will be allowed " + NUM_GUESS + " wrong guesses  If you guess incorrectly " + NUM_GUESS+ " times, you lose the game. " +
                "\n If you guess all of the letters in the word, you win." +
                "\n\n Press enter to continue ");
        keyboard.nextLine(); 
        clearScreen();
        String word = dictionaryWords[random];

        System.out.println(dictionaryWords[random]);
        String blank = blankWord(word);

        String decision; 
        //decision = keyboard.nextLine().toUpperCase();
        System.out.println("Word to guess :");
        System.out.println(blank);
        int tries = NUM_GUESS; 

        System.out.println("Enter a letter to guess or 9 to quit");
        String guess = keyboard.next();
        do{

             //System.out.println(tries); 
        while (!guess.equals("9") || !(guess.equals(word)  && tries >0))
                {
                char letter = guess.charAt(0); 
                String guesses = "";
                guesses += letter;
                if (word.indexOf(letter) < 0) {
                    tries--;
                    System.out.println("Incorrect letters tried: " + guess);
                    System.out.println("Number of guesses left: " + tries);
                    System.out.println("Enter a letter to guess or 9 to quit");
                    guess = keyboard.next();
                }
                else {
                    String correctWord = correctWord( guess,  word,  blank, letter );
                    System.out.println(correctWord); 
                    System.out.println("Enter a letter to guess or 9 to quit");
                    tries--;
                    System.out.println("Number of guesses left: " + tries);
                    guess = keyboard.next();
                    tries--;
                    System.out.println("Number of guesses left: " + tries);
                }












            }
        if (guess.equals("9")){
            System.out.println("Thanks for playing");

        }
        if ( guess.equals(word)){
            System.out.println("You won!");
        }
        if( tries == 0){
            System.out.println("You have no more guesses left"); 
        }
        System.out.println("Play again? Y/N");
        decision = keyboard.nextLine().toUpperCase();

        } while (decision.equals("Y"));
        //System.out.println("Play again? Y/N");
        //decision = keyboard.nextLine().toUpperCase();
    }
    catch (FileNotFoundException e) {
        System.out.println("There was an error opening one of the files.");
    }


}
//Clears screen after introduction 
private static void clearScreen (){
    for (int blanks = 0; blanks < 80; blanks++) {
        System.out.println();
    }
}

// This method returns a randomly selected integer
// between 0 and count-1
public static int pickrandom(int count) {
    Random generator = new Random();
    return generator.nextInt(count);
}

// Places asterisks in place of the letters in the word
// Parameter is the string word. it holds mystery word

public static String blankWord(String word) {
    String blank = "";

    for (int i = 0; i < word.length(); i++) {

        blank += " * ";
    }
    return blank;
}
//Replaces asterisks with correct guess 
     public static void fillWord(String blank,String word, char letter ){
for ( int i = 0; i <word.length() ; i++){
    if(word.charAt(i) == letter){
        blank.charAt( i ); 

    }

}
  }
  //Receives correct guesses and replaces the asterisks with correct letter 
  //Parameters is  the mystery word 
    public static String newWord (String word){
 String newWord  = "";
 if (newWord.length() > 0){
     newWord += word.charAt(0);
     for (int i = 1; i <word.length(); i ++){
         newWord += word.charAt(0);
     }

 }
return newWord;  
  }
  //Counts the number of missed guesses and replaces total count
  //Parameters are the mystery word and the letter guesses from user 
  public static boolean alreadyGuessed(String word, String guess){
 for (int i = 0 ; i< word.length() ; i++){
     if (word.charAt(i)== guess.charAt(0)){
         return true; 
     }
 }
 return false; 
     }
         //Compares to see if the letter has already been guessed
      //Parameters are the mystery word and the user's guess 
      public static boolean letterGuessed(String word, String guess){
     for (int i = 0 ; i< word.length(); i++){
     if (word.charAt(i) == guess.charAt(0)){
         return true;
     }
 }
 return false; 
    }
   //Replaces correct guess in blanks 
     public static String correctWord(String guess, String word, String blank, char letter){ 

    for (int i = 0; i < word.length(); i++) {

         if (letter == word.charAt(i)){
             if (i >0){
         blank = blank.substring (0, i) + letter + blank.substring(i +1);
             }
             if (i ==0){
                 blank = letter + blank.substring(1); 
             }

         }
    }
    return blank;
}



}
import java.util.*;
导入java.io.*;
公共级刽子手{
公共静态扫描仪键盘;
公共静态最终整数最大值=15000;
公共静态最终int NUM_GUESS=8;
公共静态最终整数字长度=20;
公共静态void main(字符串[]args){
键盘=新扫描仪(System.in);
int random=pickrandom(MAXSIZE);
试一试{
//设置与输入文件的连接
扫描仪输入=新扫描仪(新文件阅读器(“dictionary.txt”);
String[]DictionaryWord=新字符串[MAXSIZE];
int usedsize=0;
while(usedsize0))
{
字符字母=guess.charAt(0);
字符串猜测=”;
猜测+=字母;
if(单词索引of(字母)<0){
尝试--;
System.out.println(“尝试了不正确的字母:“+猜测”);
System.out.println(“剩余猜测次数:+尝试次数”);
System.out.println(“输入一个字母进行猜测或输入9退出”);
猜测=键盘。下一步();
}
否则{
字符串correctWord=correctWord(猜测、单词、空白、字母);
System.out.println(correctWord);
System.out.println(“输入一个字母进行猜测或输入9退出”);
尝试--;
System.out.println(“剩余猜测次数:+尝试次数”);
猜测=键盘。下一步();
尝试--;
System.out.println(“剩余猜测次数:+尝试次数”);
}
}
如果(猜测等于(“9”)){
System.out.println(“谢谢玩”);
}
如果(猜测等于(单词)){
System.out.println(“你赢了!”);
}
如果(尝试==0){
System.out.println(“您没有更多的猜测了”);
}
System.out.println(“再次播放?是/否”);
decision=keyboard.nextLine().toUpperCase();
}while(decision.equals(“Y”));
//System.out.println(“再次播放?是/否”);
//decision=keyboard.nextLine().toUpperCase();
}
catch(filenotfounde异常){
System.out.println(“打开其中一个文件时出错。”);
}
}
//介绍后清除屏幕
专用静态无效清除屏幕(){
for(int blanks=0;blanks<80;blanks++){
System.out.println();
}
}
//此方法返回随机选择的整数
//介于0和计数-1之间
公共静态整数随机选取(整数计数){
随机生成器=新随机();
返回生成器.nextInt(计数);
}
//用星号代替单词中的字母
//参数是字符串字。它包含神秘字
公共静态字符串空白字(字符串字){
字符串空白=”;
for(int i=0;i对于(int i=1;i您的逻辑是向后的。如果trys是
-1
,则:

while( ... || (guess.equals(word) && (tries < 8) && (tries > 0)) {
while( ... || (guess.equals(word) && (-1 < 8) && (-1 > 0)) {
while( ... || (guess.equals(word) && true && false)) {
while( ... || !(false)) {
while( ... || true) {
while(true) {

这可能是你想要的。

至少你尝试了一些东西,但这是一段非常长的代码。你怎么能期望我们通读并回答这些问题最后一种方法是用来计算字母的去向。我只是把整个代码放在这里,以防人们想了解我的程序的更多信息。你也可以从相关的两个开始方法,然后将整个程序放在下面的另一个块中。谢谢你的帮助!我本来有你的代码,但它仍然给我相同的错误。我将它改回你的建议,但我仍然得到相同的问题。另外,我猜
实际上只是
while (!guess.equals(word) && (tries > 0)) {