gpt4 book ai didi

Java 预计会执行 else,但始终会执行 if

转载 作者:太空宇宙 更新时间:2023-11-04 11:30:45 26 4
gpt4 key购买 nike

我想你已经明白我想要做什么了。当用户按“确定”而不更改默认文本时给出提示。但第一个条件似乎是正确的。我不知道这是 if 语句的问题还是我的代码的问题。我已经包含了整个类。请务必阅读注释,以避免阅读不必要的代码。

确定=JButton

文本 = JTextField

 int c = 0;
public class handler implements ActionListener{
public void actionPerformed(ActionEvent e){
//
//The following lines are not nessary for the question.
if(ok==e.getSource()){
if(!(male.isSelected() || female.isSelected()) && c==0){
c++;
JOptionPane.showMessageDialog(null,"Hey you haven't selected your gender. Do you wish to proceed","Warning",JOptionPane.PLAIN_MESSAGE);
}
}
//well these lines were not necessary
//They were just in the same class
//
//
//Here the else condition should execute
//
if(ok==e.getSource()) && (text.getText() != "Enter your name")){
JOptionPane.showMessageDialog(null,"Your name is "+text.getText(),"Name",JOptionPane.PLAIN_MESSAGE);
}else if((ok==e.getSource()) && (text.getText() == "Enter your name")){
JOptionPane.showMessageDialog(null,"Hey type in your name buddy ","Name",JOptionPane.PLAIN_MESSAGE);
}
}
}

所以问题是 if 条件被执行,而 else if 应该在我们不更改默认文本“输入你的名字”时执行。我尝试更改文本,在这种情况下 if block 也正在执行。请对您的答案进行完整解释。我是一个技能生锈的初学者。

最佳答案

您需要使用其他方法来比较字符串,例如这里:

text.getText() == "Enter your name")

您可以使用等于:

text.getText().equals("Enter your name")

您应该在原始代码的两个位置执行此操作。

关于Java 预计会执行 else,但始终会执行 if,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43834003/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com