JLWK
JLWK

Reputation: 65

Java random number issue.

I'm a diploma student and is currently new to programming and Java language, I'm making an application that teaches and trains children on how to multiply sums. The program spawns two random numbers and prints them on the screen for children to answer. The problem with this program I wrote is that, while printing the randomized questions, my random number doesn't seem to work. The program was compiled successfully and everything seems to be fine. But once I run it, the two randomized numbers are always "0". Do you guys mind giving out some pointers on why my random numbers are always spawned as "0"? Additional criticism and advice is welcome for improvement for future references :)

This is my main driver class

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package multiplicationteacher;

import java.util.Scanner;

/**
 *
 * @author Jeremy Lai
 */
public class MultiplicationTeacher {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        Scanner input = new Scanner(System.in);
        QuestionSystem qs = new QuestionSystem();

        int userInput; 

        System.out.println("How much is " + qs.num1 + "times" + qs.num2 + "?"); 
        System.out.print("Enter your answer (-1 to quit) : ");
        userInput = input.nextInt();

            if (userInput == qs.answer(userInput)) { 
            System.out.print("Very Good!");
                    }
        else {
            System.out.print("No. Please try again");
        }

    }
}

And this is my method class

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package multiplicationteacher;

import java.util.Random;

/**
 *
 * @author Jeremy Lai
 */
public class QuestionSystem {
    Random rand = new Random();

    public int num1;
    public int num2;
    public int answer;

    public int randNum1(int num1) {
        num1 = rand.nextInt();
        return num1;
    }

    public int randNum2 (int num2) {
        num2 = rand.nextInt();
        return num2;
    }

    public int answer (int answer) {
        answer = num1 * num2;
        return answer;
    }
}

Thanks in advance! :)

Upvotes: 0

Views: 179

Answers (3)

libik
libik

Reputation: 23029

In your whole code, you never call the randNum1 and randNum2 methods! So they are not generated!

You can use constructor, so the values are called when instance of QuestionSystem is created.

public QuestionSystem(){
    this.randNum1();
    this.randNum2();
}

Also, you dont have to have any return value or parametrs in your randNum methods :

public void randNum1() {
    num1 = rand.nextInt();
}

Or if you generate these numbers only once, you can have everything in your QuestionSystem constructor :

public QuestionSystem(){
    num1 = rand.nextInt();
    num2 = rand.nextInt();
}

Also if this is for children, use range to nextInt to return values to reasenable size :

num1 = rand.nextInt(100); //returns values from 0 to 99

Upvotes: 1

Germann Arlington
Germann Arlington

Reputation: 3353

You have to initialise your random numbers after you create an instance of your QuestionSystem class

Upvotes: 0

lejlot
lejlot

Reputation: 66805

You do not seem to call neither randNum1 nor randNum2 so you cannot get any random numbers - you just get the initialization values (which are 0 in Java for ints).

You could add the constructor

public QuestionSystem(){
 this.randNum1();
 this.randNum2();
}

In your QuestionSystem class

furthermore, your answer method does not need any arguments, so

public int answer (int answer) {
    answer = num1 * num2;
    return answer;
}

should be

public int answer () {
    answer = num1 * num2;
    return answer;
}

and as a consequence

userInput == qs.answer(userInput)

shoule be changed to

userInput == qs.answer()

Upvotes: 1

Related Questions