写一个运行在cmd环境下的Java游戏

24点。。。。

导入Java . util . ArrayList;

导入Java . util . list;

导入Java . util . regex . matcher;

导入Java . util . regex . pattern;

导入javax . swing . image icon;

导入javax . swing . jbutton;

导入javax . swing . jframe;

导入javax . swing . jlabel;

导入javax . swing . joptionpane;

导入javax . swing . jscrollpane;

导入javax . swing . jtextarea;

导入javax . swing . jtextfield;

公共类calculate24扩展JFrame{

private javax . swing . jpanel jContentPane = null;

private JLabel jLabel = null

private JLabel JLabel 1 = null;

private JTextField JTextField = null;

private JTextField JTextField 1 = null;

private JTextArea JTextArea = null;

private JLabel jLabel2 = null

private JButton jButton = null

private JScrollPane JScrollPane = null;

private JButton JButton 1 = 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 JButton 10 = null;

/**

*这是默认的构造函数

*/

公共计算24() {

super();

initialize();

}

/**

*此方法初始化此

*

* @返回void

*/

私有void initialize() {

this . setdefaultcloseoperation(javax . swing . jframe . exit _ ON _ CLOSE);

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

this . setcontentpane(getJContentPane());

This.setTitle("24点");

}

/**

*此方法初始化jContentPane

*

* @return javax.swing.JPanel

*/

private javax . swing . jpanel getJContentPane(){

if (jContentPane == null) {

JLabel 2 = new JLabel();

JLabel 1 = new JLabel();

jLabel = new jLabel();

jContentPane = new javax . swing . jpanel();

jcontentpane . set layout(null);

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

jLabel.setText("请输入四个数字");

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

jlabel 1 . settext(" please unter你想得到多少结果");

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

jlabel 2 . settext(" result ");

jContentPane.add(getJButton(),null);

jContentPane.add(jLabel,null);

jContentPane.add(jLabel1,null);

jContentPane.add(getJTextField(),null);

jcontentpane . add(getjtextfield 1(),null);

jContentPane.add(jLabel2,null);

jcontentpane . add(getJScrollPane(),null);

jcontentpane . add(getjbutton 1(),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(getjbutton 10(),null);

}

返回jContentPane

}

/**

*此方法初始化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 . focus adapter(){

public void focus gain(Java . awt . event . focus event e){

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

}

});

}

返回jTextField

}

/**

*此方法初始化jTextField1

*

* @return javax.swing.JTextField

*/

private JTextField get JTextField 1(){

if (jTextField1 == null) {

JTextField 1 = new JTextField();

jtextfield 1 . set bounds(293,81,161,41);

jtextfield 1 . setnextfocusablecomponent(jButton);

}

返回jtextfield 1;

}

/**

*此方法初始化jTextArea

*

* @return javax.swing.JTextArea

*/

private JTextArea getJTextArea(){

if (jTextArea == null) {

jTextArea = new jTextArea();

jtextarea . settabsize(8);

}

返回jTextArea

}

公共静态字符串bbb(List list1,List list2) {

浮点结果= 0;

for(int I = list 1 . size();我& gt0;我- ) {

if(list 1 . contains(" * "){

int j = list 1 . index of(" * ");

result = float . parse float((String)list 2 . get(j))

* float . parse float((String)list 2 . get(j+1));

list 1 . remove(j);

list 2 . remove(j);

list 2 . remove(j);

list2.add(j,string . value of(result));

} else if(list 1 . contains("/"){

int j = list 1 . index of("/");

result = float . parse float((String)list 2 . get(j))

/float . parse float((String)list 2 . get(j+1));

list 1 . remove(j);

list 2 . remove(j);

list 2 . remove(j);

list2.add(j,string . value of(result));

} else if(list 1 . contains("+"){

int j = list 1 . index of("+");

result = float . parse float((String)list 2 . get(j))

+float . parse float((String)list 2 . get(j+1));

list 1 . remove(j);

list 2 . remove(j);

list 2 . remove(j);

list2.add(j,string . value of(result));

} else if(list 1 . contains("-"){

int j = list 1 . index of("-");

result = float . parse float((String)list 2 . get(j))

-float . parse float((String)list 2 . get(j+1));

list 1 . remove(j);

list 2 . remove(j);

list 2 . remove(j);

list2.add(j,string . value of(result));

}

}

return(String)list 2 . get(0);

}

私有静态void bbb(String str,String sPrint,List list) {

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

for(int I = 0;我& ltstr . length()& amp;& amp( !"".equals(str . trim());i++)

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

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

列表);

}

私有静态列表bbb(字符串str,列表list) {

list result = new ArrayList();

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

string b 1 = str . substring(1,2);

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

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

String[]a 11 = new String[]{ a 1,b1,c1,d 1 };

for(int I = 0;我& ltlist . size();i++ ) {

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

int a = integer . parse int(temp . substring(0,1));

int b = integer . parse int(temp . substring(1,2));

int c = integer . parse int(temp . substring(2,3));

int d = integer . parse int(temp . substring(3,4));

string tempStr = a 11[a]+a 11[b]+a 11[c]+a 11[d];

如果(!result.contains(临时字符串)){

result . add(tempStr);

}

}

返回结果;

}

公共列表测试(String param,int x) {

int y = 0;

list result = new ArrayList();

list a 11 = new ArrayList();

calculate24.bbb("0123 ","",a 11);

list a 1 = calculate 24 . BBB(param,a 11);

for(int m = 0;m & lta 1 . size();m++ ) {

String param 1 =(String)a 1 . get(m);

int[]a = new int[]{ integer . parse int(param 1 . substring(0,1)),

integer . parse int(param 1 . substring(1,2)),

integer . parse int(param 1 . substring(2,3)),

integer . parse int(param 1 . substring(3,4))};

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

for(int I = 0;我& lt4;i++ ) {

for(int j = 0;j & lt4;j++ ) {

for(int k = 0;k & lt4;k++ ) {

list aa = new ArrayList();

aa . add(string . value of(a[0]));

aa . add(string . value of(a[1]));

aa . add(string . value of(a[2]));

aa . add(string . value of(a[3]));

list bb = new ArrayList();

bb . add(e[I]);

bb . add(e[j]);

bb . add(e[k]);

字符串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) {

返回结果;

}

}

list temp 1 = new ArrayList();

list temp 2 = new ArrayList();

temp 1 . add(string . value of(a[0]));

temp 1 . add(string . value of(a[1]));

temp 2 . add(e[I]);

string temp = calculate 24 . BBB(temp 2,temp 1);

aa . clear();

aa . add(temp);

aa . add(string . value of(a[2]));

aa . add(string . value of(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) {

返回结果;

}

}

temp 1 . clear();

temp 2 . clear();

temp 1 . add(string . value of(a[1]));

temp 1 . add(string . value of(a[2]));

temp 2 . add(e[j]);

temp = calculate24.bbb(temp2,temp 1);

aa . clear();

aa . add(string . value of(a[0]));

aa . add(temp);

aa . add(string . value of(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) {

返回结果;

}

}

temp 1 . clear();

temp 2 . clear();

temp 1 . add(string . value of(a[2]));

temp 1 . add(string . value of(a[3]));

temp 2 . add(e[k]);

temp = calculate24.bbb(temp2,temp 1);

aa . clear();

aa . add(string . value of(a[0]));

aa . add(string . value of(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) {

返回结果;

}

}

temp 1 . clear();

temp 2 . clear();

temp 1 . add(string . value of(a[0]));

temp 1 . add(string . value of(a[1]));

temp 1 . add(string . value of(a[2]));

temp 2 . add(e[I]);

temp 2 . add(e[j]);

temp = calculate24.bbb(temp2,temp 1);

aa . clear();

aa . add(temp);

aa . add(string . value of(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) {

返回结果;

}

}

temp 1 . clear();

temp 2 . clear();

temp 1 . add(string . value of(a[1]));

temp 1 . add(string . value of(a[2]));

temp 1 . add(string . value of(a[3]));

temp 2 . add(e[j]);

temp 2 . add(e[k]);

temp = calculate24.bbb(temp2,temp 1);

aa . clear();

aa . add(string . value of(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) {

返回结果;

}

}

temp 1 . clear();

temp 2 . clear();

temp 1 . add(string . value of(a[0]));

temp 1 . add(string . value of(a[1]));

temp 2 . add(e[I]);

temp = calculate24.bbb(temp2,temp 1);

list temp 3 = new ArrayList();

list temp 4 = new ArrayList();

temp 3 . add(string . value of(a[2]));

temp 3 . add(string . value of(a[3]));

temp 4 . add(e[k]);

string temp 11 = calculate 24 . BBB(temp 4,temp 3);

aa . clear();

aa . add(temp);

aa . add(temp 11);

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) {

返回结果;

}

}

}

}

}

}

返回结果;

}

公共静态布尔检查(String param1) {

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

matcher matcher = pattern . matcher((char sequence)param 1);

布尔结果= matcher . matches();

if (result == false) {

joptionpane . showmessagedialog(null,“请输入正确的数字”);

返回false

}否则{

返回true

}

}

公共静态布尔校验1(String param2) {

if(param2 == null){

joptionpane . showmessagedialog(null,“请输入正确的数字”);

返回false

}

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

matcher matcher = pattern . matcher((char sequence)param 2);

布尔结果= matcher . matches();

if (result == false) {

joptionpane . showmessagedialog(null,“请输入正确的数字”);

返回false

}否则{

返回true

}

}

/**

*此方法初始化jButton

*

* @return javax.swing.JButton

*/

私有JButton getJButton() {

if (jButton == null) {

jButton = new jButton();

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

jbutton . settext(" calculate ");

jbutton . addkey listener(new Java . awt . event . key adapter(){

public void key pressed(Java . awt . event . key event e){

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

if (check(jTextField.getText())

& amp& ampcheck 1(jtextfield 1 . gettext()){

如果(!jTextField1.getText()。等于(" 0 "){

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

。parse int(jtextfield 1 . gettext());

String temp =

for(int I = 0;我& ltb . size();i++ ) {

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

}

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

jTextArea.setText("无结果");

}否则{

jtextarea . settext(temp);

}

}否则{

joptionpane . showmessagedialog(null,“请输入正确的数字”);

}

}

}

}

});

jbutton . addmouse listener(new Java . awt . event . mouse adapter(){

public void mouse clicked(Java . awt . event . mouse event e){

if (check(jTextField.getText())

& amp& ampcheck 1(jtextfield 1 . gettext()){

如果(!jTextField1.getText()。等于(" 0 "){

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

。parse int(jtextfield 1 . gettext());

String temp =

for(int I = 0;我& ltb . size();i++ ) {

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

}

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

jTextArea.setText("无结果");

}否则{

jtextarea . settext(temp);

}

}否则{

joptionpane . showmessagedialog(null,“请输入正确的数字”);

}

}

}

});

}

返回jButton

}

/**

*此方法初始化jScrollPane

*

* @ return javax . swing . jscrollpane

*/

private JScrollPane getJScrollPane(){

if (jScrollPane == null) {

jScrollPane = new jScrollPane();

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

jscrollpane . setviewportview(getJTextArea());

}

返回jScrollPane

}

/**

*此方法初始化jButton1

*

* @return javax.swing.JButton

*/

私有JButton getJButton1() {

if (jButton1 == null) {

JButton 1 = new JButton();

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

jbutton 1 . settext(" 1 ");

jbutton 1 . addmouse listener(new Java . awt . event . mouse adapter(){

public void mouse clicked(Java . awt . event . mouse event e){

jtextfield . settext(jtextfield . gettext()+" 1));

}

});

}

返回jbutton 1;

}

/**

*此方法初始化jButton2

*

* @return javax.swing.JButton

*/

私有JButton getJButton2() {

if (jButton2 == null) {

JButton 2 = new JButton();

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

jbutton 2 . settext(" 2 ");

jbutton 2 . addmouse listener(new Java . awt . event . mouse adapter(){

public void mouse clicked(Java . awt . event . mouse event e){

jtextfield . settext(jtextfield . gettext()+" 2 ");

}

});

}

返回jButton2

}

/**

*该方法初始化jButton3

*

* @return javax.swing.JButton

*/

私有JButton getJButton3() {

if (jButton3 == null) {

JButton 3 = new JButton();

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

jbutton 3 . settext(" 3 ");

jbutton 3 . addmouse listener(new Java . awt . event . mouse adapter(){

public void mouse clicked(Java . awt . event . mouse event e){

jtextfield . settext(jtextfield . gettext()+" 3));

}

});

}

返回jButton3

}

/**

*此方法初始化jButton4

*

* @return javax.swing.JButton

*/

私有JButton getJButton4() {

if (jButton4 == null) {

JButton 4 = new JButton();

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

jbutton 4 . settext(" 4 ");

jbutton 4 . addmouse listener(new Java . awt . event . mouse adapter(){

public void mouse clicked(Java . awt . event . mouse event e){

jtextfield . settext(jtextfield . gettext()+" 4));

}

});

}

返回jButton4

}

/**

*该方法初始化jButton5

*

* @return javax.swing.JButton

*/

私有JButton getJButton5() {

if (jButton5 == null) {

JButton 5 = new JButton();

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

jbutton 5 . settext(" 5 ");

jbutton 5 . addmouse listener(new Java . awt . event . mouse adapter(){

public void mouse clicked(Java . awt . event . mouse event e){

jtextfield . settext(jtextfield . gettext()+" 5 ");

}

});

}

返回jButton5

}

/**

*该方法初始化jButton6

*

* @return javax.swing.JButton

*/

私有JButton getJButton6() {

if (jButton6 == null) {

JButton 6 = new JButton();

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

jbutton 6 . settext(" 6 ");

jbutton 6 . addmouse listener(new Java . awt . event . mouse adapter(){

public void mouse clicked(Java . awt . event . mouse event e){

jtextfield . settext(jtextfield . gettext()+" 6 ");

}

});

}

返回jButton6

}

/**

*该方法初始化jButton7

*

* @return javax.swing.JButton

*/

私有JButton getJButton7() {

if (jButton7 == null) {

JButton 7 = new JButton();

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

jbutton 7 . settext(" 7 ");

jbutton 7 . addmouse listener(new Java . awt . event . mouse adapter(){

public void mouse clicked(Java . awt . event . mouse event e){

jtextfield . settext(jtextfield . gettext()+" 7 ");

}

});

}

返回jButton7

}

/**

*该方法初始化jButton8

*

* @return javax.swing.JButton

*/

私有JButton getJButton8() {

if (jButton8 == null) {

JButton 8 = new JButton();

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

jbutton 8 . settext(" 8 ");

jbutton 8 . addmouse listener(new Java . awt . event . mouse adapter(){

public void mouse clicked(Java . awt . event . mouse event e){

jtextfield . settext(jtextfield . gettext()+" 8 ");

}

});

}

返回jButton8

}

/**

*该方法初始化jButton9

*

* @return javax.swing.JButton

*/

私有JButton getJButton9() {

if (jButton9 == null) {

JButton 9 = new JButton();

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

jbutton 9 . settext(" 9 ");

jbutton 9 . addmouse listener(new Java . awt . event . mouse adapter(){

public void mouse clicked(Java . awt . event . mouse event e){

jtextfield . settext(jtextfield . gettext()+" 9 ");

}

});

}

返回jButton9

}

/**

*此方法初始化jButton10

*

* @return javax.swing.JButton

*/

private JButton getjbutton 10(){

if (jButton10 == null) {

JButton 10 = new JButton();

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

jbutton 10 . settext(" 0 ");

jbutton 10 . addmouse listener(new Java . awt . event . mouse adapter(){

public void mouse clicked(Java . awt . event . mouse event e){

jtextfield . settext(jtextfield . gettext()+" 0 ");

}

});

}

返回jbutton 10;

}

/**

*启动此应用程序

*/

公共静态void main(String[] args) {

calculate 24 application = new calculate 24();

application.show()。

}

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