简体   繁体   English

即使遇到语句,else 语句也会继续执行

[英]else statement proceeds to execute even if statements are encountered

I am working in Java and I have come across a trouble in my code where I have multiple if statements and one else .我正在使用 Java 并且在我的代码中遇到了一个问题,我有多个if语句和一个else Despite the if statements being true, the else statements still execute.尽管 if 语句为真,else 语句仍然执行。 I've been encountering this all day and I can't seem to find the error.我一整天都在遇到这个问题,我似乎找不到错误。 If anyone can help如果有人可以帮忙

private void confirmActionPerformed(java.awt.event.ActionEvent evt) {                                       

    if(lname1.getText().equals("")){
         lname_ver1.setText("Field is empty!");

     }
    if (mname1.getText().equals("")){
        mname_ver1.setText("Field is empty!");

    }
    if(fname1.getText().equals("")){
       fname_ver.setText("Field is empty!");

    }
     if(s_age.getSelectedIndex()== 0){
     age_ver.setText("Field is empty!");
    }
    if(!female_button.isSelected()&& !male_button.isSelected()){
    gender_ver1.setText("Select gender!");
    }
    if(month.getSelectedIndex()==0 && day.getSelectedIndex()==0 && year.getSelectedIndex()==0){
            birth_ver1.setText("Field is empty!");
            }

    if (s_num.getText().equals("")){
          num_ver.setText("Field is empty!"); 

    }


     if(!email.getText().matches("\"^[a-zA-Z0-9]+[@]+[a-zA-Z0-9]+[.]+[a-zA-Z0-9]+$")){
       email_ver1.setText("Invalid email!");
     }
     if(email.getText().equals("")){
        email_ver1.setText("Field is empty!");

     if(fathers_name.getText().equals("")){
        father_ver1.setText("Field is empty!")
    }
     if(mothers_name.getText().equals("")){
     mother_ver1.setText("Field is empty!");   
    }
     if(parent_no.getText().equals("")){
     no_verify1.setText("Field is empty!");  
     }
     if(parent_no.getText().contains("[a-zA-Z]")){
         no_verify1.setText("Field is empty!");
     }

     if (s_nat.getText().equals("")){
      nat_ver.setText("Field is empty!");
     }
     if(!single.isSelected() && married.isSelected()){
            nat_ver.setText("Select status!!");
     }



   else
    {
        JOptionPane.showMessageDialog(null, "SUCCESS", "INFO    ", JOptionPane.INFORMATION_MESSAGE);
        dispose();
        Schedule sched = new Schedule();
        sched.setLocationRelativeTo(null);
        sched.setResizable(false);
        sched.setVisible(true);
    }`

1)Please verify whether braces opening and closing is perfectly matching your requirements. 1)请确认牙套的开合是否完全符合您的要求。 2)If else format.... if{} else if{} else if{} else{} In this format if any of the condition before else works, then else wont execute, but your case is little different like 2)If else 格式.... if{} else if{} else if{} else{} 在这种格式中,如果 else 之前的任何条件有效,则 else 不会执行,但您的情况与此有点不同

if{}
if{}
if{}
if{}
.
.
.
.
if{}  ----//last if
else{}

so in this case, else is applicable only for the last if, so when last if fails, it goes for else, and the if conditions which are mentioned before that doesn't have any relation with else.

All the if statements are independent from each other.所有的 if 语句都是相互独立的。 The else belongs only to the last if statement. else 只属于最后一个 if 语句。 It will be executed if single or married have been selected.如果选择了单身或已婚,它将被执行。 The other conditions do not matter.其他条件无关紧要。

I think, your intention is evaluating user inputs.我认为,您的意图是评估用户输入。 You want to put an error message to each invalid input and proceed only if everything is valid.您希望为每个无效输入添加一条错误消息,并仅在所有内容都有效时才继续。 If this is what you wanted, there is a simple solution for it:如果这是您想要的,有一个简单的解决方案:

private void confirmActionPerformed(java.awt.event.ActionEvent evt) {
  boolean valid = true;

  if (lname1.getText().equals("")) {
    lname_ver1.setText("Field is empty!");
    valid = false;
  }
  if (mname1.getText().equals("")) {
    mname_ver1.setText("Field is empty!");
    valid = false;
  }
  ...
  if (valid) {
    JOptionPane.showMessageDialog(null, "SUCCESS", "INFO    ", JOptionPane.INFORMATION_MESSAGE);
    dispose();
    Schedule sched = new Schedule();
    sched.setLocationRelativeTo(null);
    sched.setResizable(false);
    sched.setVisible(true);
  }
}

And keep you parantheses balanced, look at the statement if(email.getText().equals("")){ .并保持括号平衡,看看语句if(email.getText().equals("")){

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM