如果,Else语句麻烦(Java)

时间:2015-12-06 14:24:09

标签: java if-statement

我在显示某些文字和打开新课时遇到问题。我似乎只能做一个,但不能同时做到两个,否则我会在Else说“其他没有if”时出错。我必须// println或app.loginOption,因为我不能没有错误发生。谢谢你的帮助

    ResultSet rs = st.executeQuery("SELECT username, password from register WHERE username ='" + username + "' and password = '" + password + "'");
        if(rs.next())
            Apps.loginOptions();
            //System.out.println("Login successful! " );

        else
            System.out.println("Username or login not found. Please try again: ");

2 个答案:

答案 0 :(得分:0)

将两个陈述放在括号内:

if(rs.next()) {
            Apps.loginOptions();
            System.out.println("Login successful! " );

         }
  else
     System.out.println("Username or login not found. Please try again: ");

答案 1 :(得分:0)

编译时,您的代码等同于:

if(rs.next()) {
                Apps.loginOptions();
}
//System.out.println("Login successful! " );
else {
    System.out.println("Username or login not found. Please try again:");
}

你想要的是:

if(rs.next()) {
    Apps.loginOptions();
    //System.out.println("Login successful! " );
} else {
    System.out.println("Username or login not found. Please try again:");
}