Hangman游戏:如何找到单词?

huangapple go评论63阅读模式
英文:

Hangman game: How to find the word?

问题

我必须创建一个猜词游戏。我卡在最后一步。事实上,当用户找到单词时,我的输入会要求再猜一个字母。

这是一个示例:(要猜的单词是“non”)

请输入您的字母:n

n _ n 

请输入您的字母:o

n o n 

请输入您的字母

n o n

我的问题可能出在wordFind上,我不明白如何操作它?

String[] words = {"yess", "non"};
String wordRandom = words[(int) (Math.random() * words.length)];
boolean[] letterGuess = new boolean[wordRandom.length()];
boolean wordFind = false;
int numberAttempt = 5;

while (numberAttempt > 0 && !wordFind) {
    System.out.println("尝试次数:" + numberAttempt);

    for (int i = 0; i < wordRandom.length(); i++) {
        if (letterGuess[i]) {
            System.out.print(wordRandom.charAt(i));
        } else {
            System.out.print("- ");
        }
    }

    System.out.println(" ");

    System.out.print("请输入您的字母:");
    char letter = input.next().charAt(0);

    int letterFound = 0;
    boolean alreadyFound = false;

    for (int i = 0; i < wordRandom.length(); i++) {
        if (wordRandom.charAt(i) == letter) {

            if (letterGuess[i]) {
                alreadyFound = true;
            }

            letterGuess[i] = true;
            letterFound++;
        }
    }

    if (alreadyFound) {
        System.out.println("字母已被猜测并且已找到!");
    } else {
        if (letterFound == 1) {
            System.out.println("字母是正确的!");
        } else if (letterFound > 0) {
            System.out.println("字母在单词中出现了" + letterFound + "次!");
        } else {
            numberAttempt--;
            System.out.println("字母不在单词中!");
        }
    }
}
英文:

I have to create a hangman game. I am stuck in the last step. In fact, when the user finds the word my input asks yet a letter.

Here an example: (the word to find is "non")

Enter your letter please : n

n _ n 

Enter your letter please : o

n o n 

Enter your letter please :

n o n

My problem is probably my wordFind, I don't understand how to manipulate this ?

        String[] words = {&quot;yess&quot;, &quot;non&quot;};
String wordRandom = words[(int) (Math.random() *  words.length)];
boolean[] letterGuess = new boolean [wordRandom.length()];
boolean wordFind = false;
int numberAttempt = 5;
while(numberAttempt &gt; 0 &amp;&amp; !wordFind){
System.out.println(&quot;Number of attempt(s) &quot; + numberAttempt);
for(int i=0; i &lt; wordRandom.length(); i++){
if(letterGuess[i]){
System.out.print(wordRandom.charAt(i));
}
else{
System.out.print(&quot;- &quot;);
}
}
System.out.println(&quot; &quot;);
System.out.print(&quot;Enter your letter please : &quot;);
char letter = input.next().charAt(0);
int letterFound = 0;
boolean alreadyFound = false;
for(int i=0; i &lt; wordRandom.length(); i++){
if(wordRandom.charAt(i) == letter){
if(letterGuess[i]){
alreadyFound  = true;
}
letterGuess[i] = true;
letterFound++;
}
}
if(alreadyFound){
System.out.println(&quot;Letter already proposed and it has been found ! &quot;);
} else{
if(letterFound == 1){
System.out.println(&quot;The letter is correct ! &quot;);
}
else if(letterFound  &gt; 0){
System.out.println(&quot;The letter is &quot; + letterFound  + &quot; times in the word ! &quot;);
}
else{
numberAttempt--;
System.out.println(&quot;The letter is not in the word ! &quot;);
}
}
}

答案1

得分: 2

你需要添加类似这样的“已解决”条件

boolean wordFind = false;
int numberAttempt = 5;
boolean solved = false;

