【问题标题】:Loop and condition not working循环和条件不起作用
【发布时间】:2013-12-04 18:54:21
【问题描述】:

用户输入一个 ID,如果它存在,它应该显示它已经存在并再次循环回到 inout 部分。但由于某种原因,它没有这样做。在它的最后,我有代码写入一个执行的文件,即使我有一个条件让它不执行。我已经尝试了几件事,但不知道为什么它不起作用。

JTextField aaField = new JTextField(5);
JTextField bbField = new JTextField(10);
JTextField ccField = new JTextField(10);   
JTextField ddField = new JTextField(10);
JTextField eeField = new JTextField(5);
JTextField ffField = new JTextField(5);
boolean done = false;
while(done == false) {
    JPanel myPanel1 = new JPanel();
    myPanel1.add(new JLabel("ID:"));
    myPanel1.add(aaField);
    myPanel1.add(Box.createVerticalStrut(5));
    myPanel1.add(new JLabel("Name:"));
    myPanel1.add(bbField);
    myPanel1.add(Box.createHorizontalStrut(5));
    myPanel1.add(new JLabel("Address:"));
    myPanel1.add(ccField); // a spacer
    myPanel1.add(Box.createHorizontalStrut(10));
    myPanel1.add(new JLabel("Email:"));
    myPanel1.add(ddField);
    myPanel1.add(Box.createVerticalStrut(10));
    myPanel1.add(new JLabel("Password:"));
    myPanel1.add(eeField);  // a spacer
    myPanel1.add(Box.createVerticalStrut(10));
    myPanel1.add(new JLabel("dob:"));
    myPanel1.add(ffField);  // a spacer
    result1 = JOptionPane.showConfirmDialog(null, myPanel1, 
                "Please Enter Values", JOptionPane.OK_CANCEL_OPTION);

    for(int xx = 0; xx< course.size();xx++) {  
        ch = course.get(xx).getId();    
        if(aaField.getText().matches(ch)) {
            JOptionPane.showMessageDialog(null,"Please enter an ID that doesnt already exist.");
        }
        if(!(aaField.getText().matches(ch))) {
            done = true;
        }
    }
}
if(done == true && !(aaField.getText().matches(ch))&& result1 ==JOptionPane.OK_OPTION) {
    Mate a = new Mate();
    a.addMate(aaField.getText(),bbField.getText(),ccField.getText(),ddField.getText(),eeField.getText(),ffField.getText());
}

【问题讨论】:

  • 请格式化您的代码。
  • 哇,这是一大块格式错误的代码。我注意到最后你有 if(done = true &&... 这是分配,你可能想要比较 if(done == true && ...
  • 请格式化您的代码...
  • 即使我改变它也不起作用
  • 刚刚改好了

标签: java loops while-loop conditional-statements


【解决方案1】:

“...我有一个条件让它不这样做。”看起来你有,但你基本上没有。

for(int xx = 0; xx< course.size();xx++) {  
    ch = course.get(xx).getId();    
    if(aaField.getText().matches(ch)) {
        JOptionPane.showMessageDialog(null,"Please enter an ID that doesnt already exist.");
    }
    if(!(aaField.getText().matches(ch))) {
        done = true;
    }
}

我假设course 是某种列表,您从中获取某种现有值。如果您检查此代码,您会发现done如果课程中的任何值不匹配,则始终为真。

我认为你想要的基本上是这个有点颠倒的结构:

boolean done = true;
do {

    // your giant block

    for (int i = 0; i < course.size(); i++) {
        if (aaField.getText().matches(course.get(i).getId())) {
            JOptionPane.showMessageDialog(null, "Entry cannot be a duplicate.");
            done = false;
            break;
        }
    }

} while (!done);

作为旁注,您的代码很混乱。如果您是初学者,这没关系,但您需要继续努力。对于初学者,不要在循环内创建 GUI 组件。

另外,您使用的是 matches 方法,我怀疑您是否有 regular expressions 的列表,因此您可能希望改用 equals 方法:

if (aaField.getText().equals(ch)) {

除非你确实有一个正则表达式列表,无论出于何种原因,我猜在这种情况下正则表达式。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-26
    • 2017-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-30
    相关资源
    最近更新 更多