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"
2. 用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]);
}
}
3. 求java rpg小游戏源代码 最好是文字rpg 不需要很复杂 只是交作业用
连连看的小源码
package Lianliankan;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class lianliankan implements ActionListener
{
JFrame mainFrame; //主面板
Container thisContainer;
JPanel centerPanel,southPanel,northPanel; //子面板
JButton diamondsButton[][] = new JButton[6][5];//游戏按钮数组
JButton exitButton,resetButton,newlyButton; //退出,重列,重新开始按钮
JLabel fractionLable=new JLabel("0"); //分数标签
JButton firstButton,secondButton; //分别记录两次被选中的按钮
int grid[][] = new int[8][7];//储存游戏按钮位置
static boolean pressInformation=false; //判断是否有按钮被选中
int x0=0,y0=0,x=0,y=0,fristMsg=0,secondMsg=0,validateLV; //游戏按钮的位置坐标
int i,j,k,n;//消除方法控制
public void init(){
mainFrame=new JFrame("JKJ连连看");
thisContainer = mainFrame.getContentPane();
thisContainer.setLayout(new BorderLayout());
centerPanel=new JPanel();
southPanel=new JPanel();
northPanel=new JPanel();
thisContainer.add(centerPanel,"Center");
thisContainer.add(southPanel,"South");
thisContainer.add(northPanel,"North");
centerPanel.setLayout(new GridLayout(6,5));
for(int cols = 0;cols < 6;cols++){
for(int rows = 0;rows < 5;rows++ ){
diamondsButton[cols][rows]=new JButton(String.valueOf(grid[cols+1][rows+1]));
diamondsButton[cols][rows].addActionListener(this);
centerPanel.add(diamondsButton[cols][rows]);
}
}
exitButton=new JButton("退出");
exitButton.addActionListener(this);
resetButton=new JButton("重列");
resetButton.addActionListener(this);
newlyButton=new JButton("再来一局");
newlyButton.addActionListener(this);
southPanel.add(exitButton);
southPanel.add(resetButton);
southPanel.add(newlyButton);
fractionLable.setText(String.valueOf(Integer.parseInt(fractionLable.getText())));
northPanel.add(fractionLable);
mainFrame.setBounds(280,100,500,450);
mainFrame.setVisible(true);
}
public void randomBuild() {
int randoms,cols,rows;
for(int twins=1;twins<=15;twins++) {
randoms=(int)(Math.random()*25+1);
for(int alike=1;alike<=2;alike++) {
cols=(int)(Math.random()*6+1);
rows=(int)(Math.random()*5+1);
while(grid[cols][rows]!=0) {
cols=(int)(Math.random()*6+1);
rows=(int)(Math.random()*5+1);
}
this.grid[cols][rows]=randoms;
}
}
}
public void fraction(){
fractionLable.setText(String.valueOf(Integer.parseInt(fractionLable.getText())+100));
}
public void reload() {
int save[] = new int[30];
int n=0,cols,rows;
int grid[][]= new int[8][7];
for(int i=0;i<=6;i++) {
for(int j=0;j<=5;j++) {
if(this.grid[i][j]!=0) {
save[n]=this.grid[i][j];
n++;
}
}
}
n=n-1;
this.grid=grid;
while(n>=0) {
cols=(int)(Math.random()*6+1);
rows=(int)(Math.random()*5+1);
while(grid[cols][rows]!=0) {
cols=(int)(Math.random()*6+1);
rows=(int)(Math.random()*5+1);
}
this.grid[cols][rows]=save[n];
n--;
}
mainFrame.setVisible(false);
pressInformation=false; //这里一定要将按钮点击信息归为初始
init();
for(int i = 0;i < 6;i++){
for(int j = 0;j < 5;j++ ){
if(grid[i+1][j+1]==0)
diamondsButton[i][j].setVisible(false);
}
}
}
public void estimateEven(int placeX,int placeY,JButton bz) {
if(pressInformation==false) {
x=placeX;
y=placeY;
secondMsg=grid[x][y];
secondButton=bz;
pressInformation=true;
}
else {
x0=x;
y0=y;
fristMsg=secondMsg;
firstButton=secondButton;
x=placeX;
y=placeY;
secondMsg=grid[x][y];
secondButton=bz;
if(fristMsg==secondMsg && secondButton!=firstButton){
xiao();
}
}
}
public void xiao() { //相同的情况下能不能消去。仔细分析,不一条条注释
if((x0==x &&(y0==y+1||y0==y-1)) || ((x0==x+1||x0==x-1)&&(y0==y))){ //判断是否相邻
remove();
}
else{
for (j=0;j<7;j++ ) {
if (grid[x0][j]==0){ //判断第一个按钮同行哪个按钮为空
if (y>j) { //如果第二个按钮的Y坐标大于空按钮的Y坐标说明第一按钮在第二按钮左边
for (i=y-1;i>=j;i-- ){ //判断第二按钮左侧直到第一按钮中间有没有按钮
if (grid[x][i]!=0) {
k=0;
break;
}
else{ k=1; } //K=1说明通过了第一次验证
}
if (k==1) {
linePassOne();
}
}
if (y<j){ //如果第二个按钮的Y坐标小于空按钮的Y坐标说明第一按钮在第二按钮右边
for (i=y+1;i<=j ;i++ ){ //判断第二按钮左侧直到第一按钮中间有没有按钮
if (grid[x][i]!=0){
k=0;
break;
}
else { k=1; }
}
if (k==1){
linePassOne();
}
}
if (y==j ) {
linePassOne();
}
}
if (k==2) {
if (x0==x) {
remove();
}
if (x0<x) {
for (n=x0;n<=x-1;n++ ) {
if (grid[n][j]!=0) {
k=0;
break;
}
if(grid[n][j]==0 && n==x-1) {
remove();
}
}
}
if (x0>x) {
for (n=x0;n>=x+1 ;n-- ) {
if (grid[n][j]!=0) {
k=0;
break;
}
if(grid[n][j]==0 && n==x+1) {
remove();
}
}
}
}
}
for (i=0;i<8;i++ ) { //列
if (grid[i][y0]==0) {
if (x>i) {
for (j=x-1;j>=i ;j-- ) {
if (grid[j][y]!=0) {
k=0;
break;
}
else { k=1; }
}
if (k==1) {
rowPassOne();
}
}
if (x<i) {
for (j=x+1;j<=i;j++ ) {
if (grid[j][y]!=0) {
k=0;
break;
}
else { k=1; }
}
if (k==1) {
rowPassOne();
}
}
if (x==i) {
rowPassOne();
}
}
if (k==2){
if (y0==y) {
remove();
}
if (y0<y) {
for (n=y0;n<=y-1 ;n++ ) {
if (grid[i][n]!=0) {
k=0;
break;
}
if(grid[i][n]==0 && n==y-1) {
remove();
}
}
}
if (y0>y) {
for (n=y0;n>=y+1 ;n--) {
if (grid[i][n]!=0) {
k=0;
break;
}
if(grid[i][n]==0 && n==y+1) {
remove();
}
}
}
}
}
}
}
public void linePassOne(){
if (y0>j){ //第一按钮同行空按钮在左边
for (i=y0-1;i>=j ;i-- ){ //判断第一按钮同左侧空按钮之间有没按钮
if (grid[x0][i]!=0) {
k=0;
break;
}
else { k=2; } //K=2说明通过了第二次验证
}
}
if (y0<j){ //第一按钮同行空按钮在与第二按钮之间
for (i=y0+1;i<=j ;i++){
if (grid[x0][i]!=0) {
k=0;
break;
}
else{ k=2; }
}
}
}
public void rowPassOne(){
if (x0>i) {
for (j=x0-1;j>=i ;j-- ) {
if (grid[j][y0]!=0) {
k=0;
break;
}
else { k=2; }
}
}
if (x0<i) {
for (j=x0+1;j<=i ;j++ ) {
if (grid[j][y0]!=0) {
k=0;
break;
}
else { k=2; }
}
}
}
public void remove(){
firstButton.setVisible(false);
secondButton.setVisible(false);
fraction();
pressInformation=false;
k=0;
grid[x0][y0]=0;
grid[x][y]=0;
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==newlyButton){
int grid[][] = new int[8][7];
this.grid = grid;
randomBuild();
mainFrame.setVisible(false);
pressInformation=false;
init();
}
if(e.getSource()==exitButton)
System.exit(0);
if(e.getSource()==resetButton)
reload();
for(int cols = 0;cols < 6;cols++){
for(int rows = 0;rows < 5;rows++ ){
if(e.getSource()==diamondsButton[cols][rows])
estimateEven(cols+1,rows+1,diamondsButton[cols][rows]);
}
}
}
public static void main(String[] args) {
lianliankan llk = new lianliankan();
llk.randomBuild();
llk.init();
}
}
//old 998 lines
//new 318 lines
4. 急需基于eclipse的JAVA小游戏源代码!!!
单人版五子棋,不用导入,直接新建一个mywindow类就行,然后把一下代码粘贴就Ok了。或者,直接用dos就可以了。。
---------------------
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class mypanel extends Panel implements MouseListener
{
int chess[][] = new int[11][11];
boolean Is_Black_True;
mypanel()
{
Is_Black_True = true;
for(int i = 0;i < 11;i++)
{
for(int j = 0;j < 11;j++)
{
chess[i][j] = 0;
}
}
addMouseListener(this);
setBackground(Color.BLUE);
setBounds(0, 0, 360, 360);
setVisible(true);
}
public void mousePressed(MouseEvent e)
{
int x = e.getX();
int y = e.getY();
if(x < 25 || x > 330 + 25 ||y < 25 || y > 330+25)
{
return;
}
if(chess[x/30-1][y/30-1] != 0)
{
return;
}
if(Is_Black_True == true)
{
chess[x/30-1][y/30-1] = 1;
Is_Black_True = false;
repaint();
Justisewiner();
return;
}
if(Is_Black_True == false)
{
chess[x/30-1][y/30-1] = 2;
Is_Black_True = true;
repaint();
Justisewiner();
return;
}
}
void Drawline(Graphics g)
{
for(int i = 30;i <= 330;i += 30)
{
for(int j = 30;j <= 330; j+= 30)
{
g.setColor(Color.WHITE);
g.drawLine(i, j, i, 330);
}
}
for(int j = 30;j <= 330;j += 30)
{
g.setColor(Color.WHITE);
g.drawLine(30, j, 330, j);
}
}
void Drawchess(Graphics g)
{
for(int i = 0;i < 11;i++)
{
for(int j = 0;j < 11;j++)
{
if(chess[i][j] == 1)
{
g.setColor(Color.BLACK);
g.fillOval((i + 1) * 30 - 8, (j + 1) * 30 - 8, 16, 16);
}
if(chess[i][j] == 2)
{
g.setColor(Color.WHITE);
g.fillOval((i + 1) * 30 - 8, (j + 1) * 30 - 8, 16, 16);
}
}
}
}
void Justisewiner()
{
int black_count = 0;
int white_count = 0;
int i = 0;
for(i = 0;i < 11;i++)//横向判断
{
for(int j = 0;j < 11;j++)
{
if(chess[i][j] == 1)
{
black_count++;
if(black_count == 5)
{
JOptionPane.showMessageDialog(this, "黑棋胜利");
Clear_Chess();
return;
}
}
else
{
black_count = 0;
}
if(chess[i][j] == 2)
{
white_count++;
if(white_count == 5)
{
JOptionPane.showMessageDialog(this, "白棋胜利");
Clear_Chess();
return;
}
}
else
{
white_count = 0;
}
}
}
for(i = 0;i < 11;i++)//竖向判断
{
for(int j = 0;j < 11;j++)
{
if(chess[j][i] == 1)
{
black_count++;
if(black_count == 5)
{
JOptionPane.showMessageDialog(this, "黑棋胜利");
Clear_Chess();
return;
}
}
else
{
black_count = 0;
}
if(chess[j][i] == 2)
{
white_count++;
if(white_count == 5)
{
JOptionPane.showMessageDialog(this, "白棋胜利");
Clear_Chess();
return;
}
}
else
{
white_count = 0;
}
}
}
for(i = 0;i < 7;i++)//左向右斜判断
{
for(int j = 0;j < 7;j++)
{
for(int k = 0;k < 5;k++)
{
if(chess[i + k][j + k] == 1)
{
black_count++;
if(black_count == 5)
{
JOptionPane.showMessageDialog(this, "黑棋胜利");
Clear_Chess();
return;
}
}
else
{
black_count = 0;
}
if(chess[i + k][j + k] == 2)
{
white_count++;
if(white_count == 5)
{
JOptionPane.showMessageDialog(this, "白棋胜利");
Clear_Chess();
return;
}
}
else
{
white_count = 0;
}
}
}
}
for(i = 4;i < 11;i++)//右向左斜判断
{
for(int j = 6;j >= 0;j--)
{
for(int k = 0;k < 5;k++)
{
if(chess[i - k][j + k] == 1)
{
black_count++;
if(black_count == 5)
{
JOptionPane.showMessageDialog(this, "黑棋胜利");
Clear_Chess();
return;
}
}
else
{
black_count = 0;
}
if(chess[i - k][j + k] == 2)
{
white_count++;
if(white_count == 5)
{
JOptionPane.showMessageDialog(this, "白棋胜利");
Clear_Chess();
return;
}
}
else
{
white_count = 0;
}
}
}
}
}
void Clear_Chess()
{
for(int i=0;i<11;i++)
{
for(int j=0;j<11;j++)
{
chess[i][j]=0;
}
}
repaint();
}
public void paint(Graphics g)
{
Drawline(g);
Drawchess(g);
}
public void mouseExited(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void mouseReleased(MouseEvent e){}
public void mouseClicked(MouseEvent e){}
}
class myframe extends Frame implements WindowListener
{
mypanel panel;
myframe()
{
setLayout(null);
panel = new mypanel();
add(panel);
panel.setBounds(0,23, 360, 360);
setTitle("单人版五子棋");
setBounds(200, 200, 360, 383);
setVisible(true);
addWindowListener(this);
}
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
public void windowDeactivated(WindowEvent e){}
public void windowActivated(WindowEvent e){}
public void windowOpened(WindowEvent e){}
public void windowClosed(WindowEvent e){}
public void windowIconified(WindowEvent e){}
public void windowDeiconified(WindowEvent e){}
}
public class mywindow
{
public static void main(String argc [])
{
myframe f = new myframe();
}
}
5. 求java小游戏源代码
import java.applet.Applet;
import java.applet.AudioClip;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Toolkit;
import javax.sound.sampled.AudioFileFormat;
import javax.sound.sampled.AudioSystem;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.Rectangle;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JSlider;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.io.File;
import java.util.Vector;
public class Frame extends JFrame implements Runnable {
JPanel contentPane;
JPanel jPanel1 = new JPanel();
JButton jButton1 = new JButton();
JSlider jSlider1 = new JSlider();
JLabel jLabel1 = new JLabel();
JButton jButton2 = new JButton();
JLabel jLabel2 = new JLabel();
int count = 1, rapidity = 80; // count 当前进行的个数, rapidity 游标的位置
int zhengque = 0, cuowu = 0;
int rush[] = { 10 ,20 ,30 }; //游戏每关的个数 可以自由添加.列 { 10 ,20 ,30 ,40,50}
int rush_count = 0; //记录关数
char list[] = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L',
'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y',
'Z', '1', '2', '3', '4', '5', '6', '7', '8', '9' }; //随数唤戚机出现的数字 可以自由添加
Vector number = new Vector();
String paian = "链罩true";
AudioClip Musci_anjian, Music_shi, Music_chenggong;
public Frame() {
try {
setDefaultCloseOperation(EXIT_ON_CLOSE);
//-----------------声音文件薯陵---------------------
Musci_anjian = Applet.newAudioClip(new File("sounds//anjian.wav")
.toURL());
Music_shi = Applet.newAudioClip(new File("sounds//shi.wav")
.toURL());
Music_chenggong = Applet.newAudioClip(new File(
"sounds//chenggong.wav").toURL());
//---------------------------------------
jbInit();
} catch (Exception exception) {
exception.printStackTrace();
}
}
/**
* Component initialization.
*
* @throws java.lang.Exception
*/
private void jbInit() throws Exception {
contentPane = (JPanel) getContentPane();
contentPane.setLayout(null);
setSize(new Dimension(588, 530));
setTitle("Frame Title");
jPanel1.setBorder(BorderFactory.createEtchedBorder());
jPanel1.setBounds(new Rectangle(4, 4, 573, 419));
jPanel1.setLayout(null);
jButton1.setBounds(new Rectangle(277, 442, 89, 31));
jButton1.setText("开始");
jButton1.addActionListener(new Frame1_jButton1_actionAdapter(this));
jSlider1.setBounds(new Rectangle(83, 448, 164, 21));
jSlider1.setMaximum(100);
jSlider1.setMinimum(1);
jSlider1.setValue(50);
jLabel1.setText("速度");
jLabel1.setBounds(new Rectangle(35, 451, 39, 18));
jButton2.setBounds(new Rectangle(408, 442, 89, 31));
jButton2.setText("结束");
jButton2.addActionListener(new Frame1_jButton2_actionAdapter(this));
jLabel2.setText("第一关:100个");
jLabel2.setBounds(new Rectangle(414, 473, 171, 21));
contentPane.add(jPanel1);
contentPane.add(jButton2);
contentPane.add(jButton1);
contentPane.add(jSlider1);
contentPane.add(jLabel1);
contentPane.add(jLabel2);
this.addKeyListener(new MyListener());
jButton1.addKeyListener(new MyListener());
jSlider1.addKeyListener(new MyListener());
jSlider1.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
rapidity = jSlider1.getValue();
}
});
}
public void run() {
number.clear();
zhengque = 0;
cuowu = 0;
paian = "true";
while (count <= rush[rush_count]) {
try {
Thread t = new Thread(new Tthread());
t.start();
count += 1;
Thread.sleep(1000 + (int) (Math.random() * 2000)); // 生产下组停顿时间
// 最快1快.最慢2秒
} catch (InterruptedException e) {
e.printStackTrace();
}
}
while (true) { // 等待最后一个字符消失
if (number.size() == 0) {
break;
}
}
if (zhengque == 0) { // 为了以后相除..如果全部正确或者错误就会出现错误. 所以..
zhengque = 1;
}
if (cuowu == 0) {
cuowu = 1;
}
if (paian.equals("true")) { // 判断是否是自然结束
if (zhengque / cuowu >= 2) {
JOptionPane.showMessageDialog(null, "恭喜你过关了");
rush_count += 1; // 自动加1关
if (rush_count < rush.length) {
if (rapidity > 10) { // 当速度大于10的时候在-5提加速度.怕速度太快
rapidity -= 5; // 速度自动减10毫秒
jSlider1.setValue(rapidity); // 选择位置
}
Thread t = new Thread(this);
t.start();
} else {
JOptionPane.showMessageDialog(null, "牛B...你通关了..");
rush_count = 0;
count = 0;
}
} else {
JOptionPane.showMessageDialog(null, "请再接再励");
rush_count = 0;
count = 0;
}
} else {
rush_count = 0;
count = 0;
}
}
public void jButton1_actionPerformed(ActionEvent e) {
Thread t = new Thread(this);
t.start();
}
public void jButton2_actionPerformed(ActionEvent e) {
count = rush[rush_count] + 1;
paian = "flase";
}
class Tthread implements Runnable {
public void run() {
boolean fo = true;
int Y = 0, X = 0;
JLabel show = new JLabel();
show.setFont(new java.awt.Font("宋体", Font.PLAIN, 33));
jPanel1.add(show);
X = 10 + (int) (Math.random() * 400);
String parameter = list[(int) (Math.random() * list.length)] + "";
Bean bean = new Bean();
bean.setParameter(parameter);
bean.setShow(show);
number.add(bean);
show.setText(parameter);
while (fo) {
// ---------------------数字下移--------------------
show.setBounds(new Rectangle(X, Y += 2, 33, 33));
try {
Thread.sleep(rapidity);
} catch (InterruptedException e) {
e.printStackTrace();
}
if (Y >= 419) {
fo = false;
for (int i = number.size() - 1; i >= 0; i--) {
Bean bn = ((Bean) number.get(i));
if (parameter.equalsIgnoreCase(bn.getParameter())) {
cuowu += 1;
jLabel2.setText("正确:" + zhengque + "个,错误:" + cuowu
+ "个");
number.removeElementAt(i);
Music_shi.play();
break;
}
}
}
}
}
}
class MyListener extends KeyAdapter {
public void keyPressed(KeyEvent e) {
String uu = e.getKeyChar() + "";
for (int i = 0; i < number.size(); i++) {
Bean bean = ((Bean) number.get(i));
if (uu.equalsIgnoreCase(bean.getParameter())) {
zhengque += 1;
number.removeElementAt(i);
bean.getShow().setVisible(false);
jLabel2.setText("正确:" + zhengque + "个,错误:" + cuowu + "个");
Music_chenggong.play();
break;
}
}
Musci_anjian.play();
}
}
public static void main(String[] args) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception exception) {
exception.printStackTrace();
}
Frame frame = new Frame();
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = frame.getSize();
if (frameSize.height > screenSize.height) {
frameSize.height = screenSize.height;
}
if (frameSize.width > screenSize.width) {
frameSize.width = screenSize.width;
}
frame.setLocation((screenSize.width - frameSize.width) / 2,
(screenSize.height - frameSize.height) / 2);
frame.setVisible(true);
}
}
class Frame1_jButton2_actionAdapter implements ActionListener {
private Frame adaptee;
Frame1_jButton2_actionAdapter(Frame adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jButton2_actionPerformed(e);
}
}
class Frame1_jButton1_actionAdapter implements ActionListener {
private Frame adaptee;
Frame1_jButton1_actionAdapter(Frame adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jButton1_actionPerformed(e);
}
}
class Bean {
String parameter = null;
JLabel show = null;
public JLabel getShow() {
return show;
}
public void setShow(JLabel show) {
this.show = show;
}
public String getParameter() {
return parameter;
}
public void setParameter(String parameter) {
this.parameter = parameter;
}
}
6. 一个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点的数");
}
}
}
已编译通过~
7. 求java小游戏源代码
连连看java源代码
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class lianliankan implements ActionListener
{
JFrame mainFrame; //主面板
Container thisContainer;
JPanel centerPanel,southPanel,northPanel; //子面板
JButton diamondsButton[][] = new JButton[6][5];//游戏按钮数组
JButton exitButton,resetButton,newlyButton; //退出,重列,重新开始按钮
JLabel fractionLable=new JLabel("0"); //分数标签
JButton firstButton,secondButton; //分别记录两次被选中的按钮
int grid[][] = new int[8][7];//储存游戏按钮位置
static boolean pressInformation=false; //判断是否有按钮被选中
int x0=0,y0=0,x=0,y=0,fristMsg=0,secondMsg=0,validateLV; //游戏按钮的位置坐标
int i,j,k,n;//消除方法控制
public void init(){
mainFrame=new JFrame("JKJ连连看");
thisContainer = mainFrame.getContentPane();
thisContainer.setLayout(new BorderLayout());
centerPanel=new JPanel();
southPanel=new JPanel();
northPanel=new JPanel();
thisContainer.add(centerPanel,"Center");
thisContainer.add(southPanel,"South");
thisContainer.add(northPanel,"North");
centerPanel.setLayout(new GridLayout(6,5));
for(int cols = 0;cols < 6;cols++){
for(int rows = 0;rows < 5;rows++ ){
diamondsButton[cols][rows]=new JButton(String.valueOf(grid[cols+1][rows+1]));
diamondsButton[cols][rows].addActionListener(this);
centerPanel.add(diamondsButton[cols][rows]);
}
}
exitButton=new JButton("退出");
exitButton.addActionListener(this);
resetButton=new JButton("重列");
resetButton.addActionListener(this);
newlyButton=new JButton("再来一局");
newlyButton.addActionListener(this);
southPanel.add(exitButton);
southPanel.add(resetButton);
southPanel.add(newlyButton);
fractionLable.setText(String.valueOf(Integer.parseInt(fractionLable.getText())));
northPanel.add(fractionLable);
mainFrame.setBounds(280,100,500,450);
mainFrame.setVisible(true);
}
public void randomBuild() {
int randoms,cols,rows;
for(int twins=1;twins<=15;twins++) {
randoms=(int)(Math.random()*25+1);
for(int alike=1;alike<=2;alike++) {
cols=(int)(Math.random()*6+1);
rows=(int)(Math.random()*5+1);
while(grid[cols][rows]!=0) {
cols=(int)(Math.random()*6+1);
rows=(int)(Math.random()*5+1);
}
this.grid[cols][rows]=randoms;
}
}
}
public void fraction(){
fractionLable.setText(String.valueOf(Integer.parseInt(fractionLable.getText())+100));
}
public void reload() {
int save[] = new int[30];
int n=0,cols,rows;
int grid[][]= new int[8][7];
for(int i=0;i<=6;i++) {
for(int j=0;j<=5;j++) {
if(this.grid[i][j]!=0) {
save[n]=this.grid[i][j];
n++;
}
}
}
n=n-1;
this.grid=grid;
while(n>=0) {
cols=(int)(Math.random()*6+1);
rows=(int)(Math.random()*5+1);
while(grid[cols][rows]!=0) {
cols=(int)(Math.random()*6+1);
rows=(int)(Math.random()*5+1);
}
this.grid[cols][rows]=save[n];
n--;
}
mainFrame.setVisible(false);
pressInformation=false; //这里一定要将按钮点击信息归为初始
init();
for(int i = 0;i < 6;i++){
for(int j = 0;j < 5;j++ ){
if(grid[i+1][j+1]==0)
diamondsButton[i][j].setVisible(false);
}
}
}
public void estimateEven(int placeX,int placeY,JButton bz) {
if(pressInformation==false) {
x=placeX;
y=placeY;
secondMsg=grid[x][y];
secondButton=bz;
pressInformation=true;
}
else {
x0=x;
y0=y;
fristMsg=secondMsg;
firstButton=secondButton;
x=placeX;
y=placeY;
secondMsg=grid[x][y];
secondButton=bz;
if(fristMsg==secondMsg && secondButton!=firstButton){
xiao();
}
}
}
public void xiao() { //相同的情况下能不能消去。仔细分析,不一条条注释
if((x0==x &&(y0==y+1||y0==y-1)) || ((x0==x+1||x0==x-1)&&(y0==y))){ //判断是否相邻
remove();
}
else{
for (j=0;j<7;j++ ) {
if (grid[x0][j]==0){ //判断第一个按钮同行哪个按钮为空
if (y>j) { //如果第二个按钮的Y坐标大于空按钮的Y坐标说明第一按钮在第二按钮左边
for (i=y-1;i>=j;i-- ){ //判断第二按钮左侧直到第一按钮中间有没有按钮
if (grid[x][i]!=0) {
k=0;
break;
}
else{ k=1; } //K=1说明通过了第一次验证
}
if (k==1) {
linePassOne();
}
}
if (y<j){ //如果第二个按钮的Y坐标小于空按钮的Y坐标说明第一按钮在第二按钮右边
for (i=y+1;i<=j ;i++ ){ //判断第二按钮左侧直到第一按钮中间有没有按钮
if (grid[x][i]!=0){
k=0;
break;
}
else { k=1; }
}
if (k==1){
linePassOne();
}
}
if (y==j ) {
linePassOne();
}
}
if (k==2) {
if (x0==x) {
remove();
}
if (x0<x) {
for (n=x0;n<=x-1;n++ ) {
if (grid[n][j]!=0) {
k=0;
break;
}
if(grid[n][j]==0 && n==x-1) {
remove();
}
}
}
if (x0>x) {
for (n=x0;n>=x+1 ;n-- ) {
if (grid[n][j]!=0) {
k=0;
break;
}
if(grid[n][j]==0 && n==x+1) {
remove();
}
}
}
}
}
for (i=0;i<8;i++ ) { //列
if (grid[i][y0]==0) {
if (x>i) {
for (j=x-1;j>=i ;j-- ) {
if (grid[j][y]!=0) {
k=0;
break;
}
else { k=1; }
}
if (k==1) {
rowPassOne();
}
}
if (x<i) {
for (j=x+1;j<=i;j++ ) {
if (grid[j][y]!=0) {
k=0;
break;
}
else { k=1; }
}
if (k==1) {
rowPassOne();
}
}
if (x==i) {
rowPassOne();
}
}
if (k==2){
if (y0==y) {
remove();
}
if (y0<y) {
for (n=y0;n<=y-1 ;n++ ) {
if (grid[i][n]!=0) {
k=0;
break;
}
if(grid[i][n]==0 && n==y-1) {
remove();
}
}
}
if (y0>y) {
for (n=y0;n>=y+1 ;n--) {
if (grid[i][n]!=0) {
k=0;
break;
}
if(grid[i][n]==0 && n==y+1) {
remove();
}
}
}
}
}
}
}
public void linePassOne(){
if (y0>j){ //第一按钮同行空按钮在左边
for (i=y0-1;i>=j ;i-- ){ //判断第一按钮同左侧空按钮之间有没按钮
if (grid[x0][i]!=0) {
k=0;
break;
}
else { k=2; } //K=2说明通过了第二次验证
}
}
if (y0<j){ //第一按钮同行空按钮在与第二按钮之间
for (i=y0+1;i<=j ;i++){
if (grid[x0][i]!=0) {
k=0;
break;
}
else{ k=2; }
}
}
}
public void rowPassOne(){
if (x0>i) {
for (j=x0-1;j>=i ;j-- ) {
if (grid[j][y0]!=0) {
k=0;
break;
}
else { k=2; }
}
}
if (x0<i) {
for (j=x0+1;j<=i ;j++ ) {
if (grid[j][y0]!=0) {
k=0;
break;
}
else { k=2; }
}
}
}
public void remove(){
firstButton.setVisible(false);
secondButton.setVisible(false);
fraction();
pressInformation=false;
k=0;
grid[x0][y0]=0;
grid[x][y]=0;
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==newlyButton){
int grid[][] = new int[8][7];
this.grid = grid;
randomBuild();
mainFrame.setVisible(false);
pressInformation=false;
init();
}
if(e.getSource()==exitButton)
System.exit(0);
if(e.getSource()==resetButton)
reload();
for(int cols = 0;cols < 6;cols++){
for(int rows = 0;rows < 5;rows++ ){
if(e.getSource()==diamondsButton[cols][rows])
estimateEven(cols+1,rows+1,diamondsButton[cols][rows]);
}
}
}
public static void main(String[] args) {
lianliankan llk = new lianliankan();
llk.randomBuild();
llk.init();
}
}
//old 998 lines
//new 318 lines
8. 用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;