如题
写了个速算24代码,但是不能识别运算符的优先级。请各位大虾帮帮忙。改下。
代码如下:
import java.awt.BorderLayout;
public class text extends JFrame { private JPanel contentPane;
private JTextField text1;
JButton bu1,bu2,bu5,bu6;
JLabel lab5;
JLabel lab[]=new JLabel[4];
JPanel pa[]=new JPanel[4];
JPanel panel;

/**
 * Launch the application.
 */
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
text frame = new text();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
} /**
 * Create the frame.
 */
public text() {


setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 585, 519);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);

panel = new JPanel();
contentPane.add(panel, BorderLayout.CENTER);
panel.setLayout(new GridLayout(1, 0, 0, 0));

JPanel panel_1 = new JPanel();
contentPane.add(panel_1, BorderLayout.SOUTH);

for(int i=0;i<4;i++){
lab[i]=new JLabel("");
pa[i]=new JPanel();
pa[i].add(lab[i]);
pa[i].setLayout(new GridLayout(0, 1, 0, 0));
panel.add(pa[i]);
}


 bu5 = new JButton("New button");
 bu5.addActionListener(new ActionListener() {
  public void actionPerformed(ActionEvent arg0) {
  if(bu5.getText().equals("开始游戏")){
  for(int i=0;i<4;i++){
  int x=(int) (Math.random()*10);
  lab[i].setText(""+x);
  lab[i].setName(""+x);  
  }
  bu5.setText("重置");
  }else if(bu5.getText().equals("重置")){
  text1.setText("");
  for(int i=0;i<4;i++){
  int x=(int) (Math.random()*10);
  lab[i].setText(""+x);
  lab[i].setName(""+x);  
  }
  }
 
  }
 });
bu5.setText("开始游戏");
panel_1.add(bu5);

 lab5 = new JLabel("New label");
lab5.setText("列式:");
panel_1.add(lab5);

text1 = new JTextField();
panel_1.add(text1);
text1.setColumns(10);

bu6 = new JButton("New button");
bu6.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0){
int []num=new int [4];
String []ops=new String [4];
String text=text1.getText();
String a="";
boolean exist = true,kz=true;

int j=0,k=0;
int result = 0;
for(int i=0;i<4;i++){
ops[i]="";
}
if(text.length()<7){
JOptionPane.showMessageDialog(null, "输入非法");
kz=false;
text1.setText("");
}else{
for(int i=0;i<text.length();i++){
a=text.substring(i,i+1);
if(isNumber(a)){
if(j>4) {
JOptionPane.showMessageDialog(null, "输入非法");
kz=false;
text1.setText("");
break;
}else{
num[j]=Integer.parseInt(a);
j++;

}

}
else if(isOperator(a)){
// if(k>3){
// JOptionPane.showMessageDialog(null, "输入非法");
// kz=false;
// text1.setText("");
// break;
// }else{
ops[k]=a;
k++;
// }

}else {
JOptionPane.showMessageDialog(null, "输入非法");
kz=false;
text1.setText("");
break;
}
}
if(kz){
/*判断输入的数字是否与给与的数字相同 */
for(int i=0;i<4;i++){
int x=0;
 exist=true;
while(x<4&&exist){
int b=Integer.parseInt(lab[x].getText());
if(num[i]==b){
exist=false;
}
x++;
}
if(exist){
break;
}
}
if(!exist){
result=0;
int b=0;
for(int i=0;i<ops.length;i++){
if(ops[i].equals("+")){
result+=num[b];
b++;
}else if(ops[i].equals("*")){
result*=num[b];
b++;
}else if(ops[i].equals("-")){
result-=num[b];
b++;
}else if(ops[i].equals("/")){
result/=num[b];
b++;
}else if(ops[i].equals("(")){

}
}
/*判断输入的是否为24*/
if(result==24){
JOptionPane.showMessageDialog(null, "恭喜你,答对了");
text1.setText("");
for(int i=0;i<4;i++){
  lab[i].setText("");  
  }
bu5.setText("开始游戏");
}else{
JOptionPane.showMessageDialog(null, "答错了,请认真考虑");
text1.setText("");
}
}else{
JOptionPane.showMessageDialog(null, "请输入给予的数字");
text1.setText("");
}
}
}
}
});
bu6.setText("确定");
panel_1.add(bu6);

JPanel panel_5 = new JPanel();
FlowLayout flowLayout = (FlowLayout) panel_5.getLayout();
flowLayout.setAlignment(FlowLayout.RIGHT);
contentPane.add(panel_5, BorderLayout.NORTH);

bu1 = new JButton("关于");
bu1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0){
JOptionPane.showMessageDialog(null, "耗子出品");
}
});
panel_5.add(bu1);

bu2 = new JButton("退出");
bu2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
System.exit(0);
}
});
panel_5.add(bu2); }
private boolean isNumber(String a){
if(a.equals("0")||a.equals("1")||a.equals("2")||a.equals("3")||a.equals("4")
||a.equals("5")||a.equals("6")||a.equals("7")||a.equals("8")||a.equals("9")){
return true;
}else{
return false;
}

}
private boolean isOperator(String a){
if(a.equals("+")||a.equals("-")||a.equals("*")||a.equals("/")||a.equals("(")||a.equals(")")){
return true;
}else{
return false;
}
}}