软件工程概论个人作业01
程序设计思路:生成批量的指定范围的随机数并且将至储存在数组中,之后对随机数组中的数字进行查重,如果出现重复的数组则重新生成新的数组。然后按照照映射方案将随机数组变成算式输出即可
程序源代码:
1 package 算术题; 2 import java.util.Random; 3 import java.util.Scanner; 4 import java.math.*; 5 public class suanshu { 6 7 8 public static void jisuan(int i,int m,int[][] array ){ 9 String string =new String(); 10 int a,b,c,d; 11 a=(int)(Math.random()*m); 12 b=(int)(Math.random()*m); 13 c=(int)(Math.random()*4); 14 if(c==0&&b==0){ 15 jisuan(i,m,array); 16 } 17 else{ 18 if(a<b&&c==2){ 19 d=a; 20 a=b; 21 b=d; 22 } 23 array[i][0]=a; 24 array[i][1]=b; 25 array[i][2]=c; 26 } 27 //return string; 28 } 29 30 public static void judge(int a,int m,int[][] array){ 31 for(int i=1;i<a;i++){ 32 for(int j=0;j<i;j++){ 33 if(array[i][0]==array[j][0]&&array[i][1]==array[j][1]&&array[i][2]==array[j][2]){ 34 jisuan(i,m,array); 35 } 36 } 37 } 38 } 39 public static void output(int m,int[][] array ){ 40 41 switch(array[m][2]){ 42 case 1: 43 System.out.println(array[m][0]+"+"+array[m][1]+"=");break; 44 case 2: 45 46 System.out.println(array[m][0]+"-"+array[m][1]+"=");break; 47 case 3: 48 System.out.println(array[m][0]+"*"+array[m][1]+"=");break; 49 case 0: 50 System.out.println(array[m][0]+"/"+array[m][1]+"=");break; 51 } 52 } 53 public static void main(String[] args) { 54 // TODO Auto-generated method stub 55 56 System.out.println("请输入定制的数量:"); 57 int a,b,c; 58 Scanner scanner=new Scanner(System.in); 59 a=scanner.nextInt(); 60 int[][] array = new int[a][3]; 61 System.out.println("请输入数字的范围:"); 62 b=scanner.nextInt(); 63 for(int i=0;i<a;i++){ 64 jisuan(i,b,array); 65 } 66 judge(a,b,array); 67 for(int i=0;i<a;i++){ 68 System.out.print((i+1)+" "); 69 output(i,array); 70 } 71 } 72 73 }
程序运行截图:
上课的时候已经完成基本的生成随机算式的程序,回去后又对程序进行优化升级。完成整个任务。
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步