逐字逐句地猜一个字符串

时间:2014-04-09 20:55:17

标签: java string words

我有一项我正在努力的任务,我想知道是否有人可以帮助我。 我应该创建一个程序来猜测一串字符中的正确字母。我有一个名为alphabet的字符串,其中包含字母表中的所有字母以及用户输入的字符串。当用户猜出字母会显示正确的字母时,未经编辑的字母仍然是字符串中的问号。例如,字符串是我喜欢爆米花,并且用户猜到p它应该是这样的:? ???? p?p????.并且说他们猜测另一个正确的字母,如i,它将变为:i ?i?? p?p????.这是我的代码,我不知道从哪里开始。

import java.util.Scanner;
public class Characters {
    // This class controls a while loop
    // use at least 7 methods
    public static void charactersMethod() throws InterruptedException
    {
        Scanner scan = new  Scanner(System.in);
        String choice;
        Introduction();
        readString();
        guessChar();
        do {
            Scanner Scan = new Scanner (System.in);
            System.out.println("Would you like to play this game again? (Yes/No)");
            choice = Scan.next();
        }
        while (choice.equalsIgnoreCase("Yes"));

    }

    private static void Introduction() throws InterruptedException {

        System.out.println("Welcome to characters game!");
        System.out.println("This game prompts for a string of words. it then allows the player to guess the characters in the string.");
        System.out.println("it then keeps track of all of the characters guessed and tells the player he wins when");
        System.out.println("he guesses all the correct characters in the string.");

    }

    public static void readString() throws InterruptedException {
        Scanner Keyboard = new Scanner(System.in);
        String originalString;
        String guessedString;
        System.out.println("Please enter a string");

        originalString = Keyboard.nextLine();
        System.out.println("original string: " + originalString);
        System.out.println("guessed string: ?????????");
        System.out.println();
        System.out.println("Characters to choose from: abcdefghijklmnopqrstuvwxyz.");
        // when a correct letter is guessed, originalString - guess;
    }
    public static void guessChar()  throws InterruptedException
    {
        Scanner Scan = new Scanner (System.in);
        String guess;   
        System.out.println("Please guess a character: ");
        guess = Scan.nextLine();
        System.out.println("Character read: " + guess);
        while (guess.equalsIgnoreCase("A")) {

            System.out.println("The new String is " + originalString);
    }
    }
    public static void getRidChar() throws InterruptedException {

        }
        private static void sleepDemonstration() throws InterruptedException
        {

        }


    }

1 个答案:

答案 0 :(得分:0)

你可以创建两个Char数组,一个是要猜的字符串,一个是字符加?已经猜到了。

//Create char array out of String
char[] originalCharArray = originalString.toCharArray();

//Pass the two char arrays plus the guess to checkGuess

public char[] checkGuess (char[] originalCharArray, char[] guessedCharArray, Char guess) {
   for (int i = 0; i < originalCharArray.length; i++) {
       if (orignalCharArray[i] == guess) {
          guessedStringArray[i] = guess;
       }
   }
}

//This method initializes the guess array to '?'

public char[] init(char[] guessedStringArray) {
    for(int i = 0; i < guessedStringArray.length; i++) {
       guessedStringArray[i] = '?';
    }
}