-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.");
}
}
}
}
죄송합니다.
"while 루프를 실행하지 않습니다"는 의미를 설명합니다. – Blorgbeard
int choice = 1; // 실행하지 않아도됩니다. (선택! = 1) – Mechkov
Eclipse를 사용 중이라면 * 데드 코드 * 경고가 표시됩니다. 그리고 즉시 문제를 알았습니다. – user8