Friday 15 March 2013

java - Passing random numbers into an instance of an object -



java - Passing random numbers into an instance of an object -

my homework asking me math game in prompt user reply math questions , show them answer. i've created mathgameclass, when create instance of mathgame, asks me set in 2 ints, don't know because i'm trying generate them randomly.

question: how random numbers class pass instance of mathgame?

below mathgame class

underneath main instance of mathgame i'm trying figure out.

import java.util.random; public class mathgame { private int operand1; private int operand2; private int solution; public mathgame (int operand1, int operand2) { this.operand1 = operand1; this.operand2 = operand2; } public int genrandom1() { random rand = new random(); int randnum = rand.nextint(0) + 20; randnum = operand1; homecoming operand1; } public int genrandom2() { random rand = new random(); int randnum2 = rand.nextint(0) + 20; randnum2 = operand2; homecoming operand2; } public int getoperand1() { homecoming operand1; } public int getoperand2() { homecoming operand2; } public string question() { homecoming "what is" + operand1 + operand2 + "?"; } public string solution() { int solution = operand1 + operand2; homecoming "the right reply is: " + solution; } }

i error in instance of mathgame since need have 2 ints go through, don't know ints because they're supposed randomly generated, did in class.

import java.util.scanner; public class mathgamemain { public static void main(string[] args) { scanner scan = new scanner(system.in); mathgame game1 = new mathgame(); } }

you want alter this:

import java.util.random; public class mathgame { private int operand1; private int operand2; private int solution; public mathgame () { this.operand1 = getrandom(); this.operand2 = getrandom(); } public int getrandom() { random rand = new random(); int randnum = rand.nextint(20); homecoming randnum; } public int getoperand1() { homecoming operand1; } public int getoperand2() { homecoming operand2; } public string question() { homecoming "what is" + operand1 + " + " + operand2 + "?"; } public string solution() { int solution = operand1 + operand2; homecoming "the right reply is: " + solution; } }

you should generate random numbers when mathgame initialized. no need pass numbers if going randomly generate them.

java variables math random

No comments:

Post a Comment