猜词游戏的问题

超级罐

我正在尝试让Java将while循环的输出识别为变量,并在进一步的操作中使用该输出。

我想尝试让一位玩家设置单词,而另一位则猜猜它来升级它。问题出在使破折号等于玩家输入的单词中的字母数,所以我把代码分离出来了,这行得通。

但是,当我把它们全部放回去时main,它不会识别出循环结束后有多少破折号。它只能识别只有一个破折号的初始字符,因此会带来问题。

编辑:非常感谢你们,这是我第一次在堆栈溢出,再次是tnx。像魅力一样工作:D

package iB;
import java.util.Scanner;
 import java.lang.String;
 public class WordGuess {
/**
 * @param args
 */
public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    String secretWord ;
    String guess, dash = "-", upWord;
    int numGuesses = 0;
    int numWord;
    final String SENTINEL = "!";
    System.out.println("Player 2, please look away.    Player 1, please enter  the secter word: \n");
    secretWord = input.next().toUpperCase().trim();
    numWord = secretWord.length();
    //System.out.println("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");   
    for(int dashNum = 1; dashNum < numWord; dashNum++) {
        dash += "-" ;
    }
    System.out.println("WordGuess game!\n");
    do {
        System.out.println("Enter a letter (" + SENTINEL + "to guess entire word): ");
        guess = input.next().toUpperCase().trim();
        numGuesses ++;
      if (secretWord.contains(guess) && guess.length() == 1)    {
            upWord = dash.substring(0, secretWord.indexOf(guess));
            upWord += guess;
            upWord += dash.substring(secretWord.indexOf(guess) + 1, dash.length());
            dash = upWord.toUpperCase();
            System.out.println(dash);
            if (dash.equals(secretWord))    {
                System.out.println("You won!\n" + "The secret word is " + secretWord);
                System.out.println("You made " + numGuesses + " guesses."); }
      } else    if (guess.length() >= 2)    {
            System.out.println("Please only enter one letter at a time! \n");   }               
            if (guess.contains(SENTINEL)) {
            System.out.println("What is your guess? ");
            guess = input.next().toUpperCase().trim();
            if (guess.equals(secretWord))   {
                System.out.println("You won!\n" + "The secret word is " + secretWord);
                System.out.println("You made " + numGuesses + " guesses.");
                break;
            } else {
                System.out.println("You Lose!");
                System.out.println("The secret word was " + secretWord);
                System.out.println("You made " + numGuesses + " guesses.");
                    break;
                }
            }
        }   while(!guess.contains(SENTINEL));
        input.close();  
}

}

理查德·廷格

问题

以下代码段似乎试图显示单词中正确选择的字母的位置

    if (SecretWord.indexOf(guess) >= 0) {
        UpWord = dash.substring(0, SecretWord.indexOf(guess));
        UpWord += guess;
        UpWord += dash.substring(SecretWord.indexOf(guess) + 1, dash.length());
        System.out.println(UpWord);
    } else {

因此,如果单词是this并且您猜到了,i那么输出应该是

- 一世-

dash.substring重复破折号,它是破折号的一个子部分,因为破折号的长度为1个字母,除substring(0,1)以外的任何内容都将导致异常。

基本解决方案

我相信您想重复dash直到找到猜到的字母,然后再重复直到单词的结尾。类似于以下内容:

        if (SecretWord.indexOf(guess) >= 0) {
            int guessedIndex=SecretWord.indexOf(guess);

            String outString="";

            for(int i=0;i<guessedIndex;i++){
                outString+=dash; //repeat dash until we get to the correctly guessed letter
            }

            outString+=guess; //put the letter in

            for(int i=guessedIndex;i<SecretWord.length();i++){
                outString+=dash; //repeat dash until we get to end of the word
            }

            System.out.println(outString);
        } else { 

更好的解决方案

但是,这留下了仅显示字母的第一个实例的问题。可以使用另一个堆栈溢出答案来解决此问题,在该答案中,我们看到可以使用函数来获取字符的所有出现次数

public static ArrayList<Integer> getAllIndexes(String testChar, String string){
    int index=string.indexOf(testChar);


    ArrayList<Integer> indexes=new ArrayList<Integer>();
    while(index>0){
        indexes.add(index);
        index=string.indexOf(testChar,index+1);
    }

    return indexes;
}

然后使用该函数查找出现字母的所有索引,我们可以处理重复的字母

        if (SecretWord.indexOf(guess) >= 0) {
            int guessedIndex=SecretWord.indexOf(guess);

            ArrayList<Integer> indexes=getAllIndexes(guess,SecretWord);

            String outString="";

            for(int i=0;i<SecretWord.length();i++){
                if (indexes.contains(i)){ 
                    outString+=guess; //if its one of the guessed letters, put that in
                }else{
                    outString+=dash; //otherwise add a dash
                }

            }



            System.out.println(outString);
        } else {

现在一个单词hello和一个l正确输出的猜测--LL-

笔记

  • 通常遵循命名约定,即变量名以小写驼峰表示,这意味着它们以小写字母开头,SecretWord应该这样secretWord就目前而言,它看起来像SecretWord是一个类,通常以大写字母大写。
  • 很好,如果一旦您猜出一个字母后它便不再插入破折号并在以后每次都插入该字母,则可以通过使用布尔数组检查该字母是否被猜中来实现。超出了这个问题的范围
  • 所有这些解决方案都附加了字符串,这对于大量数字可能会很慢,在您的情况下,这是正确的做法,但是在将多个字符串连接到一个循环中时,请考虑使用StringBuilder来消除创建中间负载的开销弦

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章