其他分享
首页 > 其他分享> > 二柱子四则预算生成进阶

二柱子四则预算生成进阶

作者:互联网

现在终于解决了随机生成括号的问题,操作数可以支持四个了

  1 package practice;
  2 
  3 import java.util.Random;
  4 import java.util.Scanner;
  5 
  6 public class add {
  7     public static void main(String[] args){
  8         char[] fh = new char[] {'+','-','×','÷'};
  9         int[][] cy = new int[1000][3];
 10         Random r= new Random(1);
 11         Scanner sc = new Scanner(System.in);
 12         System.out.println("请输入需要产生的题目数量.");
 13         int n1 = sc.nextInt();
 14         System.out.println("请输入操作个数(1.两个 2.三个 3.四个)");
 15         int n2 = sc.nextInt();
 16         System.out.println("请输入操作数的取值范围(先下限后上限):");
 17         int n3 = sc.nextInt();
 18         int n4 = sc.nextInt();
 19         System.out.println("是否添加括号?(1.是 2.否)");
 20         int n5 = sc.nextInt();
 21         if(n2==1) {
 22             if(n5==1)System.out.println("两个操作数加括号就没必要了吧");
 23             for(int i=0;i<n1;i++) {                    //两个操作数
 24                 int ran1 = r.nextInt(n4-n3+1)+n3;
 25                 int ran2 = r.nextInt(n4-n3+1)+n3;
 26                 int ch = r.nextInt(4);
 27                 int temp=0;
 28                 for(int j=0;j<i;j++) {
 29                     if(cy[j][0]==ran1&&cy[j][1]==ch&&cy[j][2]==ran2) {
 30                         i--;
 31                         temp=1;
 32                     }
 33                 }
 34                 if(temp==1)break;
 35                 cy[i][0] = ran1;
 36                 cy[i][1] = ch;
 37                 cy[i][2] = ran2;
 38                 System.out.println(ran1+" "+fh[ch]+" "+ran2);
 39             }
 40         }
 41         else if(n2==2) {                                //三个操作数
 42             if(n5==2) {
 43                 for(int i=0;i<n1;i++) {                    //三个操作数    无括号
 44                     int ran1 = r.nextInt(n4-n3+1)+n3;
 45                     int ran2 = r.nextInt(n4-n3+1)+n3;
 46                     int ran3 = r.nextInt(n4-n3+1)+n3;
 47                     int ch1 = r.nextInt(4);
 48                     int ch2 = r.nextInt(4);
 49                     System.out.println(ran1+" "+fh[ch1]+" "+ran2+" "+fh[ch2]+" "+ran3);
 50                 }
 51             }
 52             if(n5==1) {
 53                 for(int i=0;i<n1;i++) {                    //三个操作数    有括号
 54                     int temp = 0;
 55                     int ran1 = r.nextInt(n4-n3+1)+n3;
 56                     int ran2 = r.nextInt(n4-n3+1)+n3;
 57                     int ran3 = r.nextInt(n4-n3+1)+n3;
 58                     int ch1 = r.nextInt(4);
 59                     int ch2 = r.nextInt(4);
 60                     char[] kh = new char[6];
 61                     int k1 = r.nextInt(3)+1;
 62                     int k2 = r.nextInt(3)+1;
 63                     if(k2-k1>=1) {
 64                         kh[2*k1-2]='(';
 65                         kh[2*k2-1]=')';
 66                     }else {
 67                         --i;
 68                         temp = 1;
 69                     }
 70                     if(temp==1)continue;
 71                     System.out.println(kh[0]+" "+ran1+" "+kh[1]+" "+fh[ch1]+" "+kh[2]+" "+ran2+" "+kh[3]+" "+fh[ch2]+" "+kh[4]+" "+ran3+" "+kh[5]);
 72                 }
 73             }
 74         }
 75         if(n2==3) {
 76             if(n5==2) {
 77                 for(int i=0;i<n1;i++) {                    //四个操作数   无括号
 78                     int ran1 = r.nextInt(n4-n3+1)+n3;
 79                     int ran2 = r.nextInt(n4-n3+1)+n3;
 80                     int ran3 = r.nextInt(n4-n3+1)+n3;
 81                     int ran4 = r.nextInt(n4-n3+1)+n3;
 82                     int ch1 = r.nextInt(4);
 83                     int ch2 = r.nextInt(4);
 84                     int ch3 = r.nextInt(4);
 85                     System.out.println(ran1+" "+fh[ch1]+" "+ran2+" "+fh[ch2]+" "+ran3+" "+fh[ch3]+" "+ran4);
 86                 }
 87             }
 88             if(n5==1) {
 89                 for(int i=0;i<n1;i++) {                    //四个操作数    有括号
 90                     int temp = 0;
 91                     int ran1 = r.nextInt(n4-n3+1)+n3;
 92                     int ran2 = r.nextInt(n4-n3+1)+n3;
 93                     int ran3 = r.nextInt(n4-n3+1)+n3;
 94                     int ran4 = r.nextInt(n4-n3+1)+n3;
 95                     int ch1 = r.nextInt(4);
 96                     int ch2 = r.nextInt(4);
 97                     int ch3 = r.nextInt(4);
 98                     char[] kh = new char[8];
 99                     int k1 = r.nextInt(4)+1;
100                     int k2 = r.nextInt(4)+1;
101                     if(k2-k1>=1) {
102                         kh[2*k1-2]='(';
103                         kh[2*k2-1]=')';
104                     }else {
105                         --i;
106                         temp = 1;
107                     }
108                     if(temp==1)continue;
109                     System.out.println(kh[0]+" "+ran1+" "+kh[1]+" "+fh[ch1]+" "+kh[2]+" "+ran2+" "+kh[3]+" "+fh[ch2]+" "+kh[4]+" "+ran3+" "+kh[5]+" "+fh[ch3]+" "+kh[6]+" "+ran4+" "+kh[7]);
110             }
111         }
112     }
113 }
114 }

 

标签:柱子,进阶,int,kh,System,fh,println,预算,out
来源: https://www.cnblogs.com/SHINIAN200/p/15321821.html