0
내 수학 게임의 문제에 대해 생성하는 난수가 항상 0이라는 사실을 제외하면 내 프로그램이 완벽하게 작동합니다. 나는 그것을 얻지 않는다. 나는 math.random을 사용해 보았는데 같은 문제가있다. 뭐하는거야?임의 숫자는 항상 0입니까?
import java.util.Scanner;
import java.util.Random;
public class Problem {
private int numberOfQuestions;
private int answer;
private int userAnswer;
private int score;
private int max;
private int min;
private static String question;
Random rand = new Random();
Scanner input = new Scanner(System.in);
private int num1 = rand.nextInt((max - min) + 1) + min;
private int num2 = rand.nextInt((max - min) + 1) + min;
public Problem(int max, int min, int numberOfQuestions){
this.max = max;
this.min = min;
this.numberOfQuestions = numberOfQuestions;
}
public int multiplyNumbers()
{
answer = num1 * num2;
return answer;
}
public String multiplyQuestion(){
String sum = ("What is " + num1 + " * " + num2 + "?");
return sum;
}
public int addNumbers()
{
answer = num1 + num2;
return answer;
}
public String addQuestion(){
String sum = ("What is " + num1 + " + " + num2 + "?");
return sum;
}
public int subtractNumbers()
{
answer = num1 - num2;
return answer;
}
public String subtractQuestion(){
String sum = ("What is " + num1 + " - " + num2 + "?");
return sum;
}
public int divideNumbers()
{
answer = num1/num2;
return answer;
}
public String divideQuestion(){
String sum = ("What is " + num1 + "/" + num2 + "?");
return sum;
}
public String answer(int min, int max, int numberOfQuestions){
int operator = (int)(Math.random()*4) + 1;
if(operator == 1){
question = addQuestion();
answer = addNumbers();
} else if(operator == 2) {
question = subtractQuestion();
answer = subtractNumbers();
} else if(operator == 3) {
question = divideQuestion();
answer = divideNumbers();
} else if(operator == 4) {
question = multiplyQuestion();
answer = multiplyNumbers();
}
System.out.println(question);
userAnswer = input.nextInt();
System.out.println(answer);
String result;
int score = 0;
if(userAnswer == answer){
score = score + 1;
result = "Correct. Score is currently: " + score + "/" + numberOfQuestions;
} else {
score = score - 1;
result = "Incorrect. Score is currently: " + score + "/" + numberOfQuestions;
}
return result;
}
public String toString(){
return "Math game~!" + answer(min, max, numberOfQuestions);
}
}
'min'과'max'는 아직 값이 주어지지 않을 때 값으로 공급됩니다. –
무작위가 실제로 무작위 적으로 무작위 적으로 존재한다는 점에 유의하기 위해, 임의의 숫자를 만들기 위해 무언가를 취합니다. 예를 들어, 인터럽트 또는 패턴이 발견되면 추측 할 수 있습니다. – jgr208
코드는 위에서 아래로 실행됩니다. 일단 당신이 이것을 알게되면, 그 대답은 명백해야합니다. 해결 방법은 초기화를 생성자로 이동하는 것입니다. –