class="java" name="code"> package test; /** * @author QuarterLifeForJava */ public class Test { public static void main(String[] args) { int x[] = {8,8,3,3}; int y[] = new int[x.length]; for(int a=0;a<4;a++){ for(int b=0;b<4;b++){ for(int c=0;c<4;c++){ for(int d=0;d<4;d++){ if(a!=b&&b!=c&&c!=d&&d!=a&&a!=c&&b!=d){ y[0] = x[a]; y[1] = x[b]; y[2] = x[c]; y[3] = x[d]; for(int i=0;i<4;i++){ circle(y,i); } } } } } } } //边界判断 public static boolean boundaryJudgment(double result){ if(result>=23.99999999999999&&result<=24.00000000000001){ return true; }else{ return false; } } //加 public static double jia(double x,double y){ return x+y; } //减 public static double jian(double x,double y){ return x-y; } //乘 public static double cheng(double x,double y){ return x*y; } //除 public static double chu(double x,double y){ return x/y; } //循环选择 public static double change(int num,double x,double y){ switch(num){ case 0: return jia(x,y); case 1: return jian(x,y); case 2: return cheng(x,y); case 3: return chu(x,y); } return 0; } //循环计算 public static void circle(int x[],int choice){ double mid = 0; double y = 0; double z = 0; for(int i=0;i<4;i++){ mid = change(i,x[0],x[1]); z = mid; for(int j=0;j<4;j++){ mid = z; switch(choice){ case 0: mid = change(j,mid,x[2]); for(int k=0;k<4;k++){ y = mid; mid = change(k,mid,x[3]); if(boundaryJudgment(mid)){ System.out.println("可算24点");//想输出结果,请自己写个方法,这里略了 System.exit(0); }else{ mid = y; } } break; case 1: mid = change(j,mid,x[2]); for(int k=0;k<4;k++){ y = mid; mid = change(k,x[3],mid); if(boundaryJudgment(mid)){ System.out.println("可算24点");//想输出结果,请自己写个方法,这里略了 System.exit(0); }else{ mid = y; } } break; case 2: mid = change(j,x[2],mid); for(int k=0;k<4;k++){ y = mid; mid = change(k,mid,x[3]); if(boundaryJudgment(mid)){ System.out.println("可算24点");//想输出结果,请自己写个方法,这里略了 System.exit(0); }else{ mid = y; } } break; case 3: mid = change(j,x[2],mid); for(int k=0;k<4;k++){ y = mid; mid = change(k,x[3],mid); if(boundaryJudgment(mid)){ System.out.println("可算24点");//想输出结果,请自己写个方法,这里略了 System.exit(0); }else{ mid = y; } } break; } } } } }