我的Action Listener中的我的If Else
聲明無法正常工作。如果其他人不工作?
當按下J按鈕時,如果用戶輸入了一個字符串,其數字爲0-9
和+-*/
,則程序正常執行。
否則,JOptionPane會顯示一條錯誤消息。
在下面的代碼中,它似乎跳過If
的條件,並直接去Else
無論是什麼?
如果決定編譯該代碼..
例後綴:11+就等於(1 + 1)時轉化爲綴
主
package p2gui;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import java.awt.event.*;
import javax.swing.JOptionPane;
/**
*
* @author Mike
*/
public class P2GUI extends JFrame implements ActionListener {
JFrame f = new JFrame("Three Address Generator");// Title
private final JButton evaluate;
private final JLabel textfieldLabel;
private final JTextField entryField;
private final JLabel resutfieldlabel;
private final JTextField resultField;
private final JOptionPane popup = new JOptionPane();
P2GUI() {
f.setSize(425, 180);
f.setLayout(null);//using no layout managers
f.setVisible(true);//making the frame visible //window size
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
textfieldLabel = new JLabel("Enter Postfix Expression");
f.add(textfieldLabel);
textfieldLabel.setBounds(10, 10, 160, 25);
entryField = new JTextField("");
//entryField.addActionListener(this);//ActionListener
f.add(entryField);
entryField.setBounds(160, 10, 220, 25);
evaluate = new JButton("Construct Tree");
evaluate.addActionListener(this);//ActionListener
f.add(evaluate);
evaluate.setBounds(137, 55, 130, 30);
resutfieldlabel = new JLabel(" Infix Expression ");
f.add(resutfieldlabel);
resutfieldlabel.setBounds(20, 100, 100, 25);
resultField = new JTextField("");
//resultField.addActionListener(this);//ActionListener
resultField.setEditable(false);
f.add(resultField);
resultField.setBounds(125, 100, 220, 25);
}
@Override
public void actionPerformed(ActionEvent e) {
String fullString;
fullString = entryField.getText().trim();
if(fullString.matches("\\d+") && fullString.matches("[-+*/]")){
Convert conversion = new Convert();
resultField.setText(conversion.convert(fullString));
} else {
JOptionPane.showMessageDialog(null, "Please Enter Digit and
Arithmetic operator");
//eraseTextField();
}
}
public void eraseTextField() {
entryField.setText("");
entryField.requestFocus();
}
public static void main(String[] args) {
P2GUI p1GUI;
p1GUI = new P2GUI();
}
}
/////////////////////////////////////////////END///////////////////////////////////////////////////////////////////////////////
隱蔽類
package p2gui;
import java.util.Stack;
import javax.swing.JOptionPane;
/**
*
* @author Mike
*/
public class Convert {
/**
* Checks if the input is operator or not
* @param c input to be checked
* @return true if operator
*/
private boolean operator(char c){
return c == '+' || c == '-' || c == '*' || c =='/' || c == '^';
}
/**
* Converts any postfix to infix
* @param postfix String expression to be converted
* @return String infix expression produced
*/
public String convert(String postfix){
Stack<String> stackIt = new Stack<>();
for (int i = 0; i < postfix.length(); i++) {
char c = postfix.charAt(i);
if (operator(c)) {
String b = stackIt.pop();
String a = stackIt.pop();
stackIt.push("(" + a + c + b + ")");
} else {
stackIt.push("" + c);
}
}
return stackIt.pop();
}
}
請注意,一個變量只能包含一個值,這是OR和AND運算符中的缺陷 – abcOfJavaAndCPP
@abcOfJava:否,OP正在嘗試查明字符串是否包含數字*,並且*包含運算符。 –