I am newer to java programming and have been working on a hangman 'game' of sorts. This was an exercise proposed by my instructor and after completing the basic version, I wanted to make one that is more advanced. So far I have found that the problem in the code is that both the if statement and the else if statements run. To try to fix this, I added some break statements. It did help with one problem but both statements still run.
Here is the if else statement that is faulty:
if (guess.equals(letters[i])){
wordi[i] = guess.charAt(i);
System.out.println("Included");
break;
}
else if (!guess.equals(letters[i])){
wordi[i] = '*';
wrong_guess++;
num_guess ++;
System.out.println("Not included");
break;
Here is the full code if it helps:
import java.util.Scanner;
public class Test {
public static Scanner input = new Scanner(System.in);
@SuppressWarnings({ "unused" })
public static void main(String[] args) {
String[] words = new String[10];
words[0] = "chair";
words[1] = "apple";
words[2] = "bear";
words[3] = "word";
words[4] = "table";
words[5] = "cow";
words[6] = "cabbage";
words[7] = "food";
words[8] = "computer";
words[9] = "mouse";
int cap_guess = 6;
int wrong_guess = 0;
int n = (int)(Math.random()*10);
String word = words[n];
String out_word = "";
int num_guess = 1;
for(int count = 0; count < word.length(); count ++){
out_word += "*";
}
boolean success = false;
String guess = "";
String[] letters = new String[word.length()];
for (int i = 0; i < letters.length; i++){
letters[i] = word.substring(i,i+1);
System.out.println(letters[i]);
}
while (num_guess <= cap_guess){
display(wrong_guess);
System.out.println(out_word);
System.out.print("Enter a guess: ");
guess = input.nextLine();
guess = guess.trim();
guess = guess.toLowerCase();
System.out.println("Guess: " + guess);
char[] wordi = out_word.toCharArray();
if (guess.length() == 1){
for (int i = 0; i < word.length(); i++){
if (guess.equals(letters[i])){
wordi[i] = guess.charAt(i);
System.out.println("Included");
break;
}
else if (!guess.equals(letters[i])){
wordi[i] = '*';
wrong_guess++;
num_guess ++;
System.out.println("Not included");
break;
}
}
out_word += wordi;
}
}
/*System.out.println(word);
System.out.println(out_word);*/
}
public static void display (int wrong_guess){
if (wrong_guess == 0){
System.out.println("_____________________");
System.out.println(" *----------, |");
System.out.println(" | | |");
System.out.println(" | |");
System.out.println(" | |");
System.out.println(" | |");
System.out.println(" | |");
System.out.println(" | |");
System.out.println(" | |");
System.out.println(" | |");
System.out.println(" | |");
System.out.println(" | |");
System.out.println("______________/-\\____|");
System.out.println("");
}
if (wrong_guess == 1){
System.out.println("_____________________");
System.out.println(" *----------, |");
System.out.println(" | | |");
System.out.println(" /=\\ | |");
System.out.println(" |. .| | |");
System.out.println(" \\-/ | |");
System.out.println(" | |");
System.out.println(" | |");
System.out.println(" | |");
System.out.println(" | |");
System.out.println(" | |");
System.out.println(" | |");
System.out.println("______________/-\\____|");
System.out.println("");
}
if (wrong_guess == 2){
System.out.println("_____________________");
System.out.println(" *----------, |");
System.out.println(" | | |");
System.out.println(" /=\\ | |");
System.out.println(" |. .| | |");
System.out.println(" \\-/ | |");
System.out.println(" | | |");
System.out.println(" | | |");
System.out.println(" | | |");
System.out.println(" | | |");
System.out.println(" | |");
System.out.println(" | |");
System.out.println("______________/-\\____|");
System.out.println("");
}
if (wrong_guess == 3){
System.out.println("_____________________");
System.out.println(" *----------, |");
System.out.println(" | | |");
System.out.println(" /=\\ | |");
System.out.println(" |. .| | |");
System.out.println(" \\-/ | |");
System.out.println(" | | |");
System.out.println(" |\\ | |");
System.out.println(" | \\ | |");
System.out.println(" | | |");
System.out.println(" | |");
System.out.println(" | |");
System.out.println("______________/-\\____|");
System.out.println("");
}
if (wrong_guess == 4){
System.out.println("_____________________");
System.out.println(" *----------, |");
System.out.println(" | | |");
System.out.println(" /=\\ | |");
System.out.println(" |. .| | |");
System.out.println(" \\-/ | |");
System.out.println(" | | |");
System.out.println(" /|\\ | |");
System.out.println(" / | \\ | |");
System.out.println(" | | |");
System.out.println(" | |");
System.out.println(" | |");
System.out.println("______________/-\\____|");
System.out.println("");
}
if (wrong_guess == 5){
System.out.println("_____________________");
System.out.println(" *----------, |");
System.out.println(" | | |");
System.out.println(" /=\\ | |");
System.out.println(" |. .| | |");
System.out.println(" \\-/ | |");
System.out.println(" | | |");
System.out.println(" /|\\ | |");
System.out.println(" / | \\ | |");
System.out.println(" | | |");
System.out.println(" / | |");
System.out.println(" / | |");
System.out.println("______________/-\\____|");
System.out.println("");
}
if (wrong_guess == 6){
System.out.println("_____________________");
System.out.println(" *----------, |");
System.out.println(" | | |");
System.out.println(" /=\\ | |");
System.out.println(" |x x| | |");
System.out.println(" \\-/ | |");
System.out.println(" | | |");
System.out.println(" /|\\ | |");
System.out.println(" / | \\ | |");
System.out.println(" | | |");
System.out.println(" / \\ | |");
System.out.println(" / \\ | |");
System.out.println("______________/-\\____|");
System.out.println("");
}
}
}
Thanks in advance for any help.
Aucun commentaire:
Enregistrer un commentaire