我正在尝试创建一个创建随机数学问题的Java函数。我想让这个函数创建问题然后onclick显示答案。
是否有必要使用Random int
?
我在想这样的事情会使i
成为一个随机数,然后在数学问题中使用它......任何帮助都会受到赞赏。
private int rando(int i) {
Random randomGenerator = new Random();
int randomInt = randomGenerator.nextInt();
i = randomInt;
return rando();
}
public static int add(int i, int i)
{
int a = i+i;
return a;
}
b1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
String b = "Click For Answer";
b1.setText(b.toString());
jl.setText(prob.toString());
}
});
答案 0 :(得分:2)
怎么样
//or whatever numbers you want here
int myLow = -10;
int myHigh = 10;
public int randBetween(int low, int high){
return (int) Math.round(Math.random() * (high - low)) + low;
}
public String makeProblem(AtomicLong answer){
int a = randBetween(myLow, myHigh);
int b = randBetween(myLow, myHigh);
double result;
char operator;
switch(randBetween(1,4)){
case 1:
operator = '+';
result = a + b;
break;
case 2:
operator = '-';
result = a - b;
break;
case 3:
operator = '*';
result = a * b;
break;
case 4:
operator = '/';
result = (double) a / b;
break;
default:
operator = '\0';
result = Double.NaN;
}
answer.set(Double.doubleToLongBits(result));
return String.format("%d %c %d = ?",a,operator,b);
}
public void yourFunction(){
//whatever you need to do before
AtomicLong outVariable = new AtomicLong();
String question = makeProblem(outVariable);
double answer = Double.longBitsToDouble(outVariable.get());
//whatever you need to do after
}
答案 1 :(得分:1)
类似的东西(没有测试过它或其他东西......):
public class MathProblems {
private static final int MAX_NUMBER = 10;
private static final Scanner in = new Scanner(System.in);
private static final Random random = new Random();
public static void main (String... args) {
new MathProblems().run();
}
public void run() {
while(true) {
final int a = random.nextInt(MAX_NUMBER);
final int b = random.nextInt(MAX_NUMBER);
final int type = random.nextInt(4);
switch (type) {
case 0:
add(a, b);
break;
case 1:
subtract(a, b);
break;
case 2:
multiply(a, b);
break;
}
}
}
private void add(final int a, final int b) {
final int expected = a + b;
final int answer = askQuestion(a + " + " + b + "=");
checkResult(expected, answer);
}
private void subtract(final int a, final int b) {
final int expected = a - b;
final int answer = askQuestion(a + " - " + b + "=");
checkResult(expected, answer);
}
private void multiply(final int a, final int b) {
//leaving this for you..
}
private int askQuestion(final String question) {
System.out.print(question);
return in.nextInt();
}
private void checkResult(final int expected, final int answer) {
if (expected == answer) {
System.out.println("Correct answer! You rock!");
} else {
System.out.println("WROOONG! You suck!");
}
}
}
答案 2 :(得分:0)
这个怎么样......
public class MyClass{
Random random;
public MyClass(){
random = new Random();
}
private String randomNumber(){
return Integer.toString(random.nextInt());
}
private String randomOperator(){
String[] operators = {"+", "/", "-", "*"};
int i = random.nextInt(4);
return operators[i];
}
public void execute(){
String sum = randomNumber() + randomOperator() + randomNumber();
}
}