24点java代码的简单介绍

java算24点代码:输入4个数算24点,能够在命令提示符下就可以运行。100多

import java.util.Scanner;

10年积累的网站制作、网站建设经验,可以快速应对客户对网站的新想法和需求。提供各种问题对应的解决方案。让选择我们的客户得到更好、更有力的网络服务。我虽然不认识你,你也不认识我。但先网站制作后付款的网站建设流程,更有清徐免费网站建设让你可以放心的选择与我们合作。

/** 给定4个数字计算24 */

public class Core {

private double expressionResult = 24;

// private int maxLine=10;

private boolean error = true;

private double numbers[] = new double[4];

public Object resultReturn;

/**

* 该对象拥有3个私有变量 expressionResult,所需结果 maxLine,输出结果每页行数 error,是否出错

* numbers[4],记录用来运算的4个数

*

* 其次,该对象拥有以下方法供外部调用 setNumbers(double[] 运算的数) 输入用来运算的数,4个时才能计算,无返回

* setMaxLine(int 行数) 输入每页的行数,无返回 getMaxLine() 返回每页的行数,类型为int

* setExpressionResult(double 所需结果) 输入所需结果,无返回 getExpressionResult()

* 返回所需结果,类型为double getExpression() 返回可得出所需结果的表达式,类型为字符串数组

*

* 最后,私有方法均为计算与表达式转换部分

*/

// 测试使用

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

int[] arr = new int[4];

System.out.print("输入第一个数:");

arr[0] = scanner.nextInt();

System.out.print("输入第二个数:");

arr[1] = scanner.nextInt();

System.out.print("输入第三个数:");

arr[2] = scanner.nextInt();

System.out.print("输入第四个数:");

arr[3] = scanner.nextInt();

Core s = new Core();

s.setNumbers(arr);

String[] output = s.getExpression();

for (int i = 0; i output.length; i++) {

System.out.println(output[i]);

}

}

/** 设定被计算的四个数,由于是数组,所以具有容错功能(不为4个数) */

public void setNumbers(double[] n) {

if (n.length == 4) {

error = false;

numbers = n;

} else

error = true;

}

public void setNumbers(int[] n) {

if (n.length == 4) {

error = false;

for (int i = 0; i 4; i++) {

numbers[i] = n[i];

}

} else

error = true;

}

/** 设定每页显示的行数 */

// public void setMaxLine(int n) {

// if (n0) {

// maxLine=n;

// }

// }

// /** 返回每页显示的行数 */

// public int getMaxLine() {

// return maxLine;

// }

/** 设定需要得到的结果 */

public void setExpressionResult(double n) {

expressionResult = n;

}

/** 返回所需结果 */

public double expressionResult() {

return expressionResult;

}

/** 返回符合条件的表达式 */

public String[] getExpression() {

if (!error) {

String[] expression = calculate(numbers);

return expression;

} else

return new String[] { "出错了,输入有误" };

}

/** cal24(),输出结果为24的表达式 */

private String[] calculate(double[] n) {

if (n.length != 4)

return new String[] { "Error" };

double[] n1 = new double[3];

double[] n2 = new double[2];

String[] resultString = new String[1024]; // 最多1000组解,暂时未溢出

int count = 0;

boolean isRepeat = false;

for (int t1 = 0; t1 6; t1++) {

for (int c1 = 0; c1 6; c1++) {

for (int t2 = 0; t2 3; t2++) {

for (int c2 = 0; c2 6; c2++) {

for (int c3 = 0; c3 6; c3++) {

if ((c1 / 3 == c2 / 3 (c1 % 3) * (c2 % 3) != 0)

|| (c2 / 3 == c3 / 3 (c2 % 3) * (c3 % 3) != 0)

|| (c1 / 3 == c3 / 3

(c1 % 3) * (c3 % 3) != 0 t2 == 2)) {

// 去除连减连除的解,因为x/(y/z)=x*z/y

continue;

}

n1 = cal1(n, t1, c1);

n2 = cal2(n1, t2, c2);

double result = cal(n2[0], n2[1], c3);

if ((result - expressionResult) 0.00000001

(expressionResult - result) 0.00000001) {

resultString[count] = calString(n, t1, c1, t2,

c2, c3)

+ "=" + (int) expressionResult;

for (int i = 0; i count; i++) {

isRepeat = false;

if (resultString[i]

.equals(resultString[count])) { // 去除完全重复的解

isRepeat = true;

break; // 提前退出循环

}

}

if (c1 == c2 c2 == c3 c1 % 3 == 0

t1 + t2 != 0) { // 连加连乘

isRepeat = true;

}

if (!isRepeat) {

count++;

}

}

}

}

}

}

}

if (count == 0)

return new String[] { "该组数无解" };

String[] resultReturn = new String[count];

System.arraycopy(resultString, 0, resultReturn, 0, count);

return resultReturn;

}

/** cal1(),将4个数计算一次后返回3个数 */

private double[] cal1(double[] n, int t, int c) { // t为原来的t1,c为原来的c1

double[] m = new double[3];

switch (t) {

case 0:

m[1] = n[2];

m[2] = n[3];

m[0] = cal(n[0], n[1], c);

break;

case 1:

m[1] = n[1];

m[2] = n[3];

m[0] = cal(n[0], n[2], c);

break;

case 2:

m[1] = n[1];

m[2] = n[2];

m[0] = cal(n[0], n[3], c);

break;

case 3:

m[1] = n[0];

m[2] = n[3];

m[0] = cal(n[1], n[2], c);

break;

case 4:

m[1] = n[0];

m[2] = n[2];

m[0] = cal(n[1], n[3], c);

break;

default:

m[1] = n[0];

m[2] = n[1];

m[0] = cal(n[2], n[3], c);

}

return m;

}

