java - 我的随机数总是为零?

标签 java class variables random

我的程序运行完美,除了我为数学游戏中的问题生成的随机数始终为零这一事实。我不明白。我尝试过使用 math.random,但遇到了同样的问题。它在做什么?

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);
    }
}

最佳答案

实例字段初始化表达式在构造函数主体之前执行。所以

private int num1 = rand.nextInt((max - min) + 1) + min;

在之前执行

this.max = max;
this.min = min;

在构造函数中。

此时,maxmin 的值因此为默认的0。将 num1 的初始化语句移至构造函数主体,位于 maxmin 的赋值之后。

关于java - 我的随机数总是为零?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27132539/

相关文章:

java - Swing:JPanel中的JLabels一对一宽度=内容宽度

java - 将创建多少个 String 对象

java - 一旦部署在 Android 设备上,你能否即时运行 "compact"SQLite 数据库?

java - 如何从Java执行Pig脚本

java - 为什么生成 <filename>$1.class?

android - Java (Android) 中的静态类 - 使用或不使用

c++ - 在类中使用私有(private)变量

javascript - 如何使用 JavaScript 设置函数参数?

java - 未找到变量,即使它位于上面的类中

javascript - 在 for 循环中带有 for 循环的函数两个变量都不能完全工作