2017-10-04 52 views
-10

所以我非常難以做到這一點,我的hang子手程序的最後一部分。它涉及給用戶12輪和停止程序,如果他們猜測字中的所有字母。到目前爲止,我的程序可以告訴用戶他們輸入的字母是否在單詞中,在這樣做之後,它將字母存儲在一個數組中,我計劃使用商店中輸入的所有正確字母,並且如果該長度等於長度的數組持有單詞的長度,那麼我的程序將停止,但我真的很糟糕的循環,我一直在嘗試這樣做了大約5個小時,這是盡我所能。真的困惑於Java Hang子手程序

任何幫助,非常感謝。

import java.util.Scanner; 

public class LynchMan { 

     public void LynchBoy() { 
     Scanner scan = new Scanner(System.in); 

     System.out.println("Player 1 please enter one whole word "); 
     String word = scan.nextLine(); 
     //gets user input 

     String[] let= new String[word.length()]; 
     let = word.split(""); 
     //saves user input as separate letters 



     System.out.println("your word is "+ word.length()+ " letters long"); 
     for(int i =0; i<word.length();i++) { 
      System.out.print("_ "); 
      //prints out length of users word 
     } 

     String[] holder = new String[0]; 
     //variable holds letter input 
     System.out.println(""); 



     while (holder.length<word.length()) { 
     for(int x = 0;x<12;x++) { 
     System.out.println("Player 2 You have 12 attempts "); 
     String letter = scan.nextLine(); 
     for(int i=0;i<=word.length(); i++) { 
      if (word.charAt(i) == letter.charAt(0)) { 
       System.out.println("Your charcter is here"); 
       for(int d=0;d<holder.length;d++) { 
        holder[d]=letter; 
       } 
      }else{ 

      } 
     } 

    } 
     } 

     } 

} 
+4

請閱讀[爲什麼「有人可以幫我嗎?」不是一個實際的問題?](http://meta.stackoverflow.com/questions/284236/why-is-can-someone-help-me-not-an-actual-question)我們不知道你在做什麼,不知道,或者除非你告訴我們,否則你堅持不懈。 – azurefrog

+1

使你的代碼可讀。很難理解這裏發生了什麼,因爲變量名稱沒有說明變量代表什麼。 – bcsb1001

+1

你得到什麼錯誤或什麼不工作?我建議看看這個[如何調試小程序](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/),以尋求如何解決問題的幫助。 – Confuzing

回答

0

我檢查你的代碼,並提出了一些修改它。它可能看起來效率低下,但它的工作。

import java.util.Scanner; 

public class LynchMan { 

    public static void main() { 
     Scanner scan = new Scanner(System.in); 

     System.out.println("Player 1 please enter one whole word "); 
     String word = scan.nextLine(); 
     //gets user input 

     String[] let= new String[word.length()]; 
     let = word.split(""); 
     //saves user input as separate letters 



     System.out.println("your word is "+ word.length()+ " letters long"); 
     for(int i =0; i<word.length();i++) { 
      System.out.print("_ "); 
      //prints out length of users word 
     } 
     String check[] = new String[word.length()]; 
     for(int k=0;k<word.length();k++) 
     check[k]=word.substring(k,k+1); 
     String[] holder = new String[word.length()]; 
     for(int q=0;q<word.length();q++) 
     holder[q]="_"; 
     //variable holds letter input 
     System.out.println(""); 
     int count=0; 


     boolean cr=false; 
      for(int x = 12;x>0;x--){ 
      System.out.println("Player 2 You have "+x+" attempts "); 
      String letter = scan.nextLine(); 
      for(int i=0;i<word.length(); i++) { 
       if (check[i].equalsIgnoreCase(letter.substring(0,1))) { 
       System.out.println("Your charcter is here"); 
       holder[i]=check[i]; 
       check[i]="*"; 
       count++; 
      } 
      else 
      { 
       System.out.println("Your Character is not here"); 
       } 
      } 
      for(int w=0;w<holder.length;w++) 
      System.out.print(holder[w]+" "); 
      System.out.println(); 
      if(count==word.length()) 
      { 
       System.out.println("You have won"); 
       break; 
      } 
     } 
     if(count!=word.length()) 
     System.out.println("You Lost, The word was,"+word); 
    } 
} 

我希望它有助於:)。

+0

感謝您的幫助 – Guy

0
Try this code. You had way to many nested for loops 



import java.util.Scanner; 

public class LynchMan { 

     private static Scanner scan = new Scanner(System.in); 
     public static String word; 
     public static void main(String[] args) { 


     System.out.println("Player 1 please enter one whole word "); 
     word = scan.nextLine(); 
     //gets user input 

     String[] let= new String[word.length()]; 
     let = word.split(""); 
     //saves user input as separate letters 



     System.out.println("your word is "+ word.length()+ " letters long"); 
     for(int i =0; i<word.length();i++) { 
      System.out.print("_ "); 
      //prints out length of users word 
     } 

     String[] holder = new String[0]; 
     //variable holds letter input 
     System.out.println(""); 
     System.out.println("Player 2 You have 12 attempts "); 
     Guess(); 
    } 


     public static void Guess() 
     { 
      boolean gameOver = false; 
      int counter = 0; 
      int win = 0; 
      while (!gameOver) 
      { 
       String letter = scan.nextLine(); 
       if (word.indexOf(letter.charAt(0)) >= 0) 
        { 
         System.out.println("Your character is here."); 
         win++; 



        } 
        else 
        { 
         System.out.println("Yours guess was wrong"); 
         counter++; 

        } 
       if (counter < 12) 
       { 
        gameOver = false; 
       } 
       if (win == word.length()) 
       { 
        gameOver = true; 
        System.out.println("YOU WON"); 

       } 
       else if (counter == 12) 
       { 
        gameOver = true; 
        System.out.println("YOU LOST"); 
       } 



      } 

     } 




} 
+0

感謝您的幫助,沒有給您打勾,因爲中國看起來像他需要更多 – Guy