比较字符串中的字符时的无限循环

时间:2017-06-27 06:32:04

标签: java swing

我应该使用什么语句逐个比较字符串中的字符,以便它不会进入无限循环。比如在C / C ++中比较一组字符。

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

class Calcc extends JFrame implements ActionListener {

    String l = "b";
    int z = 0;
    JFrame f;
    JTextField t1 = new JTextField(30);
    JTextField t2 = new JTextField(10);
    JButton b1 = new JButton("1");
    JButton b2 = new JButton("2");
    JButton b3 = new JButton("3");
    JButton b4 = new JButton("4");
    JButton b5 = new JButton("5");
    JButton b6 = new JButton("6");
    JButton b7 = new JButton("7");
    JButton b8 = new JButton("8");
    JButton b9 = new JButton("9");
    JButton b10 = new JButton("0");
    JButton b11 = new JButton("+");
    JButton b12 = new JButton("-");
    JButton b13 = new JButton("*");
    JButton b14 = new JButton("/");
    JButton b15 = new JButton("=");

    Calcc() {
        f = new JFrame();
        f.add(t1);
        f.add(t2);
        f.add(b1);
        f.add(b2);
        f.add(b3);
        f.add(b4);
        f.add(b5);
        f.add(b6);
        f.add(b7);
        f.add(b8);
        f.add(b9);
        f.add(b10);
        f.add(b11);
        f.add(b12);
        f.add(b13);
        f.add(b14);
        f.add(b15);
        b1.addActionListener(this);
        b2.addActionListener(this);
        b3.addActionListener(this);
        b4.addActionListener(this);
        b5.addActionListener(this);
        b6.addActionListener(this);
        b7.addActionListener(this);
        b8.addActionListener(this);
        b9.addActionListener(this);
        b10.addActionListener(this);
        b11.addActionListener(this);
        b12.addActionListener(this);
        b13.addActionListener(this);
        b14.addActionListener(this);
        b15.addActionListener(this);
        f.setVisible(true);
        f.setSize(350, 350);
        f.setLayout(new FlowLayout());
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public void actionPerformed(ActionEvent e) {

        String d;
        d = t1.getText();
        if (e.getSource() == b1) {
            t1.setText(d + "1");
        }
        if (e.getSource() == b2) {
            t1.setText(d + "2");
        }
        if (e.getSource() == b3) {
            t1.setText(d + "3");
        }
        if (e.getSource() == b4) {
            t1.setText(d + "4");
        }
        if (e.getSource() == b5) {
            t1.setText(d + "5");
        }
        if (e.getSource() == b6) {
            t1.setText(d + "6");
        }
        if (e.getSource() == b7) {
            t1.setText(d + "7");
        }
        if (e.getSource() == b8) {
            t1.setText(d + "8");
        }
        if (e.getSource() == b9) {
            t1.setText(d + "9");
        }
        if (e.getSource() == b10) {
            t1.setText(d + "0");
        }
        if (e.getSource() == b11) {
            t1.setText(d + "+");
        }
        if (e.getSource() == b12) {
            t1.setText(d + "-");
        }
        if (e.getSource() == b13) {
            t1.setText(d + "*");
        }
        if (e.getSource() == b14) {
            t1.setText(d + "/");
        }
        if (e.getSource() == b15) {
            while (l != "a") {
                int b = calcA(d);
                if (l == "+")
                    z = z + b;
                else if (l == "-")
                    z = z - b;
                else if (l == "*")
                    z = z * b;
                else if (l == "*")
                    z = z * b;
            }
            t2.setText(String.valueOf(z));
        }

    }

    int calcA(String d1) {

        l = "a";
        int k = 0;
        while ((d1 != "+") && (d1 != "-") && (d1 != "*") && (d1 != "/")) {
            if (d1 != "\n")
                break;
            k = k * 10 + Integer.valueOf(d1);
        }
        l = d1;
        return k;
    }
}

public class CalcD {

    public static void main(String[] args) {
        new Calcc();
    }

}

3 个答案:

答案 0 :(得分:1)

您不需要像在C / C ++中那样检查任何字符。您需要使用String的长度简单地迭代它。只需在循环中使用它:

for (int i=0;i<string.length();i++) {
    char c = string.charAt(i);
    // do whatever with your character.
}

也就是说,当您在Java中比较字符串的相等性时,应始终使用String.equals方法而不是==

因此,您应该从

更改代码
getSource() == b1

getSource().equals(b1)

希望这有帮助!

答案 1 :(得分:1)

您似乎在比较字符串很多,使用==来做这是不好的做法。

这是因为字符串是对象,并且使用==运算符进行比较将检查它们是否是完全相同的对象而不管值。字符串是一种特殊情况,在创建String时,jvm将决定是否使用现有对象或创建新对象,因此我们无法100%确定“==”是否会返回true或者甚至不包含它们相同的价值。

要比较字符串,请使用string1.equals(string2)。

.equals方法应该用于比较对象的值,而==运算符用于比较基元。

答案 2 :(得分:0)

谢谢你们澄清一些疑惑.... 对于上面的程序,我使用了String.charAt(int); 比较循环中的每个特定字符....

相关问题