Java if语句和else if语句都运行

Java if语句和else if语句都运行,java,if-statement,Java,If Statement,我是java编程新手,一直在做类似于刽子手的“游戏”。这是我的导师提出的练习,在完成基本版本后,我想做一个更高级的。到目前为止,我发现代码中的问题是if语句和elseif语句都在运行。为了解决这个问题,我添加了一些break语句。它确实有助于解决一个问题,但两个语句仍在运行 以下是错误的if-else语句: if (guess.equals(letters[i])){ wordi[i] = guess.charAt(i);

我是java编程新手,一直在做类似于刽子手的“游戏”。这是我的导师提出的练习,在完成基本版本后,我想做一个更高级的。到目前为止,我发现代码中的问题是if语句和elseif语句都在运行。为了解决这个问题,我添加了一些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("");
    }
}
}
import java.util.Scanner;
公开课考试{
公共静态扫描仪输入=新扫描仪(System.in);
@SuppressWarnings({“未使用”})
公共静态void main(字符串[]args){
字符串[]字=新字符串[10];
字[0]=“主席”;
单词[1]=“苹果”;
文字[2]=“熊”;
单词[3]=“单词”;
字[4]=“表”;
字[5]=“奶牛”;
字[6]=“白菜”;
词语[7]=“食品”;
字[8]=“计算机”;
文字[9]=“鼠标”;
int cap_guess=6;
int错误_猜测=0;
int n=(int)(Math.random()*10);
字符串字=字[n];
串出单词“”;
int num_guess=1;
对于(int count=0;count虽然(num_guess问题在于你的
中断
。你不在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++;
for(int i=0;i

我还没有测试,但应该可以

我稍微清理了一下你的main,但是我将把一些工作留给你。你可能应该将单词列表初始化之类的东西包装到自己的方法中。事实上,通常建议你这样做。我不这么认为我不认为你必须走极端,但有人说你不应该有一个超过8行的方法。这有点武断,但关键是while循环中发生的事情可以放在它自己的方法中。如果你可以将一块代码描述为执行单个任务,它应该放在它自己的方法中。这在调试时也将帮助您,因为所有内容都被模块化为执行特定任务的代码块。很容易判断问题出在哪里,因为当所有内容都正确分解时,代码应该只存在于相关的范围内。以字数组初始化为例,这将占用vertic的Alloctal空间,与获取用户输入、验证用户输入、显示结果等无关。如果您正在调试某些功能,则查看与之无关的代码可能会造成混乱

@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
    }
@SuppressWarnings({“unused”})
公共静态void main(字符串[]args)
{
字符串[]字=新字符串[10];
字[0]=“主席”;
单词[1]=“苹果”;
文字[2]=“熊”;
单词[3]=“单词”;
字[4]=“表”;
字[5]=“奶牛”;
字[6]=“白菜”;
词语[7]=“食品”;
字[8]=“计算机”;
文字[9]=“鼠标”;
int cap_guess=6;
int错误_猜测=0;
int num_guess=1;
字符串单词=单词[(int)(Math.random()*10)];//一行
for(Character ch:word.toCharArray())out_word+='*';//在字符数组中循环并用*
System.out.println(“单词是”+单词);
布尔成功=假;
String[]字母=新字符串[word.length()];
for(int i=0;i虽然(num_guess)我不相信它们都在运行,至少在循环的同一个迭代中。根据语言规范,它们根本不能运行。请注意,
else if(!guess.equals(字母[I]){
可以像
else{
那样更容易编写。为什么要中断呢?同样,在if{}结束时也不需要中断blockI与@AndyTurner一起在这一个上运行。它同时运行这两个,但在循环中不在同一个运行上。