如何用短划线或问号替换字符串?

时间:2015-05-31 20:33:45

标签: java string

因此,我正在制作一个刽子手游戏,用户输入一个单词,然后显示该单词,但在问号中进行伪装。每当用户正确输入单词时,该字母就会出现在问号的位置。我有猜测等所需的所有代码,我只需要有人帮助伪装用户在问号中输入的单词。

public static void main(String[] args) {
    Scanner Bob = new Scanner(System.in);
    ArrayList<String> alphabet = new ArrayList<String>(Arrays.asList("a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"));//i looked this part up Mr. A, I didn't know how to directly set values in an arraylist
    int guesses = 6;//wrong guesses

    System.out.println("Hello! Please enter in your word for the game. Only one word is allowed!");
    String word = Bob.next();
    int lettersInWord = word.length();//so you know if you've solved the word or not

    while(guesses > 0 && lettersInWord > 0){ //not or because you'll end up getting a negative 1
        System.out.println("What is your guess? One letter at a time please or you'll mess up my game.");
        String guess = Bob.next();
        guess.toLowerCase(); //i populated the arraylist with only lowercase letters so it has to be this way
        while(!alphabet.contains(guess)){ //since I remove letters from the arraylist, this is like the default to make sure a guess doesn't happen twice
            System.out.println("You already guessed this letter, or this is not a letter");
            guess = Bob.next();//just lets user make another guess
        }
        alphabet.remove(guess);//takes letter out of alphabet so no redoes happen
        if(word.contains(guess)){
            System.out.println("Yeauhhhhh, thats right! You guessed " + guess);
            lettersInWord--;//makes the word size smaller
        } else {
            System.out.println("Nahhhhh, that's wrong! You guessed " + guess + " and it is not in the word");
            guesses--;//takes a guess away
        }

        System.out.println("You have " + guesses + " wrong guesses left"); //displays at the end of the iteration
    }
    if(guesses == 0){//this comes up when you lose
        System.out.println("You have lost, the word was "+ word);
    } else {//this comes up when you've guessed the word, if its not one its the other!!!
        System.out.println("You have won! the word was  "+ word  + "good job dude. Please see\n"
                + "Mr. A for a free piece of cake and a 100 on your final.");
    }
}

3 个答案:

答案 0 :(得分:0)

您创建一个单独的问号字符串,其长度与您的单词相同。

String marks = "";
for (int i = 0; i < word.length(); i++) {
    marks += "?";
}

当正确猜到一个字母时,用字母替换一个问号。

答案 1 :(得分:0)

我最后只为你做了一个简单的例子

   public class Test {

    public static void main(String args[]) {
        String word = "Hello";
        String unCapWord = word.toLowerCase();
        int guesses = word.length();
        int count = 0;
        char usedLetters[] = new char[26];
        String testWin;


        char[] guessBox = new char[word.length()];
        for (int i =0; i < word.length(); i++)
            guessBox[i] = '?';
        Scanner bob = new Scanner(System.in);
        while (count < guesses) {
            char guess = bob.next().toLowerCase().charAt(0);
            String checkIfCharUsed = new String(usedLetters);
            if (checkIfCharUsed.contains(guess+"")) {
                System.out.println("Already used this char, try another!");
                continue;
            }
            for (int i =0; i < word.length(); i++) {
                if (unCapWord.charAt(i) == guess)
                    guessBox[i] = guess;

            }
            testWin = new String(guessBox);
            if (testWin.equals(unCapWord)) {
                System.out.println("You got it!");
                return;
            }
            usedLetters[count] = guess;
            count++;
        }
        System.out.println("You lost!");
    }
}

答案 2 :(得分:0)

这很快且很脏,但会在猜到的单词中显示每个字母的破折号。正如您猜测正确的字母一样,破折号将被猜到的字母替换。

public static void main(String[] args) throws Exception {
    Scanner Bob = new Scanner(System.in);
    ArrayList<String> alphabet = new ArrayList<>(Arrays.asList("a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"));//i looked this part up Mr. A, I didn't know how to directly set values in an arraylist
    int guesses = 6;//wrong guesses

    System.out.println("Hello! Please enter in your word for the game. Only one word is allowed!");
    String word = Bob.next();
    int lettersInWord = word.length();//so you know if you've solved the word or not
    String[] lettersOfWord = new String[lettersInWord];
    // Replace "-" with "?" if you want to use question marks
    Arrays.fill(lettersOfWord, "-");

    clearScreen();
    String guess = "";
    int lettersCorrect = -1;
    while (guesses > 0 && lettersInWord > 0) { //not or because you'll end up getting a negative 1
        if (lettersCorrect > 0) {
            System.out.println(String.format("There %s %d %s", 
                    lettersCorrect > 1 ? "are" : "is", lettersCorrect, guess));

            lettersCorrect = 0;
        } else if (lettersCorrect == 0) {
            System.out.println("Nahhhhh, that's wrong! You guessed " + guess + " and it is not in the word");
        }
        System.out.println(String.join("", lettersOfWord));
        System.out.println("");

        System.out.println("You have " + guesses + " wrong guesses left"); //displays at the end of the iteration
        System.out.println("What is your guess? One letter at a time please or you'll mess up my game.");
        guess = Bob.next().toLowerCase();

        while (!alphabet.contains(guess)) { //since I remove letters from the arraylist, this is like the default to make sure a guess doesn't happen twice
            System.out.println("You already guessed this letter, or this is not a letter");
            guess = Bob.next();//just lets user make another guess
        }
        alphabet.remove(guess);//takes letter out of alphabet so no redoes happen
        if (word.contains(guess)) {
            for (int i = 0; i < word.length(); i++) {
                if (guess.equals(String.valueOf(word.charAt(i)))) {
                    lettersOfWord[i] = guess;
                    lettersInWord--;
                    lettersCorrect++;
                }
            }
        } else {
            guesses--;//takes a guess away
        }

        clearScreen();
    }
    if (guesses == 0) {//this comes up when you lose
        System.out.println("You have lost, the word was " + word);
    } else {//this comes up when you've guessed the word, if its not one its the other!!!
        System.out.println("You have won! the word was " + word + " good job dude. Please see\n"
                + "Mr. A for a free piece of cake and a 100 on your final.");
    }
}

// Simulate a clear screen
private static void clearScreen() {
    for(int clear = 0; clear < 1000; clear++) {
       System.out.println("\b") ;
    }
}