2016-10-02 3 views
0

私はこのハングマンプログラムを持っていますが、再度プレイしたいかどうかをユーザーに確認する際に問題があります。どうすればこの問題を解決できますか?将来の返信をありがとう。 パッケージハングマン。私は編集がこれで大丈夫だと願っています。if文を使用したループの問題

import java.io.File; import java.io.FileNotFoundException;

import java.util.Scanner;

public class Hangman { 

static Scanner keyboard = new Scanner(System.in); 
static int size, size2; 
static boolean play = false; 
static String word; 
static String[] ARRAY = new String[0]; 
static String ANSI_RESET = "\u001B[0m"; 
static String ANSI_BLUE = "\u001B[34m"; 
static String ANSI_GREEN = "\u001B[32m"; 
static String ANSI_RED = "\u001B[31m"; 
static String ANSI_LIGHTBLUE = "\u001B[36m"; 
//^declarations for variables and colors for display^ 

public static void main(String[] args) { 

    randomWordPicking(); 
    //^calls method^ 
} 

public static void setUpGame() { 

    System.err.printf("Welcome to hangman.\n"); 

    try { 

     Scanner scFile = new Scanner(new File("H:\\HangMan\\src\\hangman\\HangMan.txt")); 
     String line; 
     while (scFile.hasNext()) { 
      line = scFile.nextLine(); 
      Scanner scLine = new Scanner(line); 
      size++; 
     } 
     ARRAY = new String[size]; 
     Scanner scFile1 = new Scanner(new File("H:\\HangMan\\src\\hangman\\HangMan.txt")); 
     while (scFile1.hasNext()) { 
      String getWord; 
      line = scFile1.nextLine(); 
      Scanner scLine = new Scanner(line); 
      word = scLine.next(); 
      ARRAY[size2] = word; 
      size2++; 
    //calls method for picking word^ 
     } 
    } catch (FileNotFoundException e) { 
     System.out.println(e); 
    } 
} 

public static void randomWordPicking() { 


    setUpGame(); 

    int LEFT = 6; 

    do { 

     int random = (int) (Math.random() * ARRAY.length); 

     //^genertates a random number^ 


     String randomWord = ARRAY[random]; 
     String word = randomWord; 

     //^chosses a random word and asgins it to a variable^ 

     char[] ranWord = randomWord.toCharArray(); 

     char[] dash = word.toCharArray(); 

     //^Creates a char array for the random word chosen and for the dashs which are displayed^ 



     for (int i = 0; i < dash.length; i++) { 
      dash[i] = '-'; 

      System.out.print(dash[i]); 

      //^displays dashs to the user^ 
     } 

     for (int A = 1; A <= dash.length;) { 

      System.out.print(ANSI_BLUE + "\nGuess a Letter: " + ANSI_RESET); 
      String userletters = keyboard.next(); 

      //^gets user input^ 

      if (!userletters.equalsIgnoreCase(randomWord)) { 

       //^allows program to enter loop if user has entered a letter^ 
       for (int i = 0; i < userletters.length(); i++) { 

        char userLetter = userletters.charAt(i); 

        String T = Character.toString(userLetter); 

        for (int B = 0; B < ranWord.length; B++) { 

         //^converts users input to a char and to a string^  
         if (userLetter == dash[B]) { 

          System.err.println("This " + userLetter + "' letter already exist"); 
          B++; 
          //^tells user if the letter they entered already exists^ 
          if (userLetter == dash[B - 1]) { 
           break; 
          } 
         } else if (userLetter == ranWord[B]) { 
          dash[B] = userLetter; 
          A--; 
         } 
        } 
        if (!(new String(ranWord).contains(T))) { 
         LEFT--; 
         System.out.println("You did not guess a correct letter, you have " + LEFT + " OF " 
           + dash.length + " trys left to guess correctly"); 
        } 
        //^shows how many trys the user has left to get the word right before game ends^ 
        System.out.println(dash); 
        if (LEFT == 0) { 
         System.out.println("The word you had to guess was " + word); 
         break; 
        } 
        //^shows the user the word if they didnt guess correctly^ 
       } 
      } else { 
       System.out.println(ANSI_GREEN + "\nYou have guessed the word correctly!" + ANSI_RESET); 
       break; 
      } 
      if (LEFT == 0) { 
       break; 
      } 
      if ((new String(word)).equals(new String(dash))) { 
      System.out.println(ANSI_GREEN + "\nYou have guessed the word correctly!" + ANSI_RESET); 
       break; 
      } 
     } 
    //^if user enters the word it will check and then display that they got the word correct^ 
     System.out.println("Play agian? (y/n)"); 
     String name = keyboard.next(); 
     //^asks user if they want to play again^ 
     if (name.equalsIgnoreCase("n")) { 
      play = true; 
      return; 
     } 

     //^stops program if user enters n to stop game^ 
    } while (play = false); 

} 

}

+0

限り、私はあなたが 'PLAYON()'メソッドを呼び出したい理解として。私はしかし、あなたはそれをどこに呼んでいるのかわかりません。 – AlexR

+0

@AlexRメソッドからのコードを、どこに置いておくべきですか –

答えて

1

あなたは2つの変数を比較したい場合は、割り当てを意味=演算子を使用しない代わりに

while (play = false) 
+0

ありがとうございました!! :) –

1

while (!play) 

を使用する必要があります。代わりに==を使用してください。また、あなたのケースでは!=次のようになります。

while (play != false)