首页 > 代码库 > 软件工程个人作业02
软件工程个人作业02
写一个能自动生成三十道小学四则运算题目的 “软件”,新的要求: 可以控制下列参数: 是否有乘除法; 是否有括号(最多可以支持十个数参与计算); 数值范围; 加减有无负数; 除法有无余数!
程序设计思想:
将要求的功能分别封装成类,在主函数中进行调用实现要求的功能。目前未能实现约分和多则运算,正在努力改进。
源代码:
package yunsuan; import java.util.Scanner; public class yunsuan { public static void main(String[]args) { yunsuan a=new yunsuan(); System.out.println("请选择数值范围(最大值)"); Scanner in =new Scanner(System.in); int range=in.nextInt(); System.out.println("请选择有无负数(1 or 2)"); int choose1=in.nextInt(); System.out.println("请选择有无乘除法(1 or 2)"); Scanner in3=new Scanner(System.in); int choose2=in3.nextInt(); Scanner in1=new Scanner(System.in); System.out.println("请输入要打印的数量"); int m=in1.nextInt(); for(int n=0;n<m;n++) { char[] c1={‘+‘,‘-‘,‘*‘,‘/‘}; int c=(int)(Math.random()*4); if(choose1==1&&choose2==1) { if(c1[c]==‘+‘) { a.add(range); } else if(c1[c]==‘-‘) { a.jian2(range); } else if(c1[c]==‘*‘) { a.chen(range); } else if(c1[c]==‘/‘) { a.chu1(range); } } else if((choose1==1&&choose2==2)) { if(c1[c]==‘+‘) { a.add(range); } else if(c1[c]==‘-‘) { a.jian2(range); } else if(c1[c]==‘*‘) { m++; } else if(c1[c]==‘/‘) { m++; } } else if((choose1==2&&choose2==2)) { if(c1[c]==‘+‘) { a.add(range); } else if(c1[c]==‘-‘) { a.jian1(range); } else if(c1[c]==‘*‘) { m++; } else if(c1[c]==‘/‘) { m++; } } else if((choose1==2&&choose2==1)) { if(c1[c]==‘+‘) { a.add(range); } else if(c1[c]==‘-‘) { a.jian1(range); } else if(c1[c]==‘*‘) { a.chen(range); } else if(c1[c]==‘/‘) { a.chu1(range); } } } } public void add(int range) { int answer=0; int x=getrandom(range); int y=getrandom(range); System.out.print(x); System.out.print(‘+‘); System.out.print(y); System.out.print("="); System.out.println(); System.out.println("请输入答案:"); Scanner in2=new Scanner(System.in); int q=in2.nextInt(); answer= x+ y; System.out.println(answer); if(q==answer) { System.out.println("回答正确!"); } else System.out.println("回答错误!"); } public void jian1(int range) { int answer=0; int x=getrandom(range); int y=getrandom(range); if( x> y) { System.out.print(x); System.out.print(‘-‘); System.out.print(y); System.out.print("="); System.out.println(); System.out.println("请输入答案:"); Scanner in2=new Scanner(System.in); int q=in2.nextInt(); answer=x-y; if(q==answer) { System.out.println("回答正确!"); } else System.out.println("回答错误!"); } } public void jian2(int range) { int answer=0; int x=getrandom(range); int y=getrandom(range); System.out.print(x); System.out.print(‘-‘); System.out.print(y); System.out.print("="); System.out.println(); System.out.println("请输入答案:"); Scanner in2=new Scanner(System.in); int q=in2.nextInt(); answer= x- y; System.out.println(answer); if(q==answer) { System.out.println("回答正确!"); } else System.out.println("回答错误!"); } public void chu1(int range) { int answer=0; int x=getrandom(range); int y=getrandom(range); System.out.print(x); System.out.print(‘/‘); System.out.print(y); System.out.print("="); System.out.println(); System.out.println("请输入答案:"); Scanner in2=new Scanner(System.in); int q=in2.nextInt(); answer= x/y; System.out.println(answer); if(q==answer) { System.out.println("回答正确!"); } else System.out.println("回答错误!"); } public void chen2(int range) { int answer=0; int x=getrandom(range); int y=getrandom(range); if(x<y) { System.out.print(x); System.out.print(‘*‘); System.out.print(y); System.out.print("="); System.out.println(); System.out.println("请输入答案:"); Scanner in2=new Scanner(System.in); int q=in2.nextInt(); answer= x*y; System.out.println(answer); if(q==answer) { System.out.println("回答正确!"); } else System.out.println("回答错误!"); } } public void chen(int range) { int answer=0; int x=getrandom(range); int y=getrandom(range); System.out.print(x); System.out.print(‘*‘); System.out.print(y); System.out.print("="); System.out.println(); System.out.println("请输入答案:"); Scanner in2=new Scanner(System.in); int q=in2.nextInt(); answer= x*y; System.out.println(answer); if(q==answer) { System.out.println("回答正确!"); } else System.out.println("回答错误!"); } public int getrandom(int range) { int x=(int) ( Math.random() * range ); return x; } }
程序截图:
软件工程个人作业02
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。