/** cal2(),将3个数计算一次后返回2个数 */

private double[] cal2(double[] n, int t, int c) { // t为原来的t2,c为原来的c2

double[] m = new double[2];

switch (t) {

case 0:

m[1] = n[2];

m[0] = cal(n[0], n[1], c);

break;

case 1:

m[1] = n[1];

m[0] = cal(n[0], n[2], c);

break;

default:

m[1] = n[0];

m[0] = cal(n[1], n[2], c);

}

return m;

}

/** cal(),将2个数计算后返回结果 */

private double cal(double n1, double n2, int c) { // n1,n2为运算数,c为运算类型

switch (c) {

case 0:

return n1 + n2;

case 1:

return n1 - n2;

case 2:

return n2 - n1;

case 3:

return n1 * n2;

case 4:

if (n2 == 0)

return 9999; // 使计算结果必不为24

else

return n1 / n2;

default:

if (n1 == 0)

return 9999; // 同上

else

return n2 / n1;

}

}

/** calString(),输出表达式 */

private String calString(double[] n, int t1, int c1, int t2, int c2, int c3) {

String[] nString = new String[4];

switch (t1) {

case 0:

nString[0] = calString2("" + (int) n[0], "" + (int) n[1], c1);

nString[1] = "" + (int) n[2];

nString[2] = "" + (int) n[3];

break;

case 1:

nString[0] = calString2("" + (int) n[0], "" + (int) n[2], c1);

nString[1] = "" + (int) n[1];

nString[2] = "" + (int) n[3];

break;

case 2:

nString[0] = calString2("" + (int) n[0], "" + (int) n[3], c1);

nString[1] = "" + (int) n[1];

nString[2] = "" + (int) n[2];

break;

case 3:

nString[0] = calString2("" + (int) n[1], "" + (int) n[2], c1);

nString[1] = "" + (int) n[0];

nString[2] = "" + (int) n[3];

break;

case 4:

nString[0] = calString2("" + (int) n[1], "" + (int) n[3], c1);

nString[1] = "" + (int) n[0];

nString[2] = "" + (int) n[2];

break;

default:

nString[0] = calString2("" + (int) n[2], "" + (int) n[3], c1);

nString[1] = "" + (int) n[0];

nString[2] = "" + (int) n[1];

}

if ((c2 / 3 c1 / 3 (t2 != 2 || c2 / 3 == c3 / 3))

|| ((c3 / 3 c1 / 3 + c2 / 3) t2 == 2)

|| (c3 == 1 c1 / 3 == 0)) // 特定情况下加上一个括号*****************************

nString[0] = '(' + nString[0] + ')';

switch (t2) {

case 0:

nString[0] = calString2(nString[0], "" + nString[1], c2);

nString[1] = nString[2];

break;

case 1:

nString[0] = calString2(nString[0], nString[2], c2);

break;

default:

nString[3] = nString[0];

nString[0] = calString2(nString[1], nString[2], c2);

nString[1] = nString[3];

}

if (c3 / 3 c2 / 3 || (c3 == 2 nString[0].indexOf('+') = 0)) // 特定情况下加上一个括号*****************************

nString[0] = '(' + nString[0] + ')';

return calString2(nString[0], nString[1], c3);

}

/** calString(),根据符号输出一部运算表达式 */

private String calString2(String n1, String n2, int c) {

switch (c) {

case 0:

return n1 + '+' + n2;

case 1:

return n1 + '-' + n2;

case 2:

return n2 + '-' + n1;

case 3:

return n1 + '*' + n2;

case 4:

return n1 + '/' + n2;

default:

return n2 + '/' + n1;

}

}

}

用JAVA如何算出24点?

