否则没有如果错误? [英] Else without if error?

查看:29
本文介绍了否则没有如果错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道为什么这段代码给我这个问题,请记住它已经在同一个项目的早期表单中工作,但它拒绝在这里工作.

I want to know why this code is giving me this problem, keep in mind that it has already worked in an earlier Form of the same project but it refuses to work here.

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 和其他东西了如指掌,但到目前为止他帮不上什么忙.

And if there is any way for me to calculate jCheckBox values using a different method then I'm very eager to learn. My Professor says he knows almost everything about java netbeans and stuff but so far he's not much of a help.

推荐答案

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

Let's take a closer look at your code's conditional structure by correctly separating and using indentations:

我喜欢添加一点空白来清楚地知道发生了什么.

I like to add a bit of white-space to clearly know what's going on.

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);

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

Now, with that in mind let's examine your code:

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 语句之后才运行下一行.

Here is what I see - this code does not use {} blocks to associate code with conditions. This if fine, but realize that only the very next line runs after any if statement if you don't use the {} blocks.

示例:

if (someCondition)
    this.runSingleLine();

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


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

这篇关于否则没有如果错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