我的if语句没有拿起

时间:2013-11-13 15:37:17

标签: netbeans boolean

这是我的问题:当我运行我的代码时,它应该检查两个boo-leans条件。一个应该是假的而另一个是真的。我还在其中放入了一个else代码,并编写了System-out-print ERROR以及boo-leans值。当我运行它时,它打印出一个WAS为假,一个WAS为真,但它没有被if语句捕获。这是我的代码:

    ' if((falseOnce = false)&(tAndCSelected = true)){//these are my two booleans
         String url = "jdbc:derby://localhost:1527/Register";//place where database Register is
     try{
                         String sql = "INSERT INTO Register(Username, Password, Email,Title,Prediction,Date_Joined) VALUES (?,?,?,?,?,?)";
                       String usernme = "nbuser";
                       String passed ="nbuser";
                       Connection connection = DriverManager.getConnection(url, usernme,passed);//makes a connector to the database
                         java.util.Calendar cal = java.util.Calendar.getInstance();//gets date
                        java.util.Date date = cal.getTime();
                        java.sql.Date sqldate = new Date(date.getTime());
                         PreparedStatement action = connection.prepareStatement(sql);                            
                         action.setString(1, uname);//in first position, username goes in
                         action.setString(2,password);
                         action.setString(3,email.getText());
                         action.setString(4,null);
                         action.setString(5,null);
                         action.setDate(6,sqldate);
                         action.executeUpdate();//executes the instructions
                         redirect.setText("Your account has been created. You may now log in");
                         new Timer(3000,null);//3 seconds before going back to logIn page.
                         new LogIn().setVisible(true);
                         setVisible(false);
       }catch(SQLException e){
     System.out.println("unsuccessful"+e);
       }
 }
 else{
     System.out.println("ERROR" + "falseOnce is " + falseOnce + "and tAndCSelected is"       + tAndCSelected );
 }
    '  

其余代码只是将数据输入到数据库中,如果两个布尔条件正如我所说的那样。问题是什么?

1 个答案:

答案 0 :(得分:3)

if((falseOnce = false)&(tAndCSelected = true))

每个运算符的数量太少...在Java中,等式比较为==,而不是=,逻辑且为&&,而不是&。那应该是

if((falseOnce == false)&&(tAndCSelected == true))

或更简洁

if(!falseOnce && tAndCSelected)

(仅=是赋值运算符,&单独是二进制AND)