MossDog :
I created a console project which is a simple game of hangman. Here is what I have done so far...
static String answer;
static String display = "";
public static void main(String[] args) {
System.out.println("please enter the word or phrase to be guessed");
answer = input.next();
System.out.print("Please make a guess: ");
//loop to check each character in the string
for(int i = 0; i < answer.length(); i++){
if(answer.charAt(i) != ' ') {
//adds an underscore to the String that will be printed
display += "_";
}//end if
else {
//adds a space to the String that will be printed
display += " ";
}//end else
}//end for
System.out.print(display);
}
What I'm trying to is to go through some text that the user inputs and print an underscore in the case of a letter and otherwise make a space.
- Sample input: I like dogs
- Sample output: Please make a guess: _ ____ ____
I think i am going about adding a space to the String in the wrong way and can't find anyone with the same problem.
What is actually happening is shown below,
- Sample input: I like Dogs
- Sample output: Please make a guess: _
- Sample input: this is broken
- Sample output: Please make a guess: ____
What it is doing is printing the right amount underscores until it encounters the first space and then it stops.
Delphi1024 :
input.next()
only reads until the first space ' '. If you want to read a whole line you need to use input.nextLine()
Guess you like
Origin http://43.154.161.224:23101/article/api/json?id=334601&siteId=1