導航:首頁 > 編程語言 > java隨便4個數求結果24

java隨便4個數求結果24

發布時間:2023-05-19 01:19:42

① 加減乘除24 的java 程序

告訴你個思路吧,就是排列組合的時候不是針對數字而是針對符號,對象是占的位子,因為有四個數字,所以有3個位子進行符號的放置,然後循環臘銀匹配,最壞的結果是需要花64次才能得到得到的答案應該是和24或-24進行匹配,之後的就是灶局橡輸出的程序了,你可以做個子程序來進行判斷數的組合方式,相信不會難隱旁到你的.

② 急求 JAVA的 算24點的代碼!進來看!有特別要求!

package ceshi;

/** 給定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) {
Core s = new Core();
s.setNumbers(new int[] { 3, 4, 8, 6 });
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 (n>0) {
// 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.array(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點的游戲的隨機數字問題

public void randFour(){
int[] a=new int[4];
for(int i=0;i<a.length;i++){
a[i]=(int)( Math.random()*20+1);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("第"液春棗+(i+1)+"個數森老:"鬧拆+a[i]);
}
}

④ java算24點代碼:輸入4個數算24點,能夠在命令提示符下就可以運行。100多

java算24點代碼,輸入4個數,利用for把4個數加起來,比較是不是等於24即可

⑤ 求 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"

⑥ java算24點代碼:輸入4個數算24點,能夠在命令提示符下就可以運行。100多

import java.util.Scanner;

/** 給定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 (n>0) {
// 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.array(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;
}
}
}

⑦ 隨便四個數字,計算24點有什麼技巧

隨便四個數字,計算24點技巧如下:

1、利用3×8=24、4×6=24、12×2=24求解.

把牌面上的四個數想辦法湊成3和8、4和6,再相乘求解.如3、3、6、10可組成(10-6÷3)×3=24等.又如2、3、3、7可組成(7+3-2)×3=24等.實踐證明,這種方法是利用率最大、命中率最高的一種方法.

2、利用0、11的運算特性求解.

如3、4、4、8可組成3×8+4-4=24等.又如4、5、J、K可組成11×(5-4)+13=24等

3、在有解的牌組中,用得最為廣泛的是以下六種解法:空枝(我們用a、b、c、d表示牌面上的四個數)

3.1、(a-b)×(c+d) 如(10-4)×(2+2)=24等;

3.2、(a+b)÷c×d 如(10+2)÷2×4=24等;

3.3、(a-b÷c)×d 如(3-2÷2)×12=24等;

3.4、(a+b-c)×d
如(9+5—2)×2=24等;

3.5、a×b+c—d 如11×3+l—10=24等;

3.6、(a-b)×c+d 如(4-l)×6+6=24等。

游戲時,不妨按照上述方法試一試.

(7)java隨便4個數求結果24擴展閱讀:

棋牌類益智游戲,要求四個數字運算結果等於二十四,一起來玩玩吧!這個游戲用撲克牌更容易來開展。拿一副牌,抽去大小王後(初練也可以把J/Q/K/大小王也拿去),剩下1~10這40張牌(以下用1代替A)。

任意抽取4張牌(稱為牌組),備滲用加、減、乘、除(可加括弧,高級玩家也可用乘方開方與階乘運算)把牌面上的數算成24。每張牌必須用且只能用一次。如抽出的牌是3、8、8、9,那麼算式為(9-8)×8×3=24。

方法如下:

窮舉的可行性問題。把表達式如下分成三類:

1、 無括弧的簡單表達式。

2、 有一個括弧的簡單表達式斗滾敏。

3、 有兩個括弧的較復雜表達式。

例如:(6-3)*10-6=24。

⑧ 用java編程 由用戶給四個數,判斷能否湊出24

importjava.util.LinkedList;
importjava.util.Scanner;

publicclassKnow24{
publicstaticvoidmain(String[]山頃隱args){
Scannersc=newScanner(System.in);
System.out.println("輸入四個數");
doublex1=sc.nextDouble();
doublex2=sc.nextDouble();
doublex3=sc.nextDouble();
doublex4=sc.nextDouble();
f24(x1,x2,x3,x4);

}
publicstaticvoidf24(doublex1,doublex2,doublex3,doublex4){
LinkedList<Double>L=newLinkedList<Double>();
L.add(x1);
L.add(x2);
L.add(x3);
L.add(x4);
double[]a=newdouble[4];
LinkedList<Double>L1逗廳=newLinkedList<Double>();
LinkedList<Double>L2=newLinkedList<Double>();
LinkedList<Double>L3=newLinkedList<Double>();
booleanflag=true;
String[]s={"+","-","*","/"};
for(inti=0;i<4;++i){
a[0]=L.get(i);
L1.clear();
L1.addAll(L);
L.remove(i);
for(intj=0;j<3;++j){
a[1]=L.get(j);
L2.clear();
L2.addAll(L);
L.remove(j);
for(intk=0;k<2;++k){
a[2]=L.get(k);
L3.clear();
L3.addAll(L);
L.remove(k);
for(intm=0;m<1;++m){
a[3]=L.get(m);
for(intn1=0;n1<4;n1++)
for(intn2=0;n2<4;++n2)
for(intn3=0;n3<4;++n3){
doublex=0,y=0,z=0;
switch(n1){
case0:
x=a[0]+a[1];
break;
case1:
x乎譽=a[0]-a[1];
break;
case2:
x=a[0]*a[1];
break;
case3:
try{
x=a[0]/a[1];}catch(Exceptione){
System.out.println("除數為0");
}
break;
}
switch(n2){
case0:
y=x+a[2];
break;
case1:
y=x-a[2];
break;
case2:
y=x*a[2];
break;
case3:
try{
y=x/a[2];}catch(Exceptione){
System.out.println("除數為0");
}
break;
}
switch(n3){
case0:
z=y+a[3];
break;
case1:
z=y-a[3];
break;
case2:
z=y*a[3];
break;
case3:
try{
z=y/a[3];}catch(Exceptione){
System.out.println("除數為0");
}
break;
}
if(z==24d){
System.out.println(a[0]+s[n1]+a[1]+s[n2]+a[2]+s[n3]+a[3]+"="+"24");
flag=false;
}
}

}
L.clear();
L.addAll(L3);
}
L.clear();
L.addAll(L2);
}
L.clear();
L.addAll(L1);
}
if(flag)
System.out.println("湊不出來");

}
}

⑨ 24點速算游戲 Java 代碼

這是一個遞歸的思想吧。大致是這樣的(應該行,在琢磨琢磨下)
f(x1,x2,x3,x4)=24 <==========> f(x1,x2,x3)=f1(24,x4)
f(x1,x2) <======> f1(f1(24,x4),x3)
x1 <========> f1( f1(f1(24,x4),x3),x2)
理論上來說,用遞歸要容易理專解些,寫代碼的話屬,你動動手吧。

⑩ 一個java面試題:計算24點游戲 編程

import java.util.Random;

public class test2
{
public static void main(String[] args)
{
Random random = new Random();
int a[] = new int[4];
for(int i=0; i<4; i++)
{
a[i] = random.nextInt(13)+1;
}
for(int j=0; j<4; j++)
{
System.out.println("第" + j +"個數:" + a[j]);
}
Calcula(a);

}

public static void Calcula(int[] a)
{
int add, sub, multi, div;
add = 0;
sub = 0;
multi = 0;
div = 0;
for(int i=0; i<4; i++)
{
add = add + a[i];
sub = sub - a[i];
multi = multi * a[i];
div = div/a[i];
}
if(add==24)
{
System.out.println(a[0] + "+" + a[1] + "+" + a[2] + "+" + a[3]
+ "=" + add);

}
else if(sub==24)
{
System.out.println(a[0] + "-" + a[1] + "-"茄埋旁 + a[2] + "-" + a[3]
+ "=" + sub);
}
else if(multi==24)
{
System.out.println(a[0] + "*"液返 + a[1] + "*" + a[2] + "*"顫橡 + a[3]
+ "=" + multi);
}
else if(div==24)
{
System.out.println(a[0] + "÷" + a[1] + "÷" + a[2] + "÷" + a[3]
+ "=" + div);
}
else
{
System.out.println("對不起,沒有實現24點的數");
}
}
}

已編譯通過~

閱讀全文

與java隨便4個數求結果24相關的資料

熱點內容
炫酷字體APP下載的文件在哪裡 瀏覽:668
廊坊哪裡有少兒編程機構 瀏覽:312
cad新文件能找回來嗎 瀏覽:951
導出手機qq文件到u盤 瀏覽:456
電腦如何打開ppt文件怎麼打開方式 瀏覽:782
魅族鎖定區文件夾 瀏覽:357
刻字cnc怎麼編程 瀏覽:182
學校的網路拓撲結構圖 瀏覽:784
收集100個pdf文件里關鍵詞 瀏覽:594
蘋果關閉4g網路設置 瀏覽:289
如何監測資料庫 瀏覽:967
拷貝過來的pdf文件 瀏覽:751
抖音小店的訪客數據怎麼看 瀏覽:670
怎麼把c語言編程的字元向下移動 瀏覽:786
sql刪除文件組代碼 瀏覽:978
安卓post請求多重json 瀏覽:776
微信消除數據怎麼恢復 瀏覽:918
小米刷機顯示系統找不到指定文件 瀏覽:528
蘋果手機小風扇圖app叫什麼 瀏覽:292
繁體中文輸入工具 瀏覽:916

友情鏈接