2016-10-10 5 views
-2

while 및 for 루프를 사용하여 추측 게임의 두 가지 버전을 생성하도록 요청하는 클래스 프로젝트에서 작업하고 있습니다. 과제의 마지막 부분은 GuessGame 클래스로 만든 두 가지 버전의 게임을 호출하는 Driver 클래스를 만드는 것입니다. 어떤 이유로 드라이버가 실행될 때 while 루프를 실행하지 않아 사용자가 사용하려는 게임 버전을 선택하도록 선택할 수 없습니다. 어떤 도움이 필요합니까?왜 내 드라이버가 while 루프를 실행하지 않습니까?

GuessGame 코드 :

import java.util.Scanner; 

public class GuessGame 
{ 
    public static void example() 
    { 
     Scanner in = new Scanner(System.in); 
     System.out.println("Give me a number:"); 
     int num = in.nextInt(); 
     System.out.println("You gave me " + num); 
    } 

    public static void roll1() 
    { 
     int guess = 1; 
     int randomNumber = (int) (Math.random() * 10) + 1; 
     Scanner scan = new Scanner(System.in); 
     boolean win = true; 

     System.out.println("I'm thinking of a number between 1 and 10."); 

     while (win) 
     { 
      System.out.println("What is your guess?"); 
      guess = scan.nextInt(); 
      if (guess == randomNumber) 
      { 
       System.out.println("You guessed it!"); 
       break; 
      } 
      else if (guess > randomNumber) 
      { 
       System.out.println("Smaller!"); 
      } 
      else if (guess < randomNumber) 
      { 
       System.out.println("Bigger!"); 
      } 
     } 
    } 

    public static void roll2(int num) 
    { 
     int guess = 0; 
     int randomNum = (int) (Math.random() * 20) + 5; 
     Scanner scan = new Scanner(System.in); 
     boolean win = true; 

     System.out.println("I'm thinking of a number between 5 and 25"); 

     for (int guessNum = 0; guessNum < num; guessNum++) 
     { 
      System.out.println("What is your guess?"); 
      guess = scan.nextInt(); 
      if (guess == randomNum) 
      { 
       System.out.println("You guessed it!"); 
       break; 
      } 
      else if (guess > randomNum) 
      { 
       System.out.println("Smaller!"); 
      } 
      else if (guess < randomNum) 
      { 
       System.out.println("Bigger!"); 
      } 
     } 
    } 
} 

드라이버 코드 :이 사이트를 사용하여 서식 문제, 처음

import java.util.Scanner; 

public class Driver 
{ 
    public static void main(String[] args) 
    { 
     Scanner in = new Scanner(System.in); 

     GuessGame game = new GuessGame(); 

     int choice = 1; 

     System.out.println("Guessing Game"); 
     System.out.println("-------------"); 

     while (choice != 1) 
     { 
      System.out.println("1. Version 1"); 
      System.out.println("2. Version 2"); 
      System.out.println("3. Quit"); 

      System.out.println("\nPlease enter your choice > "); 
      choice = in.nextInt(); 
      System.out.println(); 

      switch (choice) 
      { 
       case 1: 
        System.out.println("Version 1:"); 
        game.roll1(); 
        break; 
       case 2: 
        System.out.println("Version 2:"); 
        game.roll2(5); 
        break; 
       case 3: 
        System.out.println("Thanks for playing."); 
        break; 
       default: 
        System.out.println("Invalid choice."); 
      } 
     } 
    } 
} 

죄송합니다.

+1

"while 루프를 실행하지 않습니다"는 의미를 설명합니다. – Blorgbeard

+1

int choice = 1; // 실행하지 않아도됩니다. (선택! = 1) – Mechkov

+0

Eclipse를 사용 중이라면 * 데드 코드 * 경고가 표시됩니다. 그리고 즉시 문제를 알았습니다. – user8

답변

0

귀하의 while 조건은 사실이 아닙니다.

당신은 choice

int choice = 1; 

를 선언 한 다음 말 :

while (choice != 1) 
{ 
    //stuff 
} 

이 선택은 = 1로, 루프 자체를 완료 결코 선언 때문이다.

int choice = -1; 

을 다음과 같이 대안은 선택의 여지를 선언하는 것입니다 또 다른이 do while 구조를 사용하는 것입니다.

관련 문제