四则运算02
Posted YOUNG111
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了四则运算02相关的知识,希望对你有一定的参考价值。
设计思想:
把整数和真分数进行分类,在每个分类下进行输入题目量、数值范围、是否有乘除法、加减法是否有负数、除法是否有余数的操作。
在整数下定义两个随机数,在真分数下定义四个随机数。
如果有乘除法,加减法有负数,除法有余数就直接输出题目。
如果无乘除法,那么输出的结果就只有加减法的题目。
源程序代码:
package test; import java.util.Random; import java.util.Scanner; public class yunsuan { public static void main(String[] args) { // TODO Auto-generated method stub int a,b,c,m,n,x,min,max; Scanner sc = new Scanner(System.in); System.out.println("请选择整数还是真分数(整数输入1.真分数输入2):"); x = sc.nextInt(); if(x==1){ System.out.println("请输入要打印题目的数量:"); m = sc.nextInt(); System.out.println("请输入数值的范围(最小值和最大值):"); min = sc.nextInt(); max = sc.nextInt(); System.out.println("请输入是否有乘除法:(有输入1.反之输入2)"); a = sc.nextInt(); System.out.println("请输入加减法是否有负数:(有输入1.反之输入2)"); b = sc.nextInt(); System.out.println("请输入除法是否有余数:(有输入1.反之输入2)"); c = sc.nextInt(); sc.close(); if(a==1)//有乘除法 { if(b==1)//有乘除法。加减法有负数 { if(c==1)//有乘除法。加减法有负数。除法有余数 { for(int i=0;i<m;i++) { Random rand = new Random(); int firstnum = rand.nextInt(max)+min; int secondnum = rand.nextInt(max)+min; int num = rand.nextInt(4)+1; if(num==1) System.out.print(firstnum+" + "+secondnum+" = "); if(num==2) System.out.print(firstnum+" - "+secondnum+" = "); if(num==3) System.out.print(firstnum+" * "+secondnum+" = "); if(num==4) System.out.print(firstnum+" / "+secondnum+" = "); if(i%5==0){ System.out.println(); } } } if(c==2)//有乘除法。加减法有负数。除法无余数 { for(int i=0;i<m;i++) { Random rand = new Random(); int firstnum = rand.nextInt(max)+min; int secondnum = rand.nextInt(max)+min; int num = rand.nextInt(4)+1; if(num==1) System.out.print(firstnum+" + "+secondnum+" = "); if(num==2) System.out.print(firstnum+" - "+secondnum+" = "); if(num==3) System.out.print(firstnum+" * "+secondnum+" = "); if(num==4){ if(firstnum%secondnum!=0) { i=i-1; } else{System.out.print(firstnum+" / "+secondnum+" = ");} } if(i%5==0){ System.out.println(); } } } } if(b==2)//有乘除法。加减法无负数 { if(c==1)//有乘除法。加减法无负数。除法有余数 { for(int i=0;i<m;i++) { Random rand = new Random(); int firstnum = rand.nextInt(max)+min; int secondnum = rand.nextInt(max)+min; int num = rand.nextInt(4)+1; if(num==1) System.out.print(firstnum+" + "+secondnum+" = "); if(num==2) { int l; l=firstnum-secondnum; if(l<0){ i=i-1; } if(l>=0){ System.out.print(firstnum+" - "+secondnum+" = "); } } if(num==3) System.out.print(firstnum+" * "+secondnum+" = "); if(num==4) System.out.print(firstnum+" / "+secondnum+" = "); if(i%5==0){ System.out.println(); } } } if(c==2)//有乘除法。加减法无负数。除法无余数 { for(int i=0;i<m;i++) { Random rand = new Random(); int firstnum = rand.nextInt(max)+min; int secondnum = rand.nextInt(max)+min; int num = rand.nextInt(4)+1; if(num==1) System.out.print(firstnum+" + "+secondnum+" = "); if(num==2) { int l; l=firstnum-secondnum; if(l<0){ i=i-1; } if(l>=0){ System.out.print(firstnum+" - "+secondnum+" = "); } } if(num==3) System.out.print(firstnum+" * "+secondnum+" = "); if(num==4){ if(firstnum%secondnum!=0) { i=i-1; } else{System.out.print(firstnum+" / "+secondnum+" = ");} } if(i%5==0){ System.out.println(); } } } } } if(a==2)//无乘除法 { if(b==1)//没有乘除法,加减法有负数 { if(c==1||c==2){ for(int i=0;i<m;i++) { Random rand = new Random(); int firstnum = rand.nextInt(max)+min; int secondnum = rand.nextInt(max)+min; int num = rand.nextInt(2)+1; if(num==1) System.out.print(firstnum+" + "+secondnum+" = "); if(num==2) System.out.print(firstnum+" - "+secondnum+" = "); if(i%5==0){ System.out.println(); } } } } if(b==2)//没有乘除法,加减法无负数 { if(c==1||c==2){ for(int i=0;i<m;i++) { Random rand = new Random(); int firstnum = rand.nextInt(max)+min; int secondnum = rand.nextInt(max)+min; int num = rand.nextInt(2)+1; if(num==1){ System.out.print(firstnum+" + "+secondnum+" = ");} if(num==2) { int l; l=firstnum-secondnum; if(l<0){ i=i-1; } if(l>=0){ System.out.print(firstnum+" - "+secondnum+" = "); } } if(i%5==0){ System.out.println(); } } } } } } if(x==2){ System.out.println("请输入要打印题目的数量:"); m = sc.nextInt(); System.out.println("请输入数值的范围(最小值和最大值):"); min = sc.nextInt(); max = sc.nextInt(); System.out.println("请输入是否有乘数法:(有输入1.反之输入2)"); a = sc.nextInt(); System.out.println("请输入加减法是否有负数:(有输入1.反之输入2)"); b = sc.nextInt(); sc.close(); if(a==1)//有乘除法 { if(b==1)//有乘除法,加减法有负数 { for(int i=0;i<m;i++) { Random rand = new Random(); int firstnum = rand.nextInt(max)+min; int secondnum = rand.nextInt(max)+min; int thirdnum = rand.nextInt(max)+min; int fourthnum = rand.nextInt(max)+min; int num = rand.nextInt(4)+1; int flag = 0; if(firstnum>secondnum){ int temp; temp = firstnum; firstnum = secondnum; secondnum = temp; } if(thirdnum>fourthnum){ int temp; temp = thirdnum; thirdnum = fourthnum; fourthnum = temp; }//如果分子大于分母,则分子分母交换位置 if(firstnum==secondnum&&thirdnum==fourthnum){ flag=1; } if(firstnum==secondnum&&thirdnum!=fourthnum){ flag=1; } if(firstnum!=secondnum&&thirdnum==fourthnum){ flag=1; } if(flag==1){ i=i-1; }//如果分子分母相等,则重新生成 else{ if(num==1) System.out.print(firstnum+"/"+secondnum+" + "+thirdnum+"/"+fourthnum+" = "); if(num==2) System.out.print(firstnum+"/"+secondnum+" - "+thirdnum+"/"+fourthnum+" = "); if(num==3) System.out.print(firstnum+"/"+secondnum+" * "+thirdnum+"/"+fourthnum+" = "); if(num==4) System.out.print(firstnum+"/"+secondnum+" / "+thirdnum+"/"+fourthnum+" = "); if(i%5==0){ System.out.println(); } } } } if(b==2)//有乘除法,加减法无负数 { for(int i=0;i<m;i++) { Random rand = new Random(); int firstnum = rand.nextInt(max)+min; int secondnum = rand.nextInt(max)+min; int thirdnum = rand.nextInt(max)+min; int fourthnum = rand.nextInt(max)+min; int num = rand.nextInt(4)+1; int flag=0; if(firstnum>secondnum){ int temp; temp = firstnum; firstnum = secondnum; secondnum = temp; } if(thirdnum>fourthnum){ int temp; temp = thirdnum; thirdnum = fourthnum; fourthnum = temp; } if(firstnum==secondnum&&thirdnum==fourthnum){ flag=1; } if(firstnum==secondnum&&thirdnum!=fourthnum){ flag=1; } if(firstnum!=secondnum&&thirdnum==fourthnum){ flag=1; } if(flag==1){ i=i-1; } else{ if(num==1) System.out.print(firstnum+"/"+secondnum+" + "+thirdnum+"/"+fourthnum+" = "); if(num==2) { int l; l=firstnum/secondnum-thirdnum/fourthnum; if(l<0){ i=i-1; } if(l>=0){ System.out.print(firstnum+"/"+secondnum+" - "+thirdnum+"/"+fourthnum+" = "); } } if(num==3) System.out.print(firstnum+"/"+secondnum+" * "+thirdnum+"/"+fourthnum+" = "); if(num==4) System.out.print(firstnum+"/"+secondnum+" / "+thirdnum+"/"+fourthnum+" = "); if(i%5==0){ System.out.println(); } } } } } if(a==2)//无乘除法 { if(b==1)//没有乘除法,加减法有负数 { for(int i=0;i<m;i++) { Random rand = new Random(); int firstnum = rand.nextInt(max)+min; int secondnum = rand.nextInt(max)+min; int thirdnum = rand.nextInt(max)+min; int fourthnum = rand.nextInt(max)+min; int num = rand.nextInt(2)+1; int flag=0; if(firstnum>secondnum){ int temp; temp = firstnum; firstnum = secondnum; secondnum = temp; } if(thirdnum>fourthnum){ int temp; temp = thirdnum; thirdnum = fourthnum; fourthnum = temp; } if(firstnum==secondnum&&thirdnum==fourthnum){ flag=1; } if(firstnum==secondnum&&thirdnum!=fourthnum){ flag=1; } if(firstnum!=secondnum&&thirdnum==fourthnum){ flag=1; } if(flag==1){ i=i-1; } else{ if(num==1) System.out.print(firstnum+"/"+secondnum+" + "+thirdnum+"/"+fourthnum+" = "); if(num==2) System.out.print(firstnum+"/"+secondnum+" - "+thirdnum+"/"+fourthnum+" = "); if(i%5==0){ System.out.println(); } } } } if(b==2)//无乘除法,加减法无负数 { for(int i=0;i<m;i++) { Random rand = new Random(); int firstnum = rand.nextInt(max)+min; int secondnum = rand.nextInt(max)+min; int thirdnum = rand.nextInt(max)+min; int fourthnum = rand.nextInt(max)+min; int num = rand.nextInt(2)+1; int flag=0; if(firstnum>secondnum){ int temp; temp = firstnum; firstnum = secondnum; secondnum = temp; } if(thirdnum>fourthnum){ int temp; temp = thirdnum; thirdnum = fourthnum; fourthnum = temp; } if(firstnum==secondnum&&thirdnum==fourthnum){ flag=1; } if(firstnum==secondnum&&thirdnum!=fourthnum){ flag=1; } if(firstnum!=secondnum&&thirdnum==fourthnum){ flag=1; } if(flag==1){ i=i-1; } else{ if(num==1) System.out.print(firstnum+"/"+secondnum+" + "+thirdnum+"/"+fourthnum+" = "); if(num==2) { int l; l=firstnum/secondnum-thirdnum/fourthnum; if(l<0){ i=i-1; } if(l>=0){ System.out.print(firstnum+"/"+secondnum+" - "+thirdnum+"/"+fourthnum+" = "); } } if(i%5==0){ System.out.println(); } } } } } } } }
运行结果截图:
实验总结:
首先,没有完整的完成实验,其中本程序并没有完成“是否有括号”这个参数,本程序只能单纯的输出题目还不能进行答案验证,还需后续的修改。其中我觉得比较有难度的是关于真分数部分真分数的设置,真分数为分子小于分母的分数。还有就是我定义的分子分母是int类型的,不知道有没有错误。待改进。
以上是关于四则运算02的主要内容,如果未能解决你的问题,请参考以下文章
获取 badarith,[erlang,'+',[error,0],[],同时使用 Erlang 片段在 TSUNG 中执行算术运算