请教!
我以前写过一个不是Applet的四则运算 的。给你参考下,希望 对你有帮助,你只要把他改成Applet的就可以了
//主文件,保存为ceshi.java
//产生随机的四则运算题目,输入答案,判断正误
import java.util.*;
import java.io.*;
class suanshi{
//产生随机数//
Random rand=new Random();
//int a=(int)Math.random()*50;
//int b=(int)Math.random()*20;//这样的数每次运行时都是一样的.
//int c=(int)Math.random()*10+1;
//rand.setSeed(long d);
int a=(int)rand.nextInt()/1000000;
int b=(int)rand.nextInt()/1000000;
int c=(int)(rand.nextFloat()*10+1);
public static double sum=0;//一定要声明为static的,否则后面的SUM就会为0
public suanshi(){}
char d;
public suanshi(int k){ //产生运算符号//
if(1<=c&&c<=3){
d='+';
sum=a+b;
}
else
if(4<=c&&c<=6){
d='-';
sum=a-b;
}
else
if(7<=c&&c<=8){
d='×';
sum=a*b;
}
else {
d='÷';
sum=a/b;
}
System.out.print("("+a+")"+" "+d+" "+"("+b+")"+"=");
}
}
class panduan extends suanshi {
public static double count=0;
int result;
public static int fens=0;
panduan(){super(0);}
public int flag=0;
panduan(int i){
if(i==sum) {
//if(i==(a+b)) {
System.out.println("你答对了");
flag=1;
fens=fens+1;
count=count+1;
}
else {
System.out.println("你答错了"+"\n"+"不要放弃"+"\t");
System.out.println("正确答案应该是:"+sum);
flag=0;
// fens=fens-1;
count=count+1;
}
}
int f0(){
flag=0;
return flag;
}
int f1(){
flag=1;
return flag;
}
}
//主类开始//
public class ceshi extends panduan{
public int i=0;
String s="";
ceshi(){
try{
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
s=in.readLine();
i=Integer.parseInt(s);
}catch(NumberFormatException e){System.out.println("您输入的数字有误");}
catch(IOException w){}
panduan p=new panduan(i);
}
//int fc(){return i;}
public static void main(String [] args) {
int j=0;
int tt=0;
String ss="";
System.out.println("这是四则运算题!");
System.out.println("***************");
System.out.println("第 "+(int)(count+1)+" 题");
do{
ceshi t=new ceshi();
//int pp=t.fc();
// t.panduan(pp);
try{ System.out.println("输入非1的数继续.输入1推出");
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
ss=in.readLine();
j=Integer.parseInt(ss);
}catch(NumberFormatException e){System.out.println("您输入的数字有误");}
catch(IOException w){}
switch(j){
case 0:
tt=t.f0();
System.out.println("==========================");
System.out.println("第 "+(int)(count+1)+" 题");break;
case 1:
tt=t.f1();
System.out.println("=%=%=%=%=%=%=%=%=%=%=%=%");
System.out.println("您共做的"+count+"题,"+"其中答对"+fens+"题");
System.out.println("你的总分为:"+(fens/count)*100);
System.out.println("下次再努力哦!"+"\n"+"谢谢使用,再见^_^!");
System.out.println("*********************");break;
}
}while(tt==0);
}
}