我怎么能退出while循环netbean

时间:2015-12-10 02:26:26

标签: java netbeans

如何在循环时退出此语句,语句中断无用〜

while(rs.next()) {

    String uname=rs.getString("User_ID"); 
    String password=rs.getString("User_PW");
    String cusType=rs.getString("CUS_TYPE");
    if ((user.equals(uname)) && (pwd.equals(password))){
        if (cusType.equals("1")){
         Account_Info_IND accountInfo = new Account_Info_IND();
         accountInfo.setVisible(true);
         this.dispose();
         **BREAK;**
        }
        else {
           Account_Info_COMP accountInfo1 = new Account_Info_COMP();
          accountInfo1.setVisible(true);
          this.dispose();
        }
    }


}

1 个答案:

答案 0 :(得分:3)

我不明白为什么你认为break语句没用,而你没有正确解释它出了什么问题。但是使用break语句是退出循环的完全有效的方法。

例如,运行以下代码段以查看它是否正常工作:

public static void main(String[] args) {
    for (int i = 0; i < 10; i++) {
        System.out.println(i);
        if (i == 2) {
            break;
        }
    }
}

输出:

  

0
  1
  2

也许通过查看上面的代码,您将能够找出代码出了什么问题。

对于它的价值,我完全赞同MadProgrammer的评论,通过编写一个接受必要参数的正确查询,你甚至可能不需要在你的循环中有条件。

相关问题