我是一名文凭学生,目前是编程和 Java 语言的新手,我正在制作一个应用程序来教授和培训孩子如何乘以总和。该程序生成两个随机数并将它们打印在屏幕上供孩子们回答。我写的这个程序的问题是,在打印随机问题时,我的随机数似乎不起作用。程序编译成功,一切似乎都很好。但是一旦我运行它,这两个随机数总是“0”。你们介意就为什么我的随机数总是生成为“0”给出一些指示吗?欢迎提出其他批评和建议,以供将来参考:)
这是我的主要驱动程序类
/*
* 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");
}
}
}
这是我的方法类
/*
* 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;
}
}
提前致谢!:)