If statement 两个if块vs if和else if

If statement 两个if块vs if和else if,if-statement,If Statement,上面的代码有效,而第二个代码无效: if(button.getText().equals("Hello")){ button.setText("Hello World"); } else if(button.getText().equals("Hello World")){ button.setText("Hello"); } 这个直接到第二个if区 我不明白它和第一个有什么不同,它应该先到第一个if块,但它只是跳过了它 我希望你能给我解释一下 以下是完整的代码: if(b

上面的代码有效,而第二个代码无效:

if(button.getText().equals("Hello")){
     button.setText("Hello World");
}
else if(button.getText().equals("Hello World")){
     button.setText("Hello");
}
这个直接到第二个if区

我不明白它和第一个有什么不同,它应该先到第一个if块,但它只是跳过了它

我希望你能给我解释一下

以下是完整的代码:

if(button.getText().equals("Hello")){
     button.setText("Hello World");
}
if(button.getText().equals("Hello World")){
     button.setText("Hello");
}

第一个如果因为text=Hello,它将在第一个之后停止。它不会进入else-if语句

但是对于第二个,它也会检查第二个是否。 首先,它检查text=Hello->这是否为真,因此它将在Hello world中更改。
在此之后,第二个if将检查text=Hello world是否正确。

第一个if将在第一个if之后停止,因为text=Hello。它不会进入else-if语句

但是对于第二个,它也会检查第二个是否。 首先,它检查text=Hello->这是否为真,因此它将在Hello world中更改。
然后,第二个if将检查text=Hello world是否正确。

第二个if中的问题是两个块都被执行了,而else语句阻止了这一点

首先,文本等于“Hello”。在第一个if语句之后,它被更改为“helloworld”

然后转到第二条语句,现在文本是“Hello World”,它也执行该语句,将文本改回“Hello”


在第一个示例中不会出现这种情况,因为
else if
意味着只有在前面的if语句给出错误结果时才会检查条件。

第二个示例中的问题是两个块都被执行,而else语句阻止了这一点

首先,文本等于“Hello”。在第一个if语句之后,它被更改为“helloworld”

然后转到第二条语句,现在文本是“Hello World”,它也执行该语句,将文本改回“Hello”


在第一个示例中不会出现这种情况,因为
else if
意味着只有在前面的if语句给出错误结果时才会检查条件。

解释。第一个:

package javaapplication4;
import java.util.*;
import javax.swing.*;
import java.awt.event.*;

public class JavaApplication4 implements ActionListener{
    JFrame frame;
    JButton button;
    public static void main(String[] args){
        JavaApplication4 gui = new JavaApplication4();
        gui.go();
    }

    public void go() {
        frame = new JFrame();
        button = new JButton("Hello");

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(200,300);
        frame.setVisible(true);
        frame.getContentPane().add(button);

        button.addActionListener(this);
    }

    @Override
    public void actionPerformed(ActionEvent ev){
        if(button.getText().equals("Hello")){
            button.setText("Hello World");
        }
        if(button.getText().equals("Hello World")){
            button.setText("Hello");
        }
    }
}
第二个:

if(button.getText().equals("Hello")){ //let's say it's true, so the text is "Hello"
     button.setText("Hello World"); //now you change the text to "Hello World"
}
else if(button.getText().equals("Hello World")){ //this line is not executed because of "else" keyword, it means that it will be checked ONLY if the first statement was false
     button.setText("Hello"); //this line is not executed
}

解释。第一个:

package javaapplication4;
import java.util.*;
import javax.swing.*;
import java.awt.event.*;

public class JavaApplication4 implements ActionListener{
    JFrame frame;
    JButton button;
    public static void main(String[] args){
        JavaApplication4 gui = new JavaApplication4();
        gui.go();
    }

    public void go() {
        frame = new JFrame();
        button = new JButton("Hello");

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(200,300);
        frame.setVisible(true);
        frame.getContentPane().add(button);

        button.addActionListener(this);
    }

    @Override
    public void actionPerformed(ActionEvent ev){
        if(button.getText().equals("Hello")){
            button.setText("Hello World");
        }
        if(button.getText().equals("Hello World")){
            button.setText("Hello");
        }
    }
}
第二个:

if(button.getText().equals("Hello")){ //let's say it's true, so the text is "Hello"
     button.setText("Hello World"); //now you change the text to "Hello World"
}
else if(button.getText().equals("Hello World")){ //this line is not executed because of "else" keyword, it means that it will be checked ONLY if the first statement was false
     button.setText("Hello"); //this line is not executed
}

你确定不是第一个吗?你试过调试它吗?它可能会转到第一个,然后再转到第二个。你确定它不会转到第一个吗?你试过调试它吗?它可能会转到第一个,然后再转到第二个。可能是的,对不起。对stackoverflow来说是新手。@HuyNguyen不,不太可能。这个问题提供了所有的代码,只是要求解释一下。可能是的,对不起。对stackoverflow来说是新手。@HuyNguyen不,不太可能。这个问题提供了所有的代码,只是要求解释一下。真不敢相信我错过了+1我真不敢相信我错过了+1