标题: 怎样才能让显示出来的题目不重复? [打印本页] 作者: 常万 时间: 2012-4-2 10:40 标题: 怎样才能让显示出来的题目不重复? public class Addition
{
static void additionProblem(int topNumber, int bottomNumber) throws Exception
{
BufferedReader cin;
cin = new BufferedReader(new InputStreamReader(System.in));
int userAnswer;
System.out.print("\n\n\n " + topNumber + " + " + bottomNumber + " = ");
userAnswer = new Double(cin.readLine()).intValue();
int theAnswer = topNumber + bottomNumber;
if (theAnswer == userAnswer)
System.out.println(" Correct!");
else
System.out.println(" Very good, but a better answer is " + theAnswer);
} // additionProblem
public static void main(String[] argv) throws Exception
{
int i;
i = 0;
while ( i < 5)
{
additionProblem((int)(5 * Math.random()), (int)(5 * Math.random()));
i = i + 1;
} // while
} // main
} // public class作者: 盛光富 时间: 2012-4-2 11:08
建立两个数组,一个用于保存topNumber ,另外一个用于保存bottomNumber ,当生成随机数的时候在第一个数组中找该随机数是否已经生成过,然后记住他的下标在第二个数组中找相同下标的值是否和第二个随机数相同,如果都相同那就重复不输出,这样就可以避免题目重复。作者: 盛光富 时间: 2012-4-2 12:28
import java.io.*;
public class Addition
{
static void additionProblem(int topNumber, int bottomNumber) throws Exception
{
BufferedReader cin;
cin = new BufferedReader(new InputStreamReader(System.in));
int userAnswer;
System.out.print("\n" + topNumber + " + " + bottomNumber + " = ");
userAnswer = new Double(cin.readLine()).intValue();
int theAnswer = topNumber + bottomNumber;
if (theAnswer == userAnswer)
System.out.println(" Correct!");
else
System.out.println(" Very good, but a better answer is " + theAnswer);
} // additionProblem
public static void main(String[] argv) throws Exception
{
int i;
i = 0;
int[] topNumber = new int[5];
int[] bottomNumber = new int[5];
while ( i < 5)
{
int topN ,bottomN;
topN=(int)(5 * Math.random());
bottomN=(int)(5 * Math.random());