if语句和else if语句都运行

时间:2017-03-02 22:39:51

标签: java if-statement

我是java编程的新手,并且一直致力于一个刽子手游戏'各种各样的。这是我的导师提出的练习,在完成基本版本之后,我想制作一个更先进的练习。到目前为止,我发现代码中的问题是if语句和else if语句都运行。为了解决这个问题,我添加了一些break语句。它确实帮助解决了一个问题,但这两个陈述仍然存在。

这是if else语句错误:

                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;

如果有帮助,这是完整的代码:

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("");
    }
}
}

提前感谢您的帮助。

2 个答案:

答案 0 :(得分:2)

问题在于你break。你不会在for循环中迭代,你总是打破

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++;

我还没有测试但是应该没问题

答案 1 :(得分:1)

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
    }