简体   繁体   中英

Both the if statement and the else if statements run

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.

The problem is your break . You don't iterate in the for loop, you always break

for (int i = 0; i < word.length(); i++){
    boolean found = false;
    if (guess.equals(letters[i])){
        wordi[i] = guess.charAt(i);
        System.out.println("Included");
        found = true;
    }
    else if (!guess.equals(letters[i])){
        wordi[i] = '*';
    }
}
if (!found)
     wrong_guess++;
num_guess++;

I haven't tested but should be okay

I cleaned up your main a little bit, however I am going to leave some of the work back to you. You should probably wrap things like your words list initialization into its own method. In fact, generally any chunk of code that can be stripped out into its own method is usually recommended to do so. I don't think you have to go this extreme, but it has been said that you should never have a method longer than 8 lines. A bit arbitrary, but the point is that things such as what is happening inside the while loop can be put into its own method. If you can describe a block of code as performing a single task, it should be put into its own method. This will also help you while debugging because everything is modularized into chunks of codes that perform specific duties. It is easy to tell where an issue is because when everything is properly decompositioned, code should only exist in a scope where it is relevant. To use your word array initialization as an example, this takes up allot of vertical space and has nothing to do with taking user input, validating user input, displaying results, etc. If you are debugging some of that functionality, it can be confusing to view code that has nothing to do with it.

@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 num_guess = 1;
        String word = words[(int)(Math.random()*10)]; // one lined this
        for(Character ch : word.toCharArray()) out_word += '*'; // loop through the char array and fill outword with *

        System.out.println("The word is " + word);
        boolean success = false;

        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 && !success)// we want to exit on success as well
        {
            display(wrong_guess);
            System.out.println(out_word);
            System.out.print("Enter a guess: ");
            char guess = input.nextLine().toCharArray()[0]; // one line this, and it isn't used anywhere else so I moved this to the while loop
            System.out.println("Guess: " + guess);

            if(word.contains(String.valueOf(guess))) // if the word contains the character
            {
                System.out.println("Included");
                String temp = "";
                for(int i = 0; i < out_word.length(); i++)
                {
                    temp += (word.charAt(i) == guess) ? guess : out_word.charAt(i); // "unveils" the correct characters
                }
                out_word = temp;
                System.out.println("Outword: " + out_word);
            }
            else // else not else if
            {
                System.out.println("Not Included");
                wrong_guess++;
            }
            num_guess++; // don't need to put this in the if and else
            success = word.equals(out_word);
        }
        if(success) System.out.println("You Won!"); // success
        else System.out.println("You Lost!"); // failure
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM