【问题标题】:My random numbers are always zero?我的随机数总是零?
【发布时间】:2014-11-25 17:02:16
【问题描述】:

我的程序运行良好,只是我为数学游戏中的问题生成的随机数始终为零。我不明白。我试过使用 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);
    }
}

【问题讨论】:

  • min 和 max 在尚未被赋予值时被作为值提供。
  • 还要注意,从某种意义上说,随机并不是真正的随机,它需要一些东西来生成随机数,以便作为中断或者可能是什么,所以如果找到模式就可以猜测
  • 代码从上到下运行。一旦你知道了这一点,答案应该是显而易见的。解决方法是将初始化移到构造函数中。

标签: java class variables random


【解决方案1】:

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

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

在之前执行

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

在构造函数中。

此时,max 和 min 的值因此是默认的 0。在max和min的赋值之后,将num1的初始化语句移到构造函数体中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-26
    • 1970-01-01
    • 2015-09-10
    • 2012-12-27
    • 2021-04-29
    相关资源
    最近更新 更多