源码
package arithmetic; import java.util.Random; import javax.swing.*; public class Arithmetic { static void add(int limit1,int limit2,int boolminus/*int boolremainder,int boolmultiply*/) { int number1=new Random().nextInt(limit2-limit1+1)+limit1; int number2=new Random().nextInt(limit2-limit1+1)+limit1; if(boolminus==0) { if(limit1<0||limit2<0) { System.out.println("您输入的范围有误"); } else { System.out.println(number1+"+"+number2+"="); String answer=JOptionPane.showInputDialog("please input answer"); int answer1=Integer.parseInt(answer); if(answer1==number1+number2) System.out.println("right!"); else System.out.println("wrong!"); } } else { System.out.println(number1+"+"+number2+"="); String answer=JOptionPane.showInputDialog("please input answer"); int answer1=Integer.parseInt(answer); if(answer1==number1+number2) System.out.println("right!"); else System.out.println("wrong!"); } } static void subtract(int limit1,int limit2,int boolminus) { int number1=new Random().nextInt(limit2-limit1+1)+limit1; int number2=new Random().nextInt(limit2-limit1+1)+limit1; if(boolminus==0) { while(true) { if(number1<number2) { number1=new Random().nextInt(limit2-limit1+1)+limit1; number2=new Random().nextInt(limit2-limit1+1)+limit1; } else { break; } } if(limit1<0||limit2<0) { System.out.println("您输入的范围有误"); } else if(limit1>=0||limit2>=0||number1>=number2) { System.out.println(number1+"-"+number2+"="); String answer=JOptionPane.showInputDialog("please input answer"); int answer1=Integer.parseInt(answer); if(answer1==number1-number2) System.out.println("right!"); else System.out.println("wrong!"); } } else { System.out.println(number1+"-"+number2+"="); String answer=JOptionPane.showInputDialog("please input answer"); int answer1=Integer.parseInt(answer); if(answer1==number1-number2) System.out.println("right!"); else System.out.println("wrong!"); } } static void multiply(int limit1,int limit2/*int boolremainder,*/) { int number1=new Random().nextInt(limit2-limit1+1)+limit1; int number2=new Random().nextInt(limit2-limit1+1)+limit1; System.out.println(number1+"*"+number2+"="); String answer=JOptionPane.showInputDialog("please input answer"); int answer1=Integer.parseInt(answer); if(answer1==number1*number2) System.out.println("right!"); else System.out.println("wrong!"); } static void divide(int limit1,int limit2,int boolremainder) { int number1=new Random().nextInt(limit2-limit1+1)+limit1; int number2=new Random().nextInt(limit2-limit1+1)+limit1; if(boolremainder==0) { while(true) { if(number1%number2!=0) { number1=new Random().nextInt(limit2-limit1+1)+limit1; number2=new Random().nextInt(limit2-limit1+1)+limit1; } else { System.out.println(number1+"/"+number2+"="); String answer=JOptionPane.showInputDialog("please input answer"); int answer1=Integer.parseInt(answer); if(answer1==number1+number2) System.out.println("right!"); else System.out.println("wrong!"); } } } else { System.out.println(number1+"/"+number2+"="); String answer1=JOptionPane.showInputDialog("请输入商"); int answer11=Integer.parseInt(answer1); String answer2=JOptionPane.showInputDialog("请输入余数"); int answer22=Integer.parseInt(answer2); if(answer11==number1/number2&&answer22==number1%number2) System.out.println("right!"); else System.out.println("wrong!"); } } static void duoshu(int limit1,int limit2,int boolmultiply) { if(boolmultiply==0)//无乘除仅需三位数运算,超过三位显得无意义 { int number1=new Random().nextInt(limit2-limit1+1)+limit1; int number2=new Random().nextInt(limit2-limit1+1)+limit1; int number3=new Random().nextInt(limit2-limit1+1)+limit1; int choose1=new Random().nextInt(2); int choose2=new Random().nextInt(2); if(choose1==0&&choose2==0) { System.out.println(number1+"+"+number2+"+"+number3+"="); String answer=JOptionPane.showInputDialog("please input answer"); int answer1=Integer.parseInt(answer); if(answer1==number1+number2+number3) System.out.println("right!"); else System.out.println("wrong!"); } else if(choose1==1&&choose2==0) { System.out.println(number1+"-"+number2+"+"+number3+"="); String answer=JOptionPane.showInputDialog("please input answer"); int answer1=Integer.parseInt(answer); if(answer1==number1-number2+number3) System.out.println("right!"); else System.out.println("wrong!"); } else if(choose1==0&&choose2==1) { System.out.println(number1+"+"+number2+"-"+number3+"="); String answer=JOptionPane.showInputDialog("please input answer"); int answer1=Integer.parseInt(answer); if(answer1==number1+number2-number3) System.out.println("right!"); else System.out.println("wrong!"); } else if(choose1==1&&choose2==1) { System.out.println(number1+"-"+number2+"-"+number3+"="); String answer=JOptionPane.showInputDialog("please input answer"); int answer1=Integer.parseInt(answer); if(answer1==number1-number2-number3) System.out.println("right!"); else System.out.println("wrong!"); } } else if(boolmultiply==1) { int numbers=new Random().nextInt(7)+3; int i=0,j; int number[]=new int[10]; for(i=0;i<numbers;i++) { j=new Random().nextInt(limit2-limit1+1)+limit1; number[i]=j; } System.out.print(number[0]); for(i=0;i<numbers-1;i++)//无法验证结果,以及避免分母为0 { int choose=new Random().nextInt(4); switch(choose) { case 0: { System.out.print("+"+number[i+1]); break; } case 1: { System.out.print("-"+number[i+1]); break; } case 2: { System.out.print("*"+number[i+1]); break; } case 3: { System.out.print("/"+number[i+1]); break; } } } System.out.println("="); } } static void kuohao(int limit1,int limit2)//仅实现三位 { int number1=new Random().nextInt(limit2-limit1+1)+limit1; int number2=new Random().nextInt(limit2-limit1+1)+limit1; int number3=new Random().nextInt(limit2-limit1+1)+limit1; int choose1=new Random().nextInt(2); if(choose1==0) { int choose2=new Random().nextInt(2); int choose3=new Random().nextInt(2); if(choose2==0&&choose3==0) { System.out.println("("+number1+"+"+number2+")*"+number3); String answer=JOptionPane.showInputDialog("请输入答案"); int answer1=Integer.parseInt(answer); if(answer1==(number1+number2)*number3) System.out.println("right!"); else System.out.println("wrong!"); } if(choose2==1&&choose3==0) { System.out.println("("+number1+"-"+number2+")*"+number3); String answer=JOptionPane.showInputDialog("请输入答案"); int answer1=Integer.parseInt(answer); if(answer1==(number1-number2)*number3) System.out.println("right!"); else System.out.println("wrong!"); } if(choose2==0&&choose3==1) { System.out.println("("+number1+"+"+number2+")/"+number3); number1=number1+number2; int i; if(number1<number3) i=number1; else i=number3; for(int ii=i+1;ii>=1;ii--) { if(number1%ii==0&&number3%ii==0) { number1=number1/ii; number3=number3/ii; break; } } String answer=JOptionPane.showInputDialog("请输入分子"); int answer1=Integer.parseInt(answer); String answer0=JOptionPane.showInputDialog("请输入分母"); int answer10=Integer.parseInt(answer0); if(answer1==number1&&answer10==number3) System.out.println("right!"); else System.out.println("wrong!"); } if(choose2==1&&choose3==1) { System.out.println("("+number1+"-"+number2+")/"+number3); number1=number1-number2; int i; if(number1<number3) i=number1; else i=number3; for(int ii=i+1;ii>=1;ii--) { if(number1%ii==0&&number3%ii==0) { number1=number1/ii; number3=number3/ii; break; } } String answer=JOptionPane.showInputDialog("请输入分子"); int answer1=Integer.parseInt(answer); String answer0=JOptionPane.showInputDialog("请输入分母"); int answer10=Integer.parseInt(answer0); if(answer1==number1&&answer10==number3) System.out.println("right!"); else System.out.println("wrong!"); } } if(choose1==1) { int choose2=new Random().nextInt(2); int choose3=new Random().nextInt(2); if(choose2==0&&choose3==0) { System.out.println(number3+"*("+number1+"+"+number2+")"); String answer=JOptionPane.showInputDialog("请输入答案"); int answer1=Integer.parseInt(answer); if(answer1==(number1+number2)*number3) System.out.println("right!"); else System.out.println("wrong!"); } if(choose2==1&&choose3==0) { System.out.println(number3+"*("+number1+"-"+number2+")"); String answer=JOptionPane.showInputDialog("请输入答案"); int answer1=Integer.parseInt(answer); if(answer1==(number1-number2)*number3) System.out.println("right!"); else System.out.println("wrong!"); } if(choose2==0&&choose3==1) { while(true) { if(number1+number2==0) { number1=new Random().nextInt(limit2-limit1+1)+limit1; number2=new Random().nextInt(limit2-limit1+1)+limit1; } else break; } System.out.println(number3+"/("+number1+"+"+number2+")"); number1=number1+number2; int i; if(number1<number3) i=number1; else i=number3; for(int ii=i+1;ii>=1;ii--) { if(number1%ii==0&&number3%ii==0) { number1=number1/ii; number3=number3/ii; break; } } String answer=JOptionPane.showInputDialog("请输入分子"); int answer1=Integer.parseInt(answer); String answer0=JOptionPane.showInputDialog("请输入分母"); int answer10=Integer.parseInt(answer0); if(answer1==number3&&answer10==number1) System.out.println("right!"); else System.out.println("wrong!"); } if(choose2==1&&choose3==1) { while(true) { if(number1-number2==0) { number1=new Random().nextInt(limit2-limit1+1)+limit1; number2=new Random().nextInt(limit2-limit1+1)+limit1; } else break; } System.out.println(number3+"/("+number1+"-"+number2+")"); number1=number1-number2; int i; if(number1<number3) i=number1; else i=number3; for(int ii=i+1;ii>=1;ii--) { if(number1%ii==0&&number3%ii==0) { number1=number1/ii; number3=number3/ii; break; } } String answer=JOptionPane.showInputDialog("请输入分子"); int answer1=Integer.parseInt(answer); String answer0=JOptionPane.showInputDialog("请输入分母"); int answer10=Integer.parseInt(answer0); if(answer1==number3&&answer10==number1) System.out.println("right!"); else System.out.println("wrong!"); } } } static void fraction(int limit1,int limit2,int numbers,int boolmultiply) { int i,j,k; if(boolmultiply==0) { for(int o=numbers;o>0;o--) { int number1=new Random().nextInt(limit2-limit1+1)+limit1; int number2=0; while(true) { number2=new Random().nextInt(limit2-limit1+1)+limit1; if(number2!=0) break; } if(number1<number2) i=number1; else i=number2; for(int ii=i+1;ii>=1;ii--) { if(number1%ii==0&&number2%ii==0) { number1=number1/ii; number2=number2/ii; break; } } int number3=new Random().nextInt(limit2-limit1+1)+limit1; int number4=0; while(true) { number4=new Random().nextInt(limit2-limit1+1)+limit1; if(number4!=0) break; } if(number3<number4) j=number3; else j=number4; for(int jj=j+1;jj>=1;jj--) { if(number3%jj==0&&number4%jj==0) { number3=number3/jj; number4=number4/jj; break; } } k=new Random().nextInt(2); if(k==0) { System.out.println(number1+"/"+number2+"+"+number3+"/"+number4+"="); int fenzi=number1*number4+number2*number3; int fenmu=number2*number4; int small1; if(fenzi<fenmu) small1=fenzi; else small1=fenmu; for(int small11=small1+1;small11>=1;small11--) { if(fenzi%small11==0&&fenmu%small11==0) { fenzi=fenzi/small11; number2=number2/small11; break; } } String answer=JOptionPane.showInputDialog("请输入分子"); int answer1=Integer.parseInt(answer); String answer0=JOptionPane.showInputDialog("请输入分母"); int answer10=Integer.parseInt(answer0); if(answer1==fenzi&&answer10==fenmu) System.out.println("right!"); else System.out.println("wrong!"); } else if(k==1) { System.out.println(number1+"/"+number2+"-"+number3+"/"+number4+"="); int fenzi=number1*number4-number2*number3; int fenmu=number2*number4; int small1; if(fenzi<fenmu) small1=fenzi; else small1=fenmu; for(int small11=small1+1;small11>=1;small11--) { if(fenzi%small11==0&&fenmu%small11==0) { fenzi=fenzi/small11; number2=number2/small11; break; } } String answer=JOptionPane.showInputDialog("请输入分子"); int answer1=Integer.parseInt(answer); String answer0=JOptionPane.showInputDialog("请输入分母"); int answer10=Integer.parseInt(answer0); if(answer1==fenzi&&answer10==fenmu) System.out.println("right!"); else System.out.println("wrong!"); } } } else { for(int o=numbers;o>0;o--) { int number1=new Random().nextInt(limit2-limit1+1)+limit1; int number2=0; while(true) { number2=new Random().nextInt(limit2-limit1+1)+limit1; if(number2!=0) break; } if(number1<number2) i=number1; else i=number2; int number3=new Random().nextInt(limit2-limit1+1)+limit1; int number4=0; while(true) { number4=new Random().nextInt(limit2-limit1+1)+limit1; if(number4!=0) break; } if(number3<number4) j=number3; else j=number4; for(int ii=i+1;ii>=1;ii--) { if(number1%ii==0&&number2%ii==0) { number1=number1/ii; number2=number2/ii; break; } } for(int jj=j+1;jj>=1;jj--) { if(number3%jj==0&&number4%jj==0) { number3=number3/jj; number4=number4/jj; break; } } k=new Random().nextInt(4); if(k==0) { System.out.println(number1+"/"+number2+"+"+number3+"/"+number4+"="); int fenzi=number1*number4+number2*number3; int fenmu=number2*number4; int small1; if(fenzi<fenmu) small1=fenzi; else small1=fenmu; for(int small11=small1+1;small11>=1;small11--) { if(fenzi%small11==0&&fenmu%small11==0) { fenzi=fenzi/small11; number2=number2/small11; break; } } String answer=JOptionPane.showInputDialog("请输入分子"); int answer1=Integer.parseInt(answer); String answer0=JOptionPane.showInputDialog("请输入分母"); int answer10=Integer.parseInt(answer0); if(answer1==fenzi&&answer10==fenmu) System.out.println("right!"); else System.out.println("wrong!"); } else if(k==1) { System.out.println(number1+"/"+number2+"-"+number3+"/"+number4+"="); int fenzi=number1*number4-number2*number3; int fenmu=number2*number4; int small1; if(fenzi<fenmu) small1=fenzi; else small1=fenmu; for(int small11=small1+1;small11>=1;small11--) { if(fenzi%small11==0&&fenmu%small11==0) { fenzi=fenzi/small11; number2=number2/small11; break; } } String answer=JOptionPane.showInputDialog("请输入分子"); int answer1=Integer.parseInt(answer); String answer0=JOptionPane.showInputDialog("请输入分母"); int answer10=Integer.parseInt(answer0); if(answer1==fenzi&&answer10==fenmu) System.out.println("right!"); else System.out.println("wrong!"); } else if(k==2) { System.out.println(number1+"/"+number2+"*"+number3+"/"+number4+"="); int fenzi=number1*number3; int fenmu=number2*number4; int small1; if(fenzi<fenmu) small1=fenzi; else small1=fenmu; for(int small11=small1+1;small11>=1;small11--) { if(fenzi%small11==0&&fenmu%small11==0) { fenzi=fenzi/small11; number2=number2/small11; break; } } String answer=JOptionPane.showInputDialog("请输入分子"); int answer1=Integer.parseInt(answer); String answer0=JOptionPane.showInputDialog("请输入分母"); int answer10=Integer.parseInt(answer0); if(answer1==fenzi&&answer10==fenmu) System.out.println("right!"); else System.out.println("wrong!"); } else { System.out.println(number1+"/"+number2+"/"+number3+"/"+number4+"="); int fenzi=number1*number4; int fenmu=number2*number3; int small1; if(fenzi<fenmu) small1=fenzi; else small1=fenmu; for(int small11=small1+1;small11>=1;small11--) { if(fenzi%small11==0&&fenmu%small11==0) { fenzi=fenzi/small11; number2=number2/small11; break; } } String answer=JOptionPane.showInputDialog("请输入分子"); int answer1=Integer.parseInt(answer); String answer0=JOptionPane.showInputDialog("请输入分母"); int answer10=Integer.parseInt(answer0); if(answer1==fenzi&&answer10==fenmu) System.out.println("right!"); else System.out.println("wrong!"); } } } } public static void main(String[] args) { String choice0=JOptionPane.showInputDialog("是否有分数运算(有输入1,无输入0)"); int choice00=Integer.parseInt(choice0); String choice1=JOptionPane.showInputDialog("是否有乘除法(有输入1,无输入0)"); int choice11=Integer.parseInt(choice1); String choice2=JOptionPane.showInputDialog("请输入数值范围下限"); String choice21=JOptionPane.showInputDialog("请输入数值范围上限"); String choice5=JOptionPane.showInputDialog("需要多少道题"); int limit1=Integer.parseInt(choice2); int limit2=Integer.parseInt(choice21); int i=Integer.parseInt(choice5); if(choice00==0) { String choicek=JOptionPane.showInputDialog("是否有括号运算(有输入1,无输入0)"); int choicekk=Integer.parseInt(choicek); if(choicekk==0) { String choiced=JOptionPane.showInputDialog("是否有多数运算(有输入1,无输入0)"); int choicedd=Integer.parseInt(choiced); if(choicedd==0) { String choice3=JOptionPane.showInputDialog("加法有无负数(有输入1,无输入0)"); int choice33=Integer.parseInt(choice3); if(choice11==0) { for(;i>0;i--) { int j=new Random().nextInt(2); if(j==0) add(limit1,limit2,choice33); else subtract(limit1,limit2,choice33); } } else if(choice11==1) { String choice4=JOptionPane.showInputDialog("除法有无余数(有输入1,无输入0)"); int choice44=Integer.parseInt(choice4); for(;i>0;i--) { int j=new Random().nextInt(4); if(j==0) add(limit1,limit2,choice33); else if(j==1) subtract(limit1,limit2,choice33); else if(j==2) multiply(limit1,limit2); else if(j==3) divide(limit1,limit2,choice44); } } } else { for(int i1=0;i1<i;i1++) { duoshu(limit1,limit2,choice11); } } } else { for(int i1=0;i1<i;i1++) { kuohao(limit1,limit2); } } } else { fraction(limit1,limit2,i,choice11); } } }
加入了括号,加入了多数计算,加入了验证结果。未能实现网页版。