【问题标题】:How do I stop the for loop or use the original data如何停止 for 循环或使用原始数据
【发布时间】:2021-12-27 11:59:11
【问题描述】:

对不起我的英语,但我应该如何运行错误消息而不重复 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);
        }

如果由于我的英语不好,让大家产生了误解,我深表歉意。

【问题讨论】:

  • 拜托,您能解释一下您要做什么吗?您似乎因为尝试将错误的解决方案应用于业务需求而遇到此问题。如果您解释您想要做什么,我们可以为您指出正确的解决方案。因为这段代码看起来本质上是错误的。 data != data 永远是假的。

标签: java loops swing joptionpane


【解决方案1】:

如果我理解您的回答,您将找到“继续”功能。

这个打破了 de for 循环。

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

【讨论】:

  • 感谢您的帮助
【解决方案2】:

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

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

【讨论】:

  • 非常感谢您的帮助
猜你喜欢
  • 1970-01-01
  • 2021-03-15
  • 2022-01-11
  • 2021-09-18
  • 1970-01-01
  • 1970-01-01
  • 2015-02-20
  • 2021-04-16
相关资源
最近更新 更多