import java.util.Scanner; import java.util.Random; public class test{ public static int s1 = new Random().nextInt(100); public static int s2 = new Random().nextInt(100); public static int s3 = new Random().nextInt(100); public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.println("请输入想要的算法个数:"); int amout = in.nextInt(); System.out.println("请输入想要每行个数:"); int line = in.nextInt(); int[][] sum = new int[10000][3]; for(int i=0;i<amout;i++) { if(i%line==0) { System.out.print("\n"); } s3 = new Random().nextInt(100); sum[i][2]=s3; s1 = new Random().nextInt(100); s2 = new Random().nextInt(100); if((s3%4)==0){ if(s2>s1){ while(s2>s1) { s1 = new Random().nextInt(100); s2 = new Random().nextInt(100); } } } if((s3%4)==1) { sum[i][0]=s1; sum[i][1]=s2; } if((s3%4)==2) { if(s1*s2>100) { while(s1*s2>100){ s1 = new Random().nextInt(100); s2 = new Random().nextInt(100); } } } if((s3%4)==3) { while(s2==0) { s2 = new Random().nextInt(100); } if(s1%s2!=0) { while(s2==0) { s2 = new Random().nextInt(100); } while(s1%s2!=0){ s1 = new Random().nextInt(100); s2 = new Random().nextInt(100); while(s2==0) { s2 = new Random().nextInt(100); } } } } sum[i][0]=s1; sum[i][1]=s2; if(i>1) { if(sum[i-1][0]==sum[i][0]&&sum[i-1][1]==sum[i][1]&&sum[i-1][2]%4==sum[i][2]%4) { do { sum[i][0]=new Random().nextInt(100); sum[i][1]=new Random().nextInt(100); sum[i][2]=new Random().nextInt(100); }while(sum[i][0]!=sum[i-1][0]&&sum[i][1]!=sum[i-1][1]&&sum[i][2]%4!=sum[i+1][2]%4); } } if(s3%4==0) { System.out.print(i+1 +":"+ s1 + "-" + s2 +"= "); } if(s3%4==1) { System.out.print(i+1 +":"+ s1 + "+" + s2 +"= "); } if(s3%4==2) { System.out.print(i+1 +":"+ s1 + "*" + s2 +"= "); } if(s3%4==3) { System.out.print(i+1 +":"+ s1 + "/" + s2 +"= "); } } } }
程序设计思路:程序主要运用了Random函数产生随机数,将s3%4做为符号确定数,再在其中每一个条件中控制该运算符的相应条件。最后在用数组存入相应数据,将其进行判断。最后按照要求输出!
设计程序可以锻炼逻辑思维能力,以后会每天坚持敲写代码,提高自己的编程能力。
来源:博客园
作者:doublebest1
链接:https://www.cnblogs.com/yangxionghao/p/11538404.html