2017-03-02 123 views
1

我對Java編程更新,並且一直在處理各種hang子手遊戲。這是我的老師提出的一個練習,在完成基礎版本之後,我想創建一個更高級的練習。到目前爲止,我發現代碼中的問題是if語句和else語句都運行。爲了解決這個問題,我添加了一些break語句。它確實幫助解決了一個問題,但是兩個陳述仍然運行。if語句和其他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(""); 
    } 
} 
} 

預先感謝任何幫助。

+6

我不相信它們都運行,至少在循環的相同迭代中。根據語言規範,他們根本不能。 –

+1

請注意'else if(!guess.equals(letters [i])){'可以更容易地寫成else''。 –

+0

你爲什麼突破? – tramstheman

回答

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

我清理你的主要一點點,但我要離開一些工作回到你身邊。你可能應該把你的單詞列表初始化包裝成它自己的方法。實際上,通常建議可以將任何可剝離到自己的方法中的代碼塊這樣做。我不認爲你必須走得這麼極端,但有人說你不應該有超過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 
    }