如果其他不工作?

If Else not working?

我的 Action Listener 中的 If Else 语句无法正常工作。

当按下 Jbutton 时,如果用户输入了包含数字 0-9+-*/ 的字符串,则程序将正常执行。

否则,JOptionPane 会显示一条错误消息。

在下面的代码中,好像跳过了If条件,不管怎样直接进入了Else???

如果您决定编译此代码..
后缀示例:11+ 转换为 Infix

时等于 (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///////////////////////////////////////////////////////////////////////////////   

隐蔽Class

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();
    }
}

您正在使用的正则表达式 String"\d+" 检查 String 是否仅 包含数字,但您想检查是否它还包含运算符。以下正则表达式就足够了(需要一位数字和一位运算符):

if (fullString.matches(".*\d+[-+*/]*.*")) {
    // Code here...
}

matches 检查字符串 作为一个整体 是否与正则表达式匹配。如果要检查 within 字符串的匹配项,则表达式两端都需要 .*

if (fullString.matches(".*\d+.*") && fullString.matches(".*[-+*/].*")){

但是,它允许用户输入任何内容,只要它在某处至少有一个数字,并且在某处至少有一个运算符。他们可以输入任何他们喜欢的东西,只要它包括那两个部分。

如果您想检查他们是否 输入了数字和运算符,并且至少输入了其中一项:

if (fullString.matches("[-+*/\d]+") && fullString.matches(".*\d.*") && fullString.matches(".*[-+*/].*")){

也就是说 "Only digits and operators, and at least one digit and at least one operator, in any order."

不知道你是想锁定更多(digits then operator, or operator then digits; 只有一个运算符;不允许数字,然后是运算符,然后是更多数字;等等),但所讨论的 if 的基本问题是 "whole string" 问题。