簡體   English   中英

if語句和else if語句都運行

[英]Both the if statement and the else if statements run

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

在此先感謝您的幫助。

問題是你的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++;

我沒有測試但應該沒問題

我稍稍清理了你的主要部分,但是我會把一些工作留給你。 您應該將單詞列表初始化之類的內容包裝到自己的方法中。 實際上,通常建議通常建議將任何可以剝離到自己方法中的代碼塊。 我不認為你必須走這么極端,但據說你永遠不應該有超過8行的方法。 有點武斷,但重點是像while循環中發生的事情可以放入自己的方法中。 如果您可以將一段代碼描述為執行單個任務,則應將其置於自己的方法中。 這也將在調試時幫助您,因為所有內容都模塊化為執行特定任務的代碼塊。 很容易分辨出問題所在,因為在對所有內容進行適當分解時,代碼應僅存在於相關的范圍內。 以單詞數組初始化為例,這會占用整個垂直空間,與獲取用戶輸入,驗證用戶輸入,顯示結果等無關。如果您正在調試某些功能,可能會讓人感到困惑。查看與它無關的代碼。

@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
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM