【问题标题】:if else -if unexpected Messageif else -if 意外消息
【发布时间】:2023-03-07 16:21:01
【问题描述】:

我有一个涉及ContainerJButtonJPanelJTextArea 和数组的摇摆应用程序。 String 对象数组,包含 5 个元素。

我想通过一个方法返回数组中的所有元素,并在按下JButton后将它们与最终用户在文本区域中输入的元素进行比较。

如果它们相同,则应出现显示匹配元素的JOptionPane 消息。如果它们不同,JoptionPane 应该显示一条消息说Number Entered is not found in myArray 否则,一条消息说please Enter something" should appear

我面临的问题是,当最终用户输入有效号码时,JOptionPane 消息说:Number Entered is not found in myArray 出现很多次,例如输入 4 时,一条JoptionPane 消息说 Number Entered is not found in myArray 出现 3 次。

如果输入的元素正确,我如何防止出现此消息?

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

public class Array_Search extends JFrame {

    String myString[] = { "1", "2", "3", "4", "5" };

    public String[] get_Element() {
        String str[] = new String[myString.length];
        str = myString;
        return str;
    }

    public Array_Search() {
        Container pane = getContentPane();
        JPanel panel = new JPanel();
        final JTextField txt = new JTextField(
                "                                    ");
        JButton b = new JButton("Click Me ");
        panel.add(b);
        panel.add(txt);
        pane.add(panel);
        b.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent ae) {
                String[] str = get_Element();
                String s2 = txt.getText().trim();
                if (s2 != null && s2.length() > 0)
                    for (int i = 0; i < str.length; i++) {
                        if (s2.equals(str[i].trim())) {
                            JOptionPane option = new JOptionPane();
                            option.showInputDialog("" + str[i]);
                        } else {
                            JOptionPane option = new JOptionPane();
                            option.showInputDialog("Number Entered is not found in myArray");
                        }
                    }
                else {
                    JOptionPane o = new JOptionPane();
                    o.showInputDialog("please Enter something");
                }
            }
        });
    }

    public static void main(String[] args) {
        Array_Search myArray = new Array_Search();
        myArray.setSize(500, 500);
        myArray.setVisible(true);
    }
}

【问题讨论】:

  • 请仅指定您遇到错误的代码。
  • 你的格式很糟糕。不要把所有的东西都放在引号里,你还有一些奇怪的多间距。
  • 注意你不需要JOptionPane o = new JOptionPane(); o.showInputDialog("please Enter something");你可以简单地做JOptionPane.showInputDialog("please Enter something");
  • 请注意您忘记了setDefaultCloseOperation(EXIT_ON_CLOSE);

标签: java swing if-statement for-loop


【解决方案1】:

每次发现不匹配的元素时,您的代码都会显示消息。

相反,您需要查看所有元素并在此之后显示Not found 消息。 像这样的东西应该可以工作:

...
if (s2 != null && s2.length() > 0) {
    boolean isFound = false;
    for (int i = 0; i < str.length; i++) {
        if (s2.equals(str[i].trim())) {
            JOptionPane option = new JOptionPane();
            option.showInputDialog("" + str[i]);
            isFound = true;
            break;
        } 
    }
    if(!isFound) {
        JOptionPane option = new JOptionPane();
        option.showInputDialog("Number Entered is not found in myArray");
    }
} else 
...

【讨论】:

    【解决方案2】:

    您在 get_Element 方法中返回一个空数组。 可以这样修复:

     public void actionPerformed(ActionEvent ae) {
    
        String [] str = get_Element(); // replace this 
        String [] str = myString;      // with this
    

    或将 get_Element 更改为:

    public String[] get_Element() {
        return myString;
    }
    

    注意:Java code conventions 对方法名使用驼峰式大小写。 getElement 而不是 get_Element。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-08-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多