I am trying to write a code to play hangman and it is working correctly but every time when I input a character, it resets my output. Can someone please help.
my code:
import java.util.*;
public class game
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
String list[] = {"apple", "banana", "mango", "kiwi", "coconut", "papaya", "lichi", "strawberry", "orange", "cherry"};
int rand = (int)(Math.random()*9) 0;
String word = list[rand];
String ask = "_";
for(int i = 1; i < word.length();i ){
ask = ask "_";
}
System.out.println(ask);
System.out.println("hint: It is a fruit");
for (int j = 1; j<=15; j ){
System.out.println("Enter a character: ");
char input = in.next().charAt(0);
for (char i : word.toCharArray()){
if(input == i){
System.out.print(input);
continue;
}
System.out.print("_");
}
}
}
}
A small piece of the output:
______
hint: It is a fruit
Enter a character:
a
__a___
Enter a character:
o
o_____
Enter a character:
r
_r____
Enter a character:
n
___n__
Enter a character:
When I enter 'a' it prints it correctly but when I enter some other character it prints that character an not 'a'. Can somebody pls tell me what should I do to get the correct output.
CodePudding user response:
It looks like you are not saving the string with the character added to the game, you are only printing it. You will probably want to do something like add the new character to the string variable _ask rather than printing as you go, then print after the for loop has run. Basically you are not storing the past rounds anywhere.
CodePudding user response:
As mentioned in the other answer you need to remember the characters from previous attempts. This could for example be done like this:
String tempAsk = "";
for (char i : word.toCharArray()){
if(input == i){
tempAsk = i;
} else {
tempAsk = ask.charAt(i);
}
}
ask = tempAsk;
System.out.println(ask);
CodePudding user response:
I think that,
In the loop for (char i : word.toCharArray())
,
you should add the character to ask
(or have another string variable named ans
),
and then print ask
at the end of the loop
because you are not updating the value of ask and printing the place of the character in the string, and when the loop runs a second time it doesn't show the last character that u entered
plus you can have specific hints according to the fruit name using switch case
and maybe have an error pop up when the player enters the wrong character
CodePudding user response:
You can use a character array to check what letters are present so far like so:
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String list[] = {"apple", "banana", "mango", "kiwi", "coconut", "papaya", "lichi", "strawberry", "orange", "cherry"};
int rand = (int)(Math.random()*9) 0;
String word = list[rand];
// Create a character array to store the result so far
char[] result = new char[word.length()];
//Fill the array with _
Arrays.fill(result, '_');
System.out.println(new String(result));
System.out.println("hint: It is a fruit");
int numChances = 15;
for (int j = 1; j <= numChances; j ){
System.out.println("Enter a character: ");
char input = in.next().charAt(0);
for (int i = 0; i < word.length(); i ) {
if(word.charAt(i) == input){
result[i] = input;
}
}
String untilNow = new String(result);
System.out.println(untilNow);
if(untilNow.equalsIgnoreCase(word)) {
System.out.println("You win...");
break;
}
}
}