如何停止 for 循环或使用原始数据

How do I stop the for loop or use the original data

抱歉我的英语不好,但我怎么能 运行 错误消息而不重复 3 次呢?

这是原来的,如果我运行 ERROR JOptionPane 它将重复3次

String name = JOptionPane.showInputDialog(null, "Enter the Student name to search:", "Input", JOptionPane.QUESTION_MESSAGE);
        String data = ("Cannot find the student \"" + name + "\"!!"); 
        for (int i = 0; i < student.length; i++){    
            if(name.equals(student[i].getName())){
                data = ("\n Course \t Admin# \t Name \n" + student[i].getName() + student[i].getAdminNum() + student[i].getCourse() + student[i].getGpa() + student[i].getNewModule() + "\n");
                JOptionPane.showMessageDialog(null, data, "Message", JOptionPane.INFORMATION_MESSAGE);
            }
            Else{
                 JOptionPane.showMessageDialog(null, data, "Message", JOptionPane.ERROR_MESSAGE);
            }

这是当前的,if else 语句是错误的,因为我无法读取原始数据

String name = JOptionPane.showInputDialog(null, "Enter the Student name to search:", "Input", JOptionPane.QUESTION_MESSAGE);
        String data = ("Cannot find the student \"" + name + "\"!!"); 
        for (int i = 0; i < student.length; i++){    
            if(name.equals(student[i].getName())){
                data = ("\n Course \t Admin# \t Name \n" + student[i].getName() + student[i].getAdminNum() + student[i].getCourse() + student[i].getGpa() + student[i].getNewModule() + "\n");
            }
        }
        if(data != data){
            JOptionPane.showMessageDialog(null, data, "Message", JOptionPane.INFORMATION_MESSAGE);
        }
        else {
            JOptionPane.showMessageDialog(null, data, "Message", JOptionPane.ERROR_MESSAGE);
        }

如果因为我的英语不好而给大家带来任何误解,我很抱歉。

如果我理解你的回答,你就找到了“继续”功能。

这打破了 de for 循环。

https://www.w3schools.com/java/java_break.asp

创建一个名为 not_found (int) 的局部变量,并在 else 部分增加这个找到的变量,而不是每次都显示对话框。

for循环结束后,检查找到的变量并根据您的要求显示对话框。

int not_found=0;
String name = JOptionPane.showInputDialog(null, "Enter the Student name to 
search:", "Input", JOptionPane.QUESTION_MESSAGE);
    String data = ("Cannot find the student \"" + name + "\"!!"); 
    for (int i = 0; i < student.length; i++){    
        if(name.equals(student[i].getName())){
            data = ("\n Course \t Admin# \t Name \n" + student[i].getName() 
+ student[i].getAdminNum() + student[i].getCourse() + student[i].getGpa() + 
student[i].getNewModule() + "\n");
            JOptionPane.showMessageDialog(null, data, "Message", 
JOptionPane.INFORMATION_MESSAGE);
        }
        else{
             not_found++:
        }

if(not_found!=0){
JOptionPane.showMessageDialog(null, data, 
"Message",OptionPane.ERROR_MESSAGE);
}