0

我想知道如何计算计算器程序中的空间。现在它被设置为只有在每个值后面都有一个空格时才起作用。如果还有多个空间,我希望它能够工作。我尝试做一些事情(在我执行的操作中,在 '=' 符号测试下),但它并没有真正起作用。所以想知道如何让它考虑多个空格(比如如果在下一个值之前有多个空格,那么它应该认识到是这种情况,并删除额外的空格)。谢谢

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;

public class GUI extends JFrame implements ActionListener
{
JPanel buttonPanel, topPanel, operationPanel;
JTextField display = new JTextField(20);

doMath math = new doMath();
String s = "";
String b= "";
//int counter;

JButton Num1;
JButton Num2;
JButton Num3;
JButton Num4;
JButton Num5;
JButton Num6;
JButton Num7;
JButton Num8;
JButton Num9;
JButton Num0;

JButton Add;
JButton Sub;
JButton Mult;
JButton Div;
JButton Eq;
JButton Clr;
JButton Space;
public GUI()
{
    super("Calculator");
    setSize(400,400);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JPanel mainPanel = new JPanel();
    mainPanel.setLayout(new GridLayout (2,1));

    buttonPanel = new JPanel();        
    buttonPanel.setLayout(new GridLayout(5, 4));
    buttonPanel.add(Num1 = new JButton("1"));
    buttonPanel.add(Num2 = new JButton("2"));
    buttonPanel.add(Num3 = new JButton("3"));
    buttonPanel.add(Num4 = new JButton("4"));
    buttonPanel.add(Num5 = new JButton("5"));
    buttonPanel.add(Num6 = new JButton("6"));
    buttonPanel.add(Num7 = new JButton("7"));
    buttonPanel.add(Num8 = new JButton("8"));
    buttonPanel.add(Num9 = new JButton("9"));
    buttonPanel.add(Num0 = new JButton("0"));        
    buttonPanel.add(Clr = new JButton("C"));
    buttonPanel.add(Eq = new JButton("="));
    buttonPanel.add(Add = new JButton("+"));
    buttonPanel.add(Sub = new JButton("-"));
    buttonPanel.add(Mult = new JButton("*"));
    buttonPanel.add(Div = new JButton("/"));
    buttonPanel.add(Space = new JButton("Space")); 

    Num1.addActionListener(this);
    Num2.addActionListener(this);
    Num3.addActionListener(this);
    Num4.addActionListener(this);
    Num5.addActionListener(this);
    Num6.addActionListener(this);
    Num7.addActionListener(this);
    Num8.addActionListener(this);
    Num9.addActionListener(this);
    Num0.addActionListener(this);
    Clr.addActionListener(this);
    Eq.addActionListener(this);
    Add.addActionListener(this);
    Sub.addActionListener(this);
    Mult.addActionListener(this);
    Div.addActionListener(this);
    Space.addActionListener(this);

    topPanel = new JPanel();         
    topPanel.setLayout(new FlowLayout());
    topPanel.add(display);

    add(mainPanel);

    mainPanel.add(topPanel, BorderLayout.NORTH);
    mainPanel.add(buttonPanel, BorderLayout.SOUTH);

    setVisible(true);

}

public void actionPerformed(ActionEvent e) 
{
    JButton source = (JButton)e.getSource();
    String text = source.getText();
    int counter = 0;

    if (text.equals("=")) 
    {
        doMath math = new doMath();
        for(int i = 0; i <b.length(); i++)
        {

            String c = b.substring(i,(i+1));
            String d = b.substring((i+1),(i+2));
            String temp = "";
            if( c.equals(" ") && c.equals(d))
            {
                temp = b.substring(0,i)+(b.substring(i+1));
                b = temp;
            }
        }           
        int result = math.doMath1(b);

        String answer = ""+result;
        display.setText(answer);                   

    }
    else if(text.equals("Space"))
    {

        counter ++;
        if(counter <2)
        {            
            b+= " ";
            display.setText(b);
        }
        else 
        {
            b+="";
            display.setText(b);
        }
    }

    else if (text.equals("C"))
    {
        b = "";

        display.setText(b);
    }
    else 
    {

        b += (text);

        display.setText(b);

    }
    counter = 0; 

}
}
4

4 回答 4

1

此方法需要了解正则表达式。对于每个字符串,只需用一个空格替换多个连续空格的任何实例。换句话说,使用正则表达式,您可以将"\\s+"(表示“多个空格”)替换为" ",这只是空格字符。

例子:

String c = ...
c = c.replaceAll("\\s+", " ");
于 2012-11-11T02:12:33.357 回答
1
String str = "your     string";
Pattern _pattern = Pattern.compile("\\s+");
Matcher matcher = _pattern.matcher(str);
str = matcher.replaceAll(" ");
于 2012-11-11T02:15:10.360 回答
0

不要使用子字符串,而是使用 aScanner作为String参数。

或者,您可以在按下“空格”按钮后禁用它,然后在按下其他按钮时将其重新启用。

于 2012-11-11T02:09:32.230 回答
0

如果 b 不为空,您可以尝试查看前一个字符charAt,如果它是空格,则什么也不做。

于 2012-11-11T02:15:55.410 回答