The basic Hangman game in Java (mainly related to string manipulation)

I take the intro class for Java, and we have a project that deals with the executioner game. I have most of the code, but there is an error that I cannot solve.

First, the program asks the user for a letter and one space in which they think that the letter is coming, then the program displays the word in a series of hyphens and, if the user makes the correct assumption, the letter in the corresponding hyphen is replaced by the letter.

For testing purposes, the default word was narrow.

So, if I guessed the letter rand for the space, I had to guess the index 2, the program would give me:

Guess a letter: r 

Guess a space:  2

Guess:    --r---

The problem I am facing is that when I guess the index 3 for a space and try to guess the next r, the program just gives me the same result as before.

We are not allowed to use arrays or a string builder because we have not talked about them yet.

Here are my variables:

    // default word for testing
    String blank = "narrow";

    // variables to manipulate the string
    String start = "";
    String end = "";
    String word = "";

    // variables for input and output
    // input is used for the user letter guess
    String input; 
    // space is used for the user index guess
    String space = "";
    // guess is used at the end to display the word to the user and set equal to word after
    // it has been manipulated
    String guess = "";

Here is the code that the string manipulates.

    for (int i = 0; i < blank.length(); i++) {
         if (i == blank.indexOf(input)) {
             start = guess.substring(0, i);
             end = guess.substring(i + 1);
             word = start.concat(input).concat(end);                    
         }
    }

I think this is due to if statement, but I tried some other things and they did not work. Any help would be appreciated.

Thank.

+4
source share
4 answers

, , blank.indexOf(input) 2 (indexOf of 'r', 2)

, , , , , . :

  • , . .
  • , .

: , , ​​ "------" ""

    // check if the space has the letter you guessed
    if (blank.charAt(space) == input.charAt(0)) {
         // if it has just update the pattern string to also contain the new letter
         guess = guess.substring(0, space) + input + guess.substring (space + 1)

( ) .

+2

:

//set up variables
Scanner keyboard = new Scanner(System.in);
String word = "narrow";
String display = "";
for (int i = 0; i < word.length(); i++) {
  display = display + "-";
}

//loop until the word is guessed
while (display.contains("-")) {

  //show the user flow, take input
  System.out.println("Guess: " + display);
  System.out.print("Guess a letter: ");
  String letter = keyboard.nextLine();
  System.out.print("Guess a space: ");
  String spaceStr = keyboard.nextLine();
  int space = Integer.parseInt(spaceStr);

  //check if the guess is right
  if (letter.equals(word.charAt(space) + "")) {

    //modify the string shown to the user
    String temp = display.substring(space + 1);
    display = display.substring(0, space);
    display = display + letter + temp;
  }
}

, , , . , , .

+1

, blank.indexOf(input) input. this indexOf(int ch, int fromIndex). char int, fromIndex.

int lastOccurrence = 0; 
for (int i = 0; i < blank.length(); i++) {
     if (i == blank.indexOf(input, lastOccurrence)) {
         lastOccurrence = i; 
         start = guess.substring(0, i);
         end = guess.substring(i + 1);
         word = start.concat(input).concat(end);                    
     }
}
+1

indexOf(String str) .

, , - , . , :

if(blank.charAt(space) == input.charAt(0))
{
     start = guess.substring(0, space);
     end = guess.substring(space + 1);
     word = start.concat(input).concat(end);
}
+1

Source: https://habr.com/ru/post/1656537/


All Articles