1.程序设计思想:
利用随机数产生两个数和运算符,加入定制数量,利用for循环实现输出,在每个+-*/里进行判断,不能出现假分数,不能出现负数,在加入结果计算,并且进行判断。
2.源程序代码:
package yunsuan; import java.util.Scanner; public class yunsuan { public static void main(String[]args) { Scanner in=new Scanner(System.in); System.out.println("请输入要打印的数量"); int m=in.nextInt(); int answer=0; for(int n=0;n<m;n++) { int a,b; char[] c1={'+','-','*','/'}; a = (int) ( Math.random() * 100 ); b = (int) ( Math.random() * 100 ); int c=(int)(Math.random()*4); if(c1[c]=='+') { System.out.print(a); System.out.print('+'); System.out.print(b); System.out.print("="); System.out.println(); System.out.println("请输入答案:"); Scanner in1=new Scanner(System.in); int q=in1.nextInt(); answer=a+b; if(q==answer) { System.out.println("回答正确!"); } else System.out.println("回答错误!"); } else if(c1[c]=='-') { if(a>b) { System.out.print(a); System.out.print('-'); System.out.print(b); System.out.print("="); System.out.println(); System.out.println("请输入答案:"); Scanner in1=new Scanner(System.in); int q=in1.nextInt(); answer=a-b; if(q==answer) { System.out.println("回答正确!"); } else System.out.println("回答错误!"); } else m++; } else if(c1[c]=='*') { System.out.print(a); System.out.print('*'); System.out.print(b); System.out.print("="); System.out.println(); System.out.println("请输入答案:"); Scanner in1=new Scanner(System.in); int q=in1.nextInt(); answer =a*b; if(q==answer) { System.out.println("回答正确!"); } else System.out.println("回答错误!"); } else if(c1[c]=='/') { if(a<b) { System.out.print(a); System.out.print('/'); System.out.print(b); System.out.print("="); System.out.println(); System.out.println("请输入答案:"); Scanner in1=new Scanner(System.in); int q=in1.nextInt(); answer=a/b; if(q==answer) { System.out.println("回答正确!"); } else System.out.println("回答错误!"); } else m++; } } } }
3.运行结果截图:
4.课上未完成原因:
因为Eclipse运行java代码出现某些故障,所以导致未能按时完成课堂作业