2016-10-02 2 views
0

나는이 교수형 집행 프로그램을 사용하고 있지만 사용자에게 다시 재생할 것인지 묻는 문제가 있으며 항상 프로그램을 종료합니다. 이 문제를 어떻게 해결할 수 있습니까? 미래의 답장을 보내 주셔서 감사합니다. 패키지 교수형 집행 인. 나는 이걸로 편집이 잘 되었으면 좋겠다.if 문을 사용하여 루프 문제가 발생했습니다.

import java.io.File; 가져 오기 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

당신은 할당을 의미 = 연산자를 사용하지 않는,

while (!play) 

대신

while (play = false) 
+0

고맙습니다. –

1

의 사용해야합니다. 대신 ==을 사용하십시오. 또한 귀하의 경우에는 !=이어야합니다 :

while (play != false) 
관련 문제