猜一个单词(Java)

猜一个单词(Java),java,netbeans,Java,Netbeans,你好,我必须创建一个程序,让玩家猜一个单词。我的代码运行良好,但我必须编写一个条件,允许玩家尝试7次,如果玩家在第7次尝试时没有猜到单词,他/她将失败。我不知道如何写这个条件。这是我的密码: package javaapplication5; import java.io.*; import java.lang.*; import java.util.*; public class NewClass2{ public static int ReadWordsFromFile(St

你好,我必须创建一个程序,让玩家猜一个单词。我的代码运行良好,但我必须编写一个条件,允许玩家尝试7次,如果玩家在第7次尝试时没有猜到单词,他/她将失败。我不知道如何写这个条件。这是我的密码:

package javaapplication5;

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

public class NewClass2{  



 public static int ReadWordsFromFile(String[] words)
  {
        try
        {
              FileReader fr = new FileReader("Guess_words.txt");
              BufferedReader br = new BufferedReader(fr);
              int count = 0;
              for (int i = 0; i <87; i++)
              {
                    String s = br.readLine();
                    if (s == null)
                          break;
                    words[count++] = s;
              }
              fr.close();
              return count;
        }
        catch (FileNotFoundException e)
        {
              System.out.println(e.getMessage());
              return -1;
        }
        catch (IOException err)
        {
              System.out.println(err.getStackTrace());
              return -1;
        }
  }

  static public String ReadString()
  {
        try
        {
              String inpString = "";
              InputStreamReader input = new InputStreamReader(System.in);
              BufferedReader reader = new BufferedReader(input);
              return reader.readLine();
        }
        catch (Exception e)
        {
              e.printStackTrace();
        }
        return "";
  }

  public static void main(String[] args)
  {
        System.out.println("Welcome to Guess a Word\n");

        String[] words = new String[87];

        int count = ReadWordsFromFile(words);
        if (count < 0)
        {
              System.out.println("No words found in the file");
              return;
        }

        if (words == null)
              return; // Exception message was already shown
        int x = (int)(Math.random() * 87);
        int guessX = (x % count);

        String secretWord = words[guessX];

        int numChars = secretWord.length();

        System.out.print("Your secret word is: ");
        for(int i = 0; i < numChars; i++)
              System.out.print("*");
        System.out.println();

        boolean bGuessedCorrectly = false;

        System.out.println("Guess now  (To stop the program, enter #) : ");

        while (true)
        {
              String choice = ReadString();

              if (choice.startsWith("#"))
                    break;

              if (choice.compareTo(secretWord) == 0)
              {
                    bGuessedCorrectly = true;
                    break;
              }
              for (int i = 0; i < numChars; i++)
              {
                    if (i < secretWord.length() &&
                          i < choice.length())
                    {
                          if (secretWord.charAt(i) == choice.charAt(i))
                                System.out.print(choice.charAt(i));
                          else
                                System.out.print("*");
                    }
                    else
                          System.out.print("*");
              }
              System.out.println();

        }

        if (bGuessedCorrectly == false)
              System.out.println("Unfortunately you did not guess it correctly. The secret word           is: " + secretWord);
        else
              System.out.println("Congrats! You have guessed it correctly");         
  }
} 
PackageJavaApplication5;
导入java.io.*;
导入java.lang.*;
导入java.util.*;
公共类NewClass2{
公共静态int-ReadWordsFromFile(字符串[]个字)
{
尝试
{
FileReader fr=新的FileReader(“Guess_words.txt”);
BufferedReader br=新的BufferedReader(fr);
整数计数=0;

对于(inti=0;i为什么不简单地从以下内容更改循环:

while (true) {
}
为此:

for (int nrOfGuesses = 0; nrOfGuesses < 7; nrOfGuesses++) {
    // do stuff
}
for(int-nrofguesss=0;nrofguesss<7;nrofguesss++){
//做事
}
只需将while(true)
替换为for(int-trunt=0;trunt<7;trunt++)