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());
//判断重复
if(!panduan(topNumber,bottomNumber,topN,bottomN))
{
topNumber[i]=topN;
bottomNumber[i]=bottomN;
additionProblem(topN, bottomN);
i = i + 1;
}
} // while
} // main
static boolean panduan(int[] topNumber,int[] bottomNumber,int topN,int bottomN)
{
boolean result=false;
//判断重复的代码
for (int i = 0; i < topNumber.length; i++)
{
if(topNumber[i]==topN && bottomNumber[i]==bottomN)
{
result=true;//重复则跳出
break;
}
}
return result;
}
}
|