while(numberAttempt > 0 && !wordFind && !solved){

在更新letterFound后,设置solved

letterFound++;
if (letterfound == wordRandom.length()) {
    solved = true
}

然后在while循环结束后,如果solved为真,恭喜用户。

英文:

You need to add a solved condition like this

    boolean wordFind = false;
int numberAttempt = 5;
boolean solved = false;
while(numberAttempt &gt; 0 &amp;&amp; !wordFind &amp;&amp; !solved){

after you update letterFound, set solved

letterFound++;
if (letterfound == wordRandom.length()) {
solved = true
}

Then at the end after the while loop, if solved is true, congratulate the user.

答案2

得分: 1

int amountCorrect = 0; // 你可以创建一个变量 int amountCorrect = 0; 并将其用作检查。每当用户猜对一个字母时,将“amountCorrect”增加字母的数量。在“while”循环开始时,创建一个布尔检查,检查“amountCorrect”变量是否与“wordRandom”的长度相同。我知道您的代码有点长,所以我在添加代码的行旁边放了一个注释:

String[] words = {"yess", "non"};
String wordRandom = words[(int) (Math.random() * words.length)];
boolean[] letterGuess = new boolean[wordRandom.length()];
boolean wordFind = false;
int numberAttempt = 5;
int amountCorrect = 0; // amountCorrect变量

while (numberAttempt > 0 && !wordFind) {
    System.out.println("Number of attempt(s) " + numberAttempt);

    // 这是布尔检查
    if (amountCorrect == wordRandom.length()) {
        System.out.println("You Win!");
        break;
    }

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

        if (letterGuess[i]) {
            System.out.print(wordRandom.charAt(i));
        } else {
            System.out.print("- ");
        }

    }

    System.out.println(" ");

    System.out.print("Enter your letter please : ");
    char letter = input.next().charAt(0);

    int letterFound = 0;
    boolean alreadyFound = false;

    for (int i = 0; i < wordRandom.length(); i++) {
        if (wordRandom.charAt(i) == letter) {

            if (letterGuess[i]) {
                alreadyFound = true;
            }

            letterGuess[i] = true;
            letterFound++;

            // 添加到amountCorrect
            amountCorrect++;

        }

    }
    if (alreadyFound) {

        System.out.println("Letter already proposed and it has been found!");

    } else {
        if (letterFound == 1) {
            System.out.println("The letter is correct!");
        }

        else if (letterFound > 0) {
            System.out.println("The letter is " + letterFound + " times in the word!");
        }

        else {
            numberAttempt--;
            System.out.println("The letter is not in the word!");
        }

    }

}
英文:

You can create a variable int amountCorrect = 0; and use that as a check. Every time the user gets a right letter, add to amountCorrect the number of letters there are. At the beginning of the while loop, create a boolean check that checks if the amountCorrect variable is the same as the length of the wordRandom. I know your code is kind of long, so I put a comment next to the lines where I added code:

    String[] words = { &quot;yess&quot;, &quot;non&quot; };
String wordRandom = words[(int) (Math.random() * words.length)];
boolean[] letterGuess = new boolean[wordRandom.length()];
boolean wordFind = false;
int numberAttempt = 5;
int amountCorrect = 0; // The amountCorrect variable
while (numberAttempt &gt; 0 &amp;&amp; !wordFind) {
System.out.println(&quot;Number of attempt(s) &quot; + numberAttempt);
// This is the boolean check
if (amountCorrect == wordRandom.length()) {
System.out.println(&quot;You Win!&quot;);
break;
}
for (int i = 0; i &lt; wordRandom.length(); i++) {
if (letterGuess[i]) {
System.out.print(wordRandom.charAt(i));
} else {
System.out.print(&quot;- &quot;);
}
}
System.out.println(&quot; &quot;);
System.out.print(&quot;Enter your letter please : &quot;);
char letter = input.next().charAt(0);
int letterFound = 0;
boolean alreadyFound = false;
for (int i = 0; i &lt; wordRandom.length(); i++) {
if (wordRandom.charAt(i) == letter) {
if (letterGuess[i]) {
alreadyFound = true;
}
letterGuess[i] = true;
letterFound++;
// add to amountCorrect
amountCorrect++;
}
}
if (alreadyFound) {
System.out.println(&quot;Letter already proposed and it has been found ! &quot;);
} else {
if (letterFound == 1) {
System.out.println(&quot;The letter is correct ! &quot;);
}
else if (letterFound &gt; 0) {
System.out.println(&quot;The letter is &quot; + letterFound + &quot; times in the word ! &quot;);
}
else {
numberAttempt--;
System.out.println(&quot;The letter is not in the word ! &quot;);
}
}
}

huangapple
  • 本文由 发表于 2020年4月11日 05:13:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/61148781.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定