我的作业要求我做一个数学游戏,在其中我提示用户回答数学问题然后向他们展示答案。我创建了MathGame
课程,但是当我创建MathGame
的实例时,它会要求我输入两个我不知道的内容,因为我已经知道了试图随机生成它们。
问题:如何从课程中获取随机数以传递到MathGame
的实例?
以下是MathGame
类
下面是MathGame
实例的主要内容我试图找出答案。
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;
return operand1;
}
public int genRandom2() {
Random rand = new Random();
int randNum2 = rand.nextInt(0) + 20;
randNum2 = operand2;
return operand2;
}
public int getoperand1() {
return operand1;
}
public int getoperand2() {
return operand2;
}
public String question() {
return "What is" + operand1 + operand2 + "?";
}
public String solution() {
int solution = operand1 + operand2;
return "The correct answer is: " + solution;
}
}
我在MathGame的实例中出现错误,因为我需要进行两次整理,但我显然不知道这些整数,因为它们应该是随机生成的,我在类。
import java.util.Scanner;
public class MathGameMain {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
MathGame game1 = new MathGame();
}
}
答案 0 :(得分:2)
您可能希望更改为以下内容:
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);
return randNum;
}
public int getoperand1()
{
return operand1;
}
public int getoperand2()
{
return operand2;
}
public String question()
{
return "What is" + operand1 + " + " + operand2 + "?";
}
public String solution()
{
int solution = operand1 + operand2;
return "The correct answer is: " + solution;
}
}
初始化MathGame时应生成随机数。如果要随机生成数字,则无需传递数字。
答案 1 :(得分:0)
这个方法你想要什么
public int genRandom1()
{
Random rand = new Random();
int randNum = rand.nextInt(0) + 20;
randNum = operand1;
return operand1;
}
和
public int genRandom2()
{
Random rand = new Random();
int randNum2 = rand.nextInt(0) + 20;
randNum2 = operand2;
return operand2;
}
您创建了一个流程并将其存储到 randNum ,但您在方法中返回的是 operand2 。只需返回 randNum 。
答案 2 :(得分:0)
这是我从你的评论中得到的,
在Main类上调用MathGame对象,MathGame的构造函数返回存储在operand1和operand2上的2个随机数
程序将通过向operand2
<强>解决方案强>
import java.util.Random;
public class MathGame {
private int operand1;
private int operand2;
private int solution;
public MathGame ()
{
this.operand1 = genRandom();
this.operand2 = genRandom();
}
public int genRandom()
{
Random rand = new Random();
int randNum = rand.nextInt(0) + 20;
return randNum;
}
public String question()
{
return "What is" + this.operand1 + " + " + this.operand2 + "?";
}
public String solution()
{
int solution = operand1 + operand2;
return "The correct answer is: " + solution;
}
}
在主要班级
import java.util.Scanner;
public class MathGameMain {
public static void main(String[] args) {
MathGame game1 = new MathGame();
//question
System.out.println(game1.question());
// solution
System.out.println(game1.solution());
}
}