import java.util.*;
class DiceGame {
int diceFace;
int userGuess;
private void RollDice()
{
diceFace=(int)(Math.random() * 6) +1;
}
private int getUserInput(String prompt)
{
System.out.println(prompt);
Scanner s = new Scanner(System.in);
return s.nextInt();
}
private void checkUserGuess()
{
if(diceFace==userGuess)
System.out.println("맞았습니다");
else
System.out.println("틀렸습니다");
}
public void startPlaying()
{
int userGuess=getUserInput("예상값을 입력하시오: ");
RollDice();
checkUserGuess();
}
}
public class DiceGameTest {
public static void main(String[] args) {
DiceGame game=new DiceGame();
game.startPlaying();
}
}
할때마다 "틀렸습니다"가 뜨는데 뭐가 문젠거죠..ㅠㅜㅜ
class DiceGame {
int diceFace;
int userGuess;
private void RollDice()
{
diceFace=(int)(Math.random() * 6) +1;
}
private int getUserInput(String prompt)
{
System.out.println(prompt);
Scanner s = new Scanner(System.in);
return s.nextInt();
}
private void checkUserGuess()
{
if(diceFace==userGuess)
System.out.println("맞았습니다");
else
System.out.println("틀렸습니다");
}
public void startPlaying()
{
int userGuess=getUserInput("예상값을 입력하시오: ");
RollDice();
checkUserGuess();
}
}
public class DiceGameTest {
public static void main(String[] args) {
DiceGame game=new DiceGame();
game.startPlaying();
}
}
할때마다 "틀렸습니다"가 뜨는데 뭐가 문젠거죠..ㅠㅜㅜ
int userGuess=getUserInput(\"예상값을 입력하시오: \"); ->this.userGuess=getUserInput(\"예상값을 입력하시오: \");
멤버필드 userDice가 값이 없어
밑에쓰인 userGuess 지역변수라서 메서드 끝나면 사라지는거 위에 선언한 userGuess와는 다른 변수라고 생각하면 됨여
앞에 int만 빼고 걍 .userGuess=getUserInput(\"예상값을 입력하시오: \"); 라고만 써도 자동으로 this가 붙음 그래서 userGuess=getUserInput(\"예상값을 입력하시오: \"); 라고만 써도 되구
윗글 앞에.은 오타고 userGuess=getUserInput(\"예상값을 입력하시오: \");