否则没有,如果错误?

问题描述:

我想知道为什么这段代码给了我这个问题,请记住它已经在同一个项目的早期表单中工作,但它拒绝在这里工作。否则没有,如果错误?

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt){           
if (jCheckBox1.isSelected()== true) 
jCheckBox1.equals(56); 
if (jCheckBox2.isSelected()== true) 
jCheckBox2.equals(50); 
if (jCheckBox3.isSelected()== true) 
jCheckBox3.equals(56); 
if (jCheckBox4.isSelected()== true) 
jCheckBox4.equals(56); 
if (jCheckBox5.isSelected()== true) 
jCheckBox5.equals(56); 
if (jCheckBox6.isSelected()== true) 
jCheckBox6.equals(56); 
new Form6().setVisible(true); 

else 
if (jCheckBox1.isSelected()== false) 
jCheckBox1.equals(0); 
if (jCheckBox2.isSelected()== false) 
jCheckBox2.equals(0); 
if (jCheckBox3.isSelected()== false) 
jCheckBox3.equals(0); 
if (jCheckBox4.isSelected()== false) 
jCheckBox4.equals(0); 
if (jCheckBox5.isSelected()== false) 
jCheckBox5.equals(0); 
if (jCheckBox6.isSelected()== false) 
jCheckBox6.equals(0); 

JOptionPane.showMessageDialog(this, "Please Choose An Option and Try Again"); 

,如果有任何办法,我计算使用不同的方法jCheckBox值,那么我非常渴望学习。我的教授说,他几乎知道关于java netbeans和东西的所有内容,但到目前为止,他没有什么帮助。

+3

这就是为什么初学者应该在所有的使用括号的if语句(我喜欢做它内嵌以节省空间)。 (jCheckBox6.isSelected()== false){jCheckBox6.equals(0); if(jCheckBox6.isSelected()== false) }' 将它们关闭应该是一种先进的操作,而不是默认设置。 – Gavin42

+1

@ Gavin42:只有初学者? –

+4

就个人而言,我使用它们的时间为99.9%。我更喜欢视觉确认块在哪里开始和结束。 – Gavin42

让我们来看看你的代码的条件结构细看通过正确分离和使用缩进:

我喜欢有点空白添加到清楚知道发生了什么。

private void main(void){ 
    //Indentation shows how program blocks are related to one another 
    if (this.condition(parameters) == true) 
     // Indentation here shows the following statement is clearly associated with the above condition. 
     this.DoSomething(parameters); 

现在,考虑到这一点,让我们检查你的代码:

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

    if (jCheckBox1.isSelected()== true) 
     jCheckBox1.equals(56); 

    if (jCheckBox2.isSelected()== true) 
     jCheckBox2.equals(50); 

    if (jCheckBox3.isSelected()== true) 
     jCheckBox3.equals(56); 

    if (jCheckBox4.isSelected()== true) 
     jCheckBox4.equals(56); 

    if (jCheckBox5.isSelected()== true) 
     jCheckBox5.equals(56); 

    if (jCheckBox6.isSelected()== true) 
     jCheckBox6.equals(56); 

    new Form6().setVisible(true); // This statement will always run because it's not associated with a condition. 

    else // Oops! Here is an else that is not associated with an if statement! This probably doesn't compile 
     if (jCheckBox1.isSelected()== false) // This if is conditional on the else above. 
      jCheckBox1.equals(0); 

    if (jCheckBox2.isSelected()== false) 
     jCheckBox2.equals(0); 

    if (jCheckBox3.isSelected()== false) 
     jCheckBox3.equals(0); 

    if (jCheckBox4.isSelected()== false) 
     jCheckBox4.equals(0); 

    if (jCheckBox5.isSelected()== false) 
     jCheckBox5.equals(0); 

    if (jCheckBox6.isSelected()== false) 
     jCheckBox6.equals(0); 

    JOptionPane.showMessageDialog(this, "Please Choose An Option and Try Again"); 
} // I assume you mean to close the method 

这是我看到的 - 这个代码不使用{}块将代码与条件相关联。如果这很好,但是要意识到如果不使用{}块,则只有在if语句之后才会运行下一行。

例子:

if (someCondition) 
    this.runSingleLine(); 

if (someCondition) 
    this.runSingleLine(); 
else 
    this.runSomeOtherSingleLine(); 


if (someCondition) 
{ 
    this.doSomething(); 
    this.doSomethingElse(); 
    ... 
    this.finishProcedure(); 
} 

使用curly brackets (braces)并缩进到避免这种微不足道的错误。

代码

if (jCheckBox6.isSelected()== true) 
jCheckBox6.equals(56); 
new Form6().setVisible(true); 

else 
if (jCheckBox1.isSelected()== false) 
jCheckBox1.equals(0); 

相当于

if (jCheckBox6.isSelected()== true) { 
    jCheckBox6.equals(56); 
} 
new Form6().setVisible(true); // <-- WHAT??? 

else if (jCheckBox1.isSelected()== false) { // <-- WHERE IS MY IF? 
    jCheckBox1.equals(0); 
} 

虽然上面解释了语法错误,所得到的程序将仍然是无义 - 这既是由于无意义“其他“配对,因为所有的陈述都没有(或者说)意味着什么。然而,没有问题描述的进一步猜测似乎并不明智。

+2

+1为程序困惑的评论。 – christopher

我不明白这可能在以前的项目中有效。

这else语句本身(没有对应的if语句):

else 
      if (jCheckBox1.isSelected()== false) 

而这个又是什么意义呢?你是不是赋予这个新对象的引用:

new Form6().setVisible(true);