2017-11-22 10 views
-11

ゲームを終了したときに再びプレイしたいかどうかをユーザーに尋ねます。現在、それらを実行する唯一の方法は、プログラムを再度呼び出すことです。しかし、私は彼らがゲームを終えた後にもう一度遊びたいかどうかをユーザーに促したいと思います。キーボードから受信ワードを使ってJavaを再起動する方法

public class Hangman 
{ 

    Random r; 
    GetData get; 
    String[] words = {"eat","what" }; 
    String word; 
    boolean finished = false; 
    int badGuessCount=0; 
    boolean [] foundLetters; 
    String entryWord =" "; 

    public Hangman() 
    { 
     r = new Random(); 
     get = new GetData(); 
     playAGame(); 
    } 

    public void playAGame() 
    { 

     word = words[r.nextInt(words.length)]; 

     foundLetters = new boolean[word.length()]; 

     while (!finished) 
     { 
      showGallows(); 
      showWord(); 
      getGuess(); 
      checkGuess(); 
      if (badGuessCount==6) 
      { 
       System.out.print('\u000C'); 
       showGallows(); 
       System.out.println("Sorry, but you lost."); 
       System.out.println("The word was: "+word); 
       finished=true; 
      } 
     } 
    } 

    public void showGallows() 
    { 
     System.out.print('\u000C'); 
     if (badGuessCount==0) 
      man_0(); 
     if (badGuessCount==1) 
      man_1(); 
     if (badGuessCount==2) 
      man_2(); 
     if (badGuessCount==3) 
      man_3(); 
     if (badGuessCount==4) 
      man_4(); 
     if (badGuessCount==5) 
      man_5(); 
     if (badGuessCount==6) 
      completedMan(); 



     System.out.println("\n"); 
    } 

    public boolean showWord() 
    { 
     boolean goodGuess = false; 
     char ch = entryWord.charAt(0); 
     for (int lc=0; lc < word.length(); lc++) 
      if (foundLetters[lc]==true) 
      { 
       System.out.print(word.charAt(lc)+" "); 
      } 
      else if (word.charAt(lc)==ch) 
        { 
         System.out.print(word.charAt(lc)+" "); 
         foundLetters[lc] = true; 
         goodGuess = true; 
        } 
        else 
         System.out.print("_ "); 
     return goodGuess; 
    } 

    public void getGuess() 
    { 
     System.out.println("\n\n\nWhat letter do you want to guess?"); 
     System.out.println("Type the whole word to guess the word."); 
     System.out.println("You have "+(6 - badGuessCount)+ "guess left."); 
     System.out.print("Enter guess"); 
     entryWord = get.aWord(); 
    } 

    public void checkGuess() 
    { 
     boolean goodGuess; 
     if (entryWord.length()>1) 
     { 
      if (entryWord.equals(word)) 
      { 
       System.out.println("\n\nYes You won!"); 
       finished = true; 
       System.out.println("close and run if you want to play again!"); 
       String pause = get.aWord(); 
      }  
    } 
    else 
    { 
     showGallows(); 
     goodGuess = showWord(); 
     if (goodGuess) 
     { 
      System.out.println("\n\n\nGood guess"); 
      System.out.println("Press the Enter key to continue!"); 
      String pause = get.aWord(); 
     } 
      else 
      { 
      badGuessCount++; 
      System.out.println("\n\n\nBad guess!"); 
      System.out.println("Press the Enter key to continue!"); 
      String pause = get.aWord(); 
      } 
     } 

    } 

    //public void completedMan() 


} 

ユーザーに再度プレイを促し、入力に基づいてゲームを再開するにはどうすればよいですか?

+0

を行うために、私は、私はプログラムは、私は再びそれを実行するために閉じて、オープンする必要がありますが、私は彼らからいくつかの単語を受け取るとき、それは自分自身を再起動する必要終え再生するとき、これは私のプログラムのコードである私のプログラムを再起動するとしますどのような私のキーボード – forkspoon

+0

だから、ループを書く - 私は問題が何かを理解していない。また、なぜ 'switch'ではなく' if'文を使っていますか? – EJoshuaS

+0

ループを書きますが再起動しません – forkspoon

答えて

0

playAGame();自体をループ内に配置する必要があります。

do 
{ 
    playAGame(); 
} while (UserWantsToKeepPlaying()); 

private boolean UserWantsToKeepPlaying() { 
    // Ask the user if they want to keep playing 
} 

私はdo示唆 - あなたは常に少なくとも一度ゲームをプレイするだろう、とあなたは(おそらく)、彼らはゲームをプレイし終わった後まで再びプレーを促すたくないのでwhileループを。

+0

私はそのコードを使用していますが復帰プログラムでスタックしたときに何かが必要な場合ブール値 – forkspoon

+0

を理解してくれてありがとう – forkspoon

0

while(UserWantsToKeepPlaying());私は

do 
    { 
     playAGame(); 
    } while (UserWantsToKeepPlaying()); 
} 

public boolean UserWantsToKeepPlaying(int number) 
{ 
    for (int i = 1; i > number; i--) 
    if (number % i == 0 && i != number) return false; 
    else return true; 
    System.out.print("\n\n Play Again 'No' Press 0 Yes' Press 1 : "); 
} 
関連する問題