24点的源代码,因该可以计算出4则运算24 public class Test24Point{ public static void main(String[] args){ int index = 0 ; int temp = 0 ; int totalSUC = 0 ; int numb[] = new int[4];//the first four numbers double num[][] = new double[36][3];//three numbers after calculating double total[] = new double[6];//the number after three steps of calculating double p[][] = new double[6][8]; double q[][] = new double[3][7]; //System.out.println(2465%108); //System.out.println(2465/108); System.out.println("\"a--b\"means\"b-a\""); System.out.println("\"a//b\"means\"b/a\"\n"); /* for(int h = 0; h = 9; h ++)//Get the first four numbers for calculating and store into the array numb[4]; for(int i = 0; i = 9; i ++) for(int j = 0; j = 9; j ++) for(int k = 0; k = 9; k ++){ numb[0] = h ; numb[1] = i ; numb[2] = j ; numb[3] = k ; }*/ for(int i = 0 ; i 4 ; i ++){ numb = Integer.parseInt(args); } for(int i = 0; i 3; i ++)//Get two of the four to calculate and then store the new number into the array p; for(int j = i + 1; j 4 ; j ++,temp ++){ p[temp][0] = numb + numb[j]; p[temp][1] = numb - numb[j]; p[temp][2] = numb[j] - numb; p[temp][3] = numb * numb[j]; if(numb[j] != 0) p[temp][4] = numb / (double)numb[j]; else p[temp][4] = 10000; if(numb != 0) p[temp][5] = numb[j] / (double)numb; else p[temp][5] = 10000;

24点速算游戏 Java 代码

import java.util.ArrayList; 

import java.util.Arrays; 

import java.util.Collection; 

import java.util.HashSet; 

import java.util.List; 

import java.util.Set; 

/** 

* 用给定的4个整数通过加减乘除运算得到24点,如果有多种情况,则全部输出,如果不能得到24点,输出提示br 

* @思路:将指定的4个数字进行全排列,将运算符‘+’、‘-’、‘*’、‘/’取3个进行所有情况排列, 

* 然后将所有的数字排列与所有的运算符排列按顺序计算, 

* 如果最后计算结果等于想要的结果值比如24,则为符合条件的运算, 

* 将所有符合条件的数字排列和运算符排列存储起来,并在最后打印输出所有可能的情况 

* @author chenjie 

*/ 

public class TwentyFourPoint { 

public static void main(String[] args) { 

try { 

SetString set = caculate(new int[] { 18, 18, 6, 12 }, 24); 

printlnResultSet(set); 

} catch (Exception e) { 

// e.printStackTrace();开发期间方便查找错误,测试通过后就无需打印错误信息了 

System.err.println(e.getMessage()); 

/** 

* 打印结果集 

* @param resultSet 

* 结果集 

*/ 

private static void printlnResultSet(CollectionString resultSet) { 

for (String str : resultSet) { 

System.out.println(str); 

/** 

* 得到给定整形数组的全排列情况 

* @param numbers 

* 给定的整形数组 

* @return 全排列数组 

*/ 

private static int[][] arrangeAllNumbers(int[] numbers) { 

Listint[] list = new ArrayListint[](); 

allSort(numbers, 0, numbers.length - 1, list); 

int[][] resultSet = new int[list.size()][list.get(0).length]; 

resultSet = list.toArray(resultSet); 

return resultSet; 

/** 

* 得到给定的操作中出现的所有操作符排列情况 

* @param operators 

* 出现的操作符数组 

* @param number 

* 每组操作符的数量 

* @return 所有操作符排列数组 

*/ 

private static char[][] arrangeAllOperators(char[] operators, int number) { 

int setSize = (int) Math.pow(operators.length, number); 

int index = 0; 

char[][] resultSet = new char[setSize][number]; 

for (int i = 0; i  operators.length; i++) { 

for (int j = 0; j  operators.length; j++) { 

for (int k = 0; k  operators.length; k++) { 

resultSet[index][0] = operators[i]; 

resultSet[index][1] = operators[j]; 

resultSet[index][2] = operators[k]; 

index++; 

return resultSet; 

/** 

* 根据给定的一组整数,通过加减乘除运算,得到想要的结果,如果可以得到结果,则返回所有可能的结果的运算形式。 

* 返回的运算形式,均按从左到右的顺序计算,并不是遵循四则运算法则,比如:br 

* 输出的结果形式为:br 

* 1 * 8 - 6 * 12 = 24 br 

* 表示的运算顺序是:br 

* 1:1 * 8 = 8,br 

* 2:8 - 6 = 2,br 

* 3:2 * 12 = 24br 

* 而不是按照四则运算法则计算:br 

* 1:1 * 8 = 8,br 

* 2:6 * 12 = 72,br 

* 3:8 * 72 = 576br 

* @param numbers 

* 给定进行运算的一组整数,4个数为一组 

* @param targetNumber 

* 想要得到的结果 

* @return 所有可能得到想要的结果的所有运算形式的字符串形式集合 

* @throws Exception 

* 如果不能得到想要的结果,则抛出该异常,表明根据指定的一组数字通过一系列的加减乘除不能得到想要的结果 

*/ 

public static SetString caculate(int[] numbers, int targetNumber) 

throws Exception { 

SetString resultSet = new HashSetString();// 这里用Set而不是用List,主要是因为当给定的一组数字中如果有重复数字的话,同一结果会被出现多次,如果用List存放的话,会将重复的结果都存放起来,而Set会自动消除重复值 

char[][] operatorsArrangement = arrangeAllOperators(new char[] { '+', 

'-', '*', '/' }, 3); 

int[][] numbersArrangement = arrangeAllNumbers(numbers); 

for (int[] nums : numbersArrangement) 

for (char[] operators : operatorsArrangement) { 

int result = 0; 

try { 

result = caculate(nums, operators); 

} catch (Exception e) {// 出现非精确计算 

continue; 

if (result == targetNumber) 

resultSet.add(buildString(nums, operators, targetNumber));// 如果计算后的结果等于想要的结果,就存放到集合中 

if (resultSet.isEmpty()) 

throw new Exception("给定的数字:" + Arrays.toString(numbers) 

+ "不能通过加减乘除运算得到结果:" + targetNumber); 

return resultSet; 

/** 

* 将一组整型数字以给定的操作符按顺序拼接为一个完整的表达式字符串 

* @param nums 

* 一组整型数字 

* @param operators 

* 一组操作符 

* @param target 

* 目标值 

* @return 拼接好的表达式字符串 

*/ 

private static String buildString(int[] nums, char[] operators, int target) { 

String str = String.valueOf(nums[0]); 

for (int i = 0; i  operators.length; i++) { 

str = str + ' ' + operators[i] + ' ' + nums[i + 1]; 

str = str + " = " + target; 

return str; 

/** 

* 将给定的一组数字以给定的操作符按顺序进行运算,如:int result = caculate(new int[]{3,4,5,8}, new 

* char[]{'+','-','*'}); 

* @param nums 

* 一组数字 

* @param operators 

* 一组运算符,数量为数字的个数减1 

* @return 最后的计算结果 

* @throws Exception 

* 当计算结果不精确时,抛出该异常,主要是针对除法运算,例如18 / 8 = 2,诸如这样不精确计算将抛出该异常 

*/ 

private static int caculate(int[] nums, char[] operators) throws Exception { 

int result = 0; 

for (int i = 0; i  operators.length; i++) { 

if (i == 0) { 

result = caculate(nums[i], nums[i + 1], operators[i]); 

} else { 

result = caculate(result, nums[i + 1], operators[i]); 

return result; 

/** 

* 根据指定操作符将两个给定的数字进行计算 

* @param num1 

* 数字1 

* @param num2 

* 数字2 

* @param operator 

* 操作符,只能从“+、-、*、/”4个操作符中取值 

* @return 计算结果 

* @throws Exception 

* 当计算结果不精确时,抛出该异常,主要是针对除法运算,例如18 / 8 = 2,诸如这样不精确计算将抛出该异常 

*/ 

private static int caculate(int num1, int num2, char operator) 

throws Exception { 

double result = 0; 

switch (operator) {// 根据操作符做相应的计算操作 

case '+': 

result = num1 + num2; 

break; 

case '-': 

result = num1 - num2; 

break; 

case '*': 

result = num1 * num2; 

break; 

case '/': 

result = (double) num1 / (double) num2; 

break; 

if (!check(result)) 

throw new Exception("不精确的计算数字"); 

return (int) result; 

/** 

* 检查指定的浮点数是否可以直接转换为整型数字而不损失精度 

* @param result 

* 要检查的浮点数 

* @return 如果可以进行无损转换,返回true,否则返回false 

*/ 

private static boolean check(double result) { 

String str = String.valueOf(result); 

int pointIndex = str.indexOf(".");// 小数点的下标值 

String fraction = str.substring(pointIndex + 1); 

return fraction.equals("0") ? true : false;// 通过判断小数点后是否只有一个0来确定是否可以无损转换为整型数值 

/** 

* 对传入的整型数组buf进行全排列 

* @param buf 

* 要进行全排列的整型数组 

* @param start 

* 开始的下标值 

* @param end 

* 结束下标值 

* @param list 

* 保存最后全排列结果的集合 

*/ 

private static void allSort(int[] buf, int start, int end, Listint[] list) { 

if (start == end) {// 当只要求对数组中一个字母进行全排列时,只要就按该数组输出即可 

int[] a = new int[buf.length]; 

System.arraycopy(buf, 0, a, 0, a.length); 

list.add(a); 

} else {// 多个字母全排列 

for (int i = start; i = end; i++) { 

int temp = buf;// 交换数组第一个元素与后续的元素 

buf = buf[i]; 

buf[i] = temp; 

allSort(buf, start + 1, end, list);// 后续元素递归全排列 

temp = buf;// 将交换后的数组还原 

buf = buf[i]; 

buf[i] = temp; 

求 JAVA 算24点的代码

import java.util.ArrayList;

import java.util.List;

import java.util.regex.Matcher;

import java.util.regex.Pattern;

import javax.swing.ImageIcon;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JOptionPane;

import javax.swing.JScrollPane;

import javax.swing.JTextArea;

import javax.swing.JTextField;

public class calculate24 extends JFrame{

private javax.swing.JPanel jContentPane = null;

private JLabel jLabel = null;

private JLabel jLabel1 = null;

private JTextField jTextField = null;

private JTextField jTextField1 = null;

private JTextArea jTextArea = null;

private JLabel jLabel2 = null;

private JButton jButton = null;

private JScrollPane jScrollPane = null;

private JButton jButton1 = null;

private JButton jButton2 = null;

private JButton jButton3 = null;

private JButton jButton4 = null;

private JButton jButton5 = null;

private JButton jButton6 = null;

private JButton jButton7 = null;

private JButton jButton8 = null;

private JButton jButton9 = null;

private JButton jButton10 = null;

/**

* This is the default constructor

*/

public calculate24() {

super();

initialize();

}

/**

* This method initializes this

*

* @return void

*/

private void initialize() {

this.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);

this.setBounds(200, 200, 565, 452);

this.setContentPane(getJContentPane());

this.setTitle("24点");

}

/**

* This method initializes jContentPane

*

* @return javax.swing.JPanel

*/

private javax.swing.JPanel getJContentPane() {

if (jContentPane == null) {

jLabel2 = new JLabel();

jLabel1 = new JLabel();

jLabel = new JLabel();

jContentPane = new javax.swing.JPanel();

jContentPane.setLayout(null);

jLabel.setBounds(66, 52, 150, 45);

jLabel.setText("please unter four number");

jLabel1.setBounds(253, 52, 282, 45);

jLabel1.setText("please unter how many result do you want to get");

jLabel2.setBounds(354, 201, 70, 36);

jLabel2.setText("result");

jContentPane.add(getJButton(), null);

jContentPane.add(jLabel, null);

jContentPane.add(jLabel1, null);

jContentPane.add(getJTextField(), null);

jContentPane.add(getJTextField1(), null);

jContentPane.add(jLabel2, null);

jContentPane.add(getJScrollPane(), null);

jContentPane.add(getJButton1(), null);

jContentPane.add(getJButton2(), null);

jContentPane.add(getJButton3(), null);

jContentPane.add(getJButton4(), null);

jContentPane.add(getJButton5(), null);

jContentPane.add(getJButton6(), null);

jContentPane.add(getJButton7(), null);

jContentPane.add(getJButton8(), null);

jContentPane.add(getJButton9(), null);

jContentPane.add(getJButton10(), null);

}

return jContentPane;

}

/**

* This method initializes jTextField

*

* @return javax.swing.JTextField

*/

private JTextField getJTextField() {

if (jTextField == null) {

jTextField = new JTextField();

jTextField.setBounds(67, 84, 149, 41);

jTextField.addFocusListener(new java.awt.event.FocusAdapter() {

public void focusGained(java.awt.event.FocusEvent e) {

jTextField.select(0,jTextField.getText().length());

}

});

}

return jTextField;

}

/**

* This method initializes jTextField1

*

* @return javax.swing.JTextField

*/

private JTextField getJTextField1() {

if (jTextField1 == null) {

jTextField1 = new JTextField();

jTextField1.setBounds(293, 81, 161, 41);

jTextField1.setNextFocusableComponent(jButton);

}

return jTextField1;

}

/**

* This method initializes jTextArea

*

* @return javax.swing.JTextArea

*/

private JTextArea getJTextArea() {

if (jTextArea == null) {

jTextArea = new JTextArea();

jTextArea.setTabSize(8);

}

return jTextArea;

}

public static String bbb(List list1, List list2) {

float result = 0;

for (int i = list1.size(); i 0; i-- ) {

if (list1.contains("*")) {

int j = list1.indexOf("*");

result = Float.parseFloat((String)list2.get(j))

* Float.parseFloat((String)list2.get(j + 1));

list1.remove(j);

list2.remove(j);

list2.remove(j);

list2.add(j, String.valueOf(result));

} else if (list1.contains("/")) {

int j = list1.indexOf("/");

result = Float.parseFloat((String)list2.get(j))

/ Float.parseFloat((String)list2.get(j + 1));

list1.remove(j);

list2.remove(j);

list2.remove(j);

list2.add(j, String.valueOf(result));

} else if (list1.contains("+")) {

int j = list1.indexOf("+");

result = Float.parseFloat((String)list2.get(j))

+ Float.parseFloat((String)list2.get(j + 1));

list1.remove(j);

list2.remove(j);

list2.remove(j);

list2.add(j, String.valueOf(result));

} else if (list1.contains("-")) {

int j = list1.indexOf("-");

result = Float.parseFloat((String)list2.get(j))

- Float.parseFloat((String)list2.get(j + 1));

list1.remove(j);

list2.remove(j);

list2.remove(j);

list2.add(j, String.valueOf(result));

}

}

return (String)list2.get(0);

}

private static void bbb(String str, String sPrint, List list) {

if (!"".equals(str.trim()) ? false : list.add(sPrint))

;

for (int i = 0; i str.length() ( !"".equals(str.trim()) ); i++ )

if (str.charAt(i) != ' ')

bbb(str.replace(str.charAt(i), ' '), sPrint + str.charAt(i),

list);

}

private static List bbb(String str, List list) {

List result = new ArrayList();

String a1 = str.substring(0, 1);

String b1 = str.substring(1, 2);

String c1 = str.substring(2, 3);

String d1 = str.substring(3, 4);

String[] a11 = new String[] { a1, b1, c1, d1 };

for (int i = 0; i list.size(); i++ ) {

String temp = (String)list.get(i);

int a = Integer.parseInt(temp.substring(0, 1));

int b = Integer.parseInt(temp.substring(1, 2));

int c = Integer.parseInt(temp.substring(2, 3));

int d = Integer.parseInt(temp.substring(3, 4));

String tempStr = a11[a] + a11[b] + a11[c] + a11[d];

if(!result.contains(tempStr)){

result.add(tempStr);

}

}

return result;

}

public List test(String param, int x) {

int y = 0;

List result = new ArrayList();

List a11 = new ArrayList();

calculate24.bbb("0123", "", a11);

List a1 = calculate24.bbb(param, a11);

for (int m = 0; m a1.size(); m++ ) {

String param1 = (String)a1.get(m);

int[] a = new int[] { Integer.parseInt(param1.substring(0, 1)),

Integer.parseInt(param1.substring(1, 2)),

Integer.parseInt(param1.substring(2, 3)),

Integer.parseInt(param1.substring(3, 4)) };

String[] e = new String[] { "*", "/", "+", "-" };

for (int i = 0; i 4; i++ ) {

for (int j = 0; j 4; j++ ) {

for (int k = 0; k 4; k++ ) {

List aa = new ArrayList();

aa.add(String.valueOf(a[0]));

aa.add(String.valueOf(a[1]));

aa.add(String.valueOf(a[2]));

aa.add(String.valueOf(a[3]));

List bb = new ArrayList();

bb.add(e[i]);

bb.add(e[j]);

bb.add(e[k]);

String s = a[0] + e[i] + a[1] + e[j] + a[2] + e[k]

+ a[3];

String tempS = s;

s = calculate24.bbb(bb, aa);

if (Float.parseFloat(s) == 24) {

y++ ;

result.add(tempS + "=24");

if (y == x) {

return result;

}

}

List temp1 = new ArrayList();

List temp2 = new ArrayList();

temp1.add(String.valueOf(a[0]));

temp1.add(String.valueOf(a[1]));

temp2.add(e[i]);

String temp = calculate24.bbb(temp2, temp1);

aa.clear();

aa.add(temp);

aa.add(String.valueOf(a[2]));

aa.add(String.valueOf(a[3]));

bb.clear();

bb.add(e[j]);

bb.add(e[k]);

s = "(" + a[0] + e[i] + a[1] + ")" + e[j] + a[2] + e[k]

+ a[3];

tempS = s;

s = calculate24.bbb(bb, aa);

if (Float.parseFloat(s) == 24) {

y++ ;

result.add(tempS + "=24");

if (y == x) {

return result;

}

}

temp1.clear();

temp2.clear();

temp1.add(String.valueOf(a[1]));

temp1.add(String.valueOf(a[2]));

temp2.add(e[j]);

temp = calculate24.bbb(temp2, temp1);

aa.clear();

aa.add(String.valueOf(a[0]));

aa.add(temp);

aa.add(String.valueOf(a[3]));

bb.clear();

bb.add(e[i]);

bb.add(e[k]);

s = a[0] + e[i] + "(" + a[1] + e[j] + a[2] + ")" + e[k]

+ a[3];

tempS = s;

s = calculate24.bbb(bb, aa);

if (Float.parseFloat(s) == 24) {

y++ ;

result.add(tempS + "=24");

if (y == x) {

return result;

}

}

temp1.clear();

temp2.clear();

temp1.add(String.valueOf(a[2]));

temp1.add(String.valueOf(a[3]));

temp2.add(e[k]);

temp = calculate24.bbb(temp2, temp1);

aa.clear();

aa.add(String.valueOf(a[0]));

aa.add(String.valueOf(a[1]));

aa.add(temp);

bb.clear();

bb.add(e[i]);

bb.add(e[j]);

s = a[0] + e[i] + a[1] + e[j] + "(" + a[2] + e[k]

+ a[3] + ")";

tempS = s;

s = calculate24.bbb(bb, aa);

if (Float.parseFloat(s) == 24) {

y++ ;

result.add(tempS + "=24");

if (y == x) {

return result;

}

}

temp1.clear();

temp2.clear();

temp1.add(String.valueOf(a[0]));

temp1.add(String.valueOf(a[1]));

temp1.add(String.valueOf(a[2]));

temp2.add(e[i]);

temp2.add(e[j]);

temp = calculate24.bbb(temp2, temp1);

aa.clear();

aa.add(temp);

aa.add(String.valueOf(a[3]));

bb.clear();

bb.add(e[k]);

s = "(" + a[0] + e[i] + a[1] + e[j] + a[2] + ")" + e[k]

+ a[3];

tempS = s;

s = calculate24.bbb(bb, aa);

if (Float.parseFloat(s) == 24) {

y++ ;

result.add(tempS + "=24");

if (y == x) {

return result;

}

}

temp1.clear();

temp2.clear();

temp1.add(String.valueOf(a[1]));

temp1.add(String.valueOf(a[2]));

temp1.add(String.valueOf(a[3]));

temp2.add(e[j]);

temp2.add(e[k]);

temp = calculate24.bbb(temp2, temp1);

aa.clear();

aa.add(String.valueOf(a[0]));

aa.add(temp);

bb.clear();

bb.add(e[i]);

s = a[0] + e[i] + "(" + a[1] + e[j] + a[2] + e[k]

+ a[3] + ")";

tempS = s;

s = calculate24.bbb(bb, aa);

if (Float.parseFloat(s) == 24) {

y++ ;

result.add(tempS + "=24");

if (y == x) {

return result;

}

}

temp1.clear();

temp2.clear();

temp1.add(String.valueOf(a[0]));

temp1.add(String.valueOf(a[1]));

temp2.add(e[i]);

temp = calculate24.bbb(temp2, temp1);

List temp3 = new ArrayList();

List temp4 = new ArrayList();

temp3.add(String.valueOf(a[2]));

temp3.add(String.valueOf(a[3]));

temp4.add(e[k]);

String temp11 = calculate24.bbb(temp4, temp3);

aa.clear();

aa.add(temp);

aa.add(temp11);

bb.clear();

bb.add(e[j]);

s = "(" + a[0] + e[i] + a[1] + ")" + e[j] + "(" + a[2]

+ e[k] + a[3] + ")";

tempS = s;

s = calculate24.bbb(bb, aa);

if (Float.parseFloat(s) == 24) {

y++ ;

result.add(tempS + "=24");

if (y == x) {

return result;

}

}

}

}

}

}

return result;

}

public static boolean check(String param1) {

Pattern pattern = Pattern.compile("[0-9]{4}");

Matcher matcher = pattern.matcher((CharSequence)param1);

boolean result = matcher.matches();

if (result == false) {

JOptionPane.showMessageDialog(null, "please enter correct number");

return false;

} else {

return true;

}

}

public static boolean check1(String param2) {

if(param2 == null){

JOptionPane.showMessageDialog(null, "please enter correct number");

return false;

}

Pattern pattern = Pattern.compile("[0-9]{0,99}");

Matcher matcher = pattern.matcher((CharSequence)param2);

boolean result = matcher.matches();

if (result == false) {

JOptionPane.showMessageDialog(null, "please enter correct number");

return false;

} else {

return true;

}

}

/**

* This method initializes jButton

*

* @return javax.swing.JButton

*/

private JButton getJButton() {

if (jButton == null) {

jButton = new JButton();

jButton.setBounds(81, 275, 110, 54);

jButton.setText("calculate");

jButton.addKeyListener(new java.awt.event.KeyAdapter() {

public void keyPressed(java.awt.event.KeyEvent e) {

if(e.getKeyCode()==10){

if (check(jTextField.getText())

check1(jTextField1.getText())) {

if(!jTextField1.getText().equals("0")){

List b = test(jTextField.getText(), Integer

.parseInt(jTextField1.getText()));

String temp = "";

for (int i = 0; i b.size(); i++ ) {

temp = temp + b.get(i) + "\n";

}

if (b.size() == 0) {

jTextArea.setText("NO RESULT");

} else {

jTextArea.setText(temp);

}

}else{

JOptionPane.showMessageDialog(null, "please enter correct number");

}

}

}

}

});

jButton.addMouseListener(new java.awt.event.MouseAdapter() {

public void mouseClicked(java.awt.event.MouseEvent e) {

if (check(jTextField.getText())

check1(jTextField1.getText())) {

if(!jTextField1.getText().equals("0")){

List b = test(jTextField.getText(), Integer

.parseInt(jTextField1.getText()));

String temp = "";

for (int i = 0; i b.size(); i++ ) {

temp = temp + b.get(i) + "\n";

}

if (b.size() == 0) {

jTextArea.setText("NO RESULT");

} else {

jTextArea.setText(temp);

}

}else{

JOptionPane.showMessageDialog(null, "please enter correct number");

}

}

}

});

}

return jButton;

}

/**

* This method initializes jScrollPane

*

* @return javax.swing.JScrollPane

*/

private JScrollPane getJScrollPane() {

if (jScrollPane == null) {

jScrollPane = new JScrollPane();

jScrollPane.setBounds(267, 238, 216, 124);

jScrollPane.setViewportView(getJTextArea());

}

return jScrollPane;

}

/**

* This method initializes jButton1

*

* @return javax.swing.JButton

*/

private JButton getJButton1() {

if (jButton1 == null) {

jButton1 = new JButton();

jButton1.setBounds(40, 148, 42, 28);

jButton1.setText("1");

jButton1.addMouseListener(new java.awt.event.MouseAdapter() {

public void mouseClicked(java.awt.event.MouseEvent e) {

jTextField.setText(jTextField.getText()+"1");

}

});

}

return jButton1;

}

/**

* This method initializes jButton2

*

* @return javax.swing.JButton

*/

private JButton getJButton2() {

if (jButton2 == null) {

jButton2 = new JButton();

jButton2.setBounds(90, 148, 42, 28);

jButton2.setText("2");

jButton2.addMouseListener(new java.awt.event.MouseAdapter() {

public void mouseClicked(java.awt.event.MouseEvent e) {

jTextField.setText(jTextField.getText()+"2");

}

});

}

return jButton2;

}

/**

* This method initializes jButton3

*

* @return javax.swing.JButton

*/

private JButton getJButton3() {

if (jButton3 == null) {

jButton3 = new JButton();

jButton3.setBounds(140, 148, 42, 28);

jButton3.setText("3");

jButton3.addMouseListener(new java.awt.event.MouseAdapter() {

public void mouseClicked(java.awt.event.MouseEvent e) {

jTextField.setText(jTextField.getText()+"3");

}

});

}

return jButton3;

}

/**

* This method initializes jButton4

*

* @return javax.swing.JButton

*/

private JButton getJButton4() {

if (jButton4 == null) {

jButton4 = new JButton();

jButton4.setBounds(190, 148, 42, 28);

jButton4.setText("4");

jButton4.addMouseListener(new java.awt.event.MouseAdapter() {

public void mouseClicked(java.awt.event.MouseEvent e) {

jTextField.setText(jTextField.getText()+"4");

}

});

}

return jButton4;

}

/**

* This method initializes jButton5

*

* @return javax.swing.JButton

*/

private JButton getJButton5() {

if (jButton5 == null) {

jButton5 = new JButton();

jButton5.setBounds(240, 148, 42, 28);

jButton5.setText("5");

jButton5.addMouseListener(new java.awt.event.MouseAdapter() {

public void mouseClicked(java.awt.event.MouseEvent e) {

jTextField.setText(jTextField.getText()+"5");

}

});

}

return jButton5;

}

/**

* This method initializes jButton6

*

* @return javax.swing.JButton

*/

private JButton getJButton6() {

if (jButton6 == null) {

jButton6 = new JButton();

jButton6.setBounds(40, 188, 42, 28);

jButton6.setText("6");

jButton6.addMouseListener(new java.awt.event.MouseAdapter() {

public void mouseClicked(java.awt.event.MouseEvent e) {

jTextField.setText(jTextField.getText()+"6");

}

});

}

return jButton6;

}

/**

* This method initializes jButton7

*

* @return javax.swing.JButton

*/

private JButton getJButton7() {

if (jButton7 == null) {

jButton7 = new JButton();

jButton7.setBounds(90, 188, 42, 28);

jButton7.setText("7");

jButton7.addMouseListener(new java.awt.event.MouseAdapter() {

public void mouseClicked(java.awt.event.MouseEvent e) {

jTextField.setText(jTextField.getText()+"7");

}

});

}

return jButton7;

}

/**

* This method initializes jButton8

*

* @return javax.swing.JButton

*/

private JButton getJButton8() {

if (jButton8 == null) {

jButton8 = new JButton();

jButton8.setBounds(140, 188, 42, 28);

jButton8.setText("8");

jButton8.addMouseListener(new java.awt.event.MouseAdapter() {

public void mouseClicked(java.awt.event.MouseEvent e) {

jTextField.setText(jTextField.getText()+"8");

}

});

}

return jButton8;

}

/**

* This method initializes jButton9

*

* @return javax.swing.JButton

*/

private JButton getJButton9() {

if (jButton9 == null) {

jButton9 = new JButton();

jButton9.setBounds(190, 188, 42, 28);

jButton9.setText("9");

jButton9.addMouseListener(new java.awt.event.MouseAdapter() {

public void mouseClicked(java.awt.event.MouseEvent e) {

jTextField.setText(jTextField.getText()+"9");

}

});

}

return jButton9;

}

/**

* This method initializes jButton10

*

* @return javax.swing.JButton

*/

private JButton getJButton10() {

if (jButton10 == null) {

jButton10 = new JButton();

jButton10.setBounds(240, 188, 42, 28);

jButton10.setText("0");

jButton10.addMouseListener(new java.awt.event.MouseAdapter() {

public void mouseClicked(java.awt.event.MouseEvent e) {

jTextField.setText(jTextField.getText()+"0");

}

});

}

return jButton10;

}

/**

* Launches this application

*/

public static void main(String[] args) {

calculate24 application = new calculate24();

application.show();

}

} // @jve:decl-index=0:visual-constraint="10,10"

算24点 java代码

C的代码要吗?我对java不是很熟,我试着用java写下吧。给我点时间!

package test.cardgame;

public class BinaryTreeNode

{

private BinaryTreeNode leftSon=null;

private BinaryTreeNode rightSon=null;

private BinaryTreeNode parent=null;

private double data=0;

private int sign=-1;

public int getSign()

{

return sign;

}

public void setSign(int sign)

{

this.sign = sign;

}

public BinaryTreeNode(BinaryTreeNode parent,BinaryTreeNode leftSon,BinaryTreeNode rightSon)

{

this.parent=parent;

this.leftSon=leftSon;

this.rightSon=rightSon;

}

public BinaryTreeNode()

{

}

public BinaryTreeNode getLeftSon()

{

return leftSon;

}

public void setLeftSon(BinaryTreeNode leftSon)

{

this.leftSon = leftSon;

leftSon.setParent(this);

}

public BinaryTreeNode getParent()

{

return parent;

}

public void setParent(BinaryTreeNode parent)

{

this.parent = parent;

}

public BinaryTreeNode getRightSon()

{

return rightSon;

}

public void setRightSon(BinaryTreeNode rightSon)

{

this.rightSon = rightSon;

rightSon.setParent(this);

}

public boolean isLeaf()

{

return (this.leftSon==nullthis.rightSon==null);

}

public boolean isRoot()

{

return this.parent==null;

}

public double getData()

{

return data;

}

public void setData(double data)

{

this.data = data;

}

}

package test.cardgame;

import java.util.ArrayList;

public class CardGame

{

private ArrayListString expressions=new ArrayListString();

public void solute(ArrayListBinaryTreeNode nodes,double target)

{

//whether the root data equals target

if (nodes.size()==1)

{

if (nodes.get(0).getData()==target)

{

String expression=printBinaryTree(nodes.get(0));

addExpression(expression);

return;

}

}

for (int i=0;inodes.size();i++)

{

for (int j=0;jnodes.size();j++)

{

if (i==j)

{

continue;

}

for (int k=0;k4;k++)

{

BinaryTreeNode node=new BinaryTreeNode();

BinaryTreeNode leftSon=nodes.get(i);

BinaryTreeNode rightSon=nodes.get(j);

if (k==0)

{

node.setData(leftSon.getData()+rightSon.getData());

}

else if (k==1)

{

node.setData(leftSon.getData()-rightSon.getData());

}

else if (k==2)

{

node.setData(leftSon.getData()*rightSon.getData());

}

else if (k==3)

{

if (rightSon.getData()==0)

{

continue;

}

node.setData(leftSon.getData()/rightSon.getData());

}

node.setLeftSon(leftSon);

node.setRightSon(rightSon);

node.setSign(k);

ArrayListBinaryTreeNode clonedArrayList=cloneArrayList(nodes);

//remove nodes from the tree

clonedArrayList.remove(leftSon);

clonedArrayList.remove(rightSon);

clonedArrayList.add(node);

solute(clonedArrayList,target);

}

}

}

}

public void printResult()

{

for (int i=0;iexpressions.size();i++)

{

System.out.println("Solution "+i+": "+expressions.get(i));

}

}

private void addExpression(String expression)

{

if (expressions.contains(expression))

{

return;

}

expressions.add(expression);

}

private ArrayListBinaryTreeNode cloneArrayList(ArrayListBinaryTreeNode source)

{

ArrayListBinaryTreeNode result=new ArrayListBinaryTreeNode();

for (int i=0;isource.size();i++)

{

result.add(source.get(i));

}

return result;

}

private String printBinaryTree(BinaryTreeNode resultRoot)

{

if (resultRoot.isLeaf())

{

return doubleToString(resultRoot.getData());

}

else

{

String expression="(";

expression+=printBinaryTree(resultRoot.getLeftSon());

int sign=resultRoot.getSign();

if (sign==0)

{

expression+="+";

}

else if (sign==1)

{

expression+="-";

}

else if (sign==2)

{

expression+="*";

}

else if (sign==3)

{

expression+="/";

}

expression+=printBinaryTree(resultRoot.getRightSon());

expression+=")";

return expression;

}

}

private String doubleToString(double value)

{

int intValue=(int)value;

if (value==intValue)

{

return String.valueOf(intValue);

}

else

{

return String.valueOf(value);

}

}

public BinaryTreeNode buildBinaryTreeNode(double value)

{

BinaryTreeNode node=new BinaryTreeNode();

node.setData(value);

return node;

}

public static void main(String[] args)

{

CardGame cardGame=new CardGame();

ArrayListBinaryTreeNode nodes=new ArrayListBinaryTreeNode();

nodes.add(cardGame.buildBinaryTreeNode(4));

nodes.add(cardGame.buildBinaryTreeNode(6));

nodes.add(cardGame.buildBinaryTreeNode(1));

nodes.add(cardGame.buildBinaryTreeNode(1));

cardGame.solute(nodes, 24);

cardGame.printResult();

}

}


新闻标题:24点java代码的简单介绍
网站网址:http://lszwz.com/article/higiee.html

其他资讯

售后响应及时

7×24小时客服热线

数据备份

更安全、更高效、更稳定

价格公道精准

项目经理精准报价不弄虚作假

合作无风险

重合同讲信誉,无效全额退款