我正在创建一个子手游戏。目前有两个问题

时间:2018-10-20 04:26:24

标签: java

首先,将秘密单词打印为短划线,然后用户输入要猜测的字母。如果他们猜对了字母,则会更新破折号。因此,如果单词是java,它将显示为----;如果用户键入a,则它将更新并显示-a-a。我的程序可以做到这一点,但它还会在末尾添加额外的破折号,而且我不知道如何使其不打印那些额外的破折号。这给我带来了另一个问题,用户被问到他们想猜字母的索引是什么。因此,如果用户键入字母a和索引1,则更新的单词将显示-a--,但是我的程序会更新a所在位置的所有实例,因此显示-a-a。这是我的代码:

import java.util.Scanner; 公共类HangMan2 {

private static final boolean testingMode = true;

public static void main(String[] args) {

    Scanner keyboard = new Scanner(System.in);
    int guessRemaining = 20;
    int rounds = 1;
    int roundScore;

    String wordString = "";


    String word = RandomWord.newWord();
    int length = word.length();

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

        wordString += "-";

    }


    System.out.println("The word is: " +wordString);

    System.out.println("The secret word is: " +word);

    System.out.println("Enter the number of spaces allowed");
    int spacesAllowed = keyboard.nextInt();
    keyboard.nextLine();

    if(spacesAllowed > length)
    {
        System.out.println("Invalid input. Try again.");
        System.out.println("Enter the number of spaces allowed");
        spacesAllowed = keyboard.nextInt();
    }

    while(guessRemaining > 0) {

        System.out.println("Please enter the letter you want to guess: ");
        String letterGuess = keyboard.next();
        char letterCharacter = letterGuess.charAt(0);

        System.out.println("Please enter the number of spaces you want to check (seperated by spaces): ");
        String spacesChecked = keyboard.next();

            boolean guessCheck;
            // check if the letter is in the string
            guessCheck = (word.indexOf(letterCharacter)) != -1;

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

                         if (letterCharacter == word.charAt(i)) {

                            wordString = wordString.substring(0, i) + letterGuess + wordString.substring(i);

                            System.out.println("Your guess is in the word!");

                            System.out.println("The updated word is: " +wordString); 
                            } //end of if statement

                     } //end of for loop
            }

            else
                    {
                         System.out.println("Your letter was not found in the spaces you provided");
                         guessRemaining--;
                         System.out.println("You have " +guessRemaining+ " guesses remaining.");
                    }   
    }


    if(guessRemaining != 0)
    {
        System.out.println("You win!");
        System.out.println("You have guessed the word! Congratulations");

        roundScore = (guessRemaining * 10) / spacesAllowed;

    } //end of if

    else{
        System.out.println("Guesses Remaining: 0");
        System.out.println("You have failed to guess the word... :(");
    } 

    System.out.println("Would you like to play again? Yes (y) or No (n)");
    String playAgain = keyboard.next();

    if(!playAgain.equals("y") && !playAgain.equals("n"))
    {
        System.out.println("Invalid response, please try again... ");
    }

    if(playAgain.equals("y"))
    {
        rounds++;

    }

    else
    {
        System.exit(0);
    }
}

}

1 个答案:

答案 0 :(得分:0)

wordString = wordString.substring(0, i) + letterGuess + wordString.substring(i);

if ((wordString.substring(0,i) + wordString.substring(i)).equals(wordString))
     System.out.println("These are completely identical");
else
     System.out.println("You solved it yourself ;)");

提示:您发布的代码中为58。

第二部分:完全不同的程序结构。 您需要跟踪用户的两个猜测值,一个作为字符,另一个作为整数。 您将使用解决我提供的if语句悖论的相似代码,将wordString.toCharArray()[indexUserGuessed]与characterUserGuessed进行比较,并根据需要更新结果或游戏状态。

最后,欢迎使用Stack Exchange。我们大多数人不会为您做功课。

哦,我将查找“ StringBuilder Java”的示例,因为您可能会发现使用此类比使用String更容易操作String。