Java Hangman帮助,包括我的代码 [英] Java hangman help, my code included

查看:69
本文介绍了Java Hangman帮助,包括我的代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

**您好,我必须用Java创建一个子手游戏.我不能使用数组.我的大多数代码都已完成,但是我遇到了一些问题,欢迎一些提示.

**Hello, I have to create a hangman game in java. I cant use arrays. Most of my code is done but I have been having some problems and some tips would be welcome.

我刚刚发现了其他可以帮助的内容.在提示用户输入新的秘密单词并使用newHangMan.setSecretWord(newWord)之后;我的伪装词未重置为"????" (与秘密单词中的单词相同的?"号).

I just found something else that I could use help on. After prompting the user for a new secret word and using newHangMan.setSecretWord(newWord); my disguised word does not reset to "????" (with the same number of "?" as words in the secret word).

对于这么长的帖子和错误的格式(第一次在这里发布),我感到非常抱歉. 有人可以帮忙吗?**

I'm very sorry for such a long post and the bad formatting(1st time posting here). Can anyone help?**

这是我的课程文件:

 public class HangMan 
{

private String secretWord = "bigbang",  disguisedWord = "";
private int guessCount = 0, missCount = 0;



public void setSecretWord(String newWord)
{
    secretWord = newWord;
    guessCount = 0;
    missCount = 0;

    int wordLength = newWord.length();
    while(wordLength > 0)
    {
        disguisedWord = disguisedWord + "?";
        wordLength--;
    }

}

public String getSecretWord()
{
    return secretWord;
}




 public boolean isFound()
 {  
    return secretWord.equalsIgnoreCase(disguisedWord);

 }


 public String getDisguisedWord()
 {
     return disguisedWord;
 }

 public int getGuessCount()
 {
     return guessCount;
 }

 public int getMissesCount()
 {
     return missCount;
 }

 public void guessCharacter(char c)
 {
    // int position = secretWord.indexOf(c);
     boolean got_it = false;
     String updateDisguised="";

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

         if(c == secretWord.charAt(i))
         {

             updateDisguised = updateDisguised + secretWord.charAt(i);

             String checkDuplicate = updateDisguised.substring(0,i);
             int duplicatePos = checkDuplicate.indexOf(c);
             if(duplicatePos <0)
                 guessCount++;
             got_it = true;


         }
         else 
         {
             updateDisguised = updateDisguised + disguisedWord.charAt(i);
         }

     }
     if(got_it == false)
     {
         missCount++;
         guessCount++;
     }


     disguisedWord = updateDisguised; 

 }










 }

这是我的主要方法:

import java.util.Scanner;



 public class HangManGame {

public static void main(String[] args) 
{


    boolean retry= true;
    String retry_ans;

    Scanner kb = new Scanner(System.in);


    HangMan newHangMan = new HangMan();

    String word = newHangMan.getSecretWord();
    String input;
    char guess;
     newHangMan.setSecretWord(word);

    System.out.println("Hangman game starts:");

do{


    System.out.println("Guess this: " + newHangMan.getDisguisedWord());
    System.out.println("Enter your guess character: [guess]");
    input = kb.next();
    guess = input.charAt(0);

    newHangMan.guessCharacter(guess);

    System.out.println(newHangMan.getDisguisedWord());

    System.out.println("Number of guesses so far : " + newHangMan.getGuessCount());
    System.out.println("NUmber of misses so far: " + newHangMan.getMissesCount());




    if((newHangMan.getMissesCount()==7) || (newHangMan.isFound()))
    {
        System.out.println("The game is over");

        System.out.println("Would you like to try again?");
        retry_ans = kb.next();
        if(retry_ans.equalsIgnoreCase("yes"))
        {
            retry = true;
            System.out.println("Please enter a new secret word:");
            String newWord = kb.next();
            newHangMan.setSecretWord(newWord);
        }
        else
        {
            retry =false;
        }

    }






    }   while(retry == true);




}

}

推荐答案

(newHangMan.isFound()=true)

应该是

newHangMan.isFound()

请勿将布尔值与另一个布尔值进行比较. =表示boolean.

Do not make an bool compare to another bool. The = is evaluate the boolean.

这篇关于Java Hangman帮助,包括我的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