gpt4 book ai didi

java - 即使条件变为 false,代码也会在 if block 内运行

转载 作者:行者123 更新时间:2023-12-01 07:07:25 25 4
gpt4 key购买 nike

我不知道发生了什么。即使条件为 false,if block 内的代码仍在运行。我在 netbeans ide 7.4 中设计了界面,然后将设计复制粘贴到 eclipse 中。这是我的代码

import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class AddSymbol extends javax.swing.JFrame {

/**
* Creates new form NewJFrame
*/
Connection c;
public AddSymbol() {


initComponents();
}

/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/

// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
String x = "hello";
if(x.equals("hii"))
{
System.out.println("Inside hi");
}

jinstrument = new javax.swing.JComboBox();
jsymbol = new javax.swing.JComboBox();
jexpiry = new javax.swing.JComboBox();
joption = new javax.swing.JComboBox();
jstrikeprice = new javax.swing.JComboBox();
instrument = new javax.swing.JLabel();
symbol = new javax.swing.JLabel();
expiry = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
jLabel5 = new javax.swing.JLabel();
jPanel2 = new javax.swing.JPanel();
instrumentLabel = new javax.swing.JLabel();
symbolLabel = new javax.swing.JLabel();
expiryLabel = new javax.swing.JLabel();
optionLabel = new javax.swing.JLabel();
strikeLabel = new javax.swing.JLabel();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
getContentPane().setLayout(null);

jinstrument.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Equity", "OPTIDX", "OPTSTK", "FUTIDX", "FUTSTK", " " }));
getContentPane().add(jinstrument);
jinstrument.setBounds(10, 38, 83, 20);

jsymbol.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
getContentPane().add(jsymbol);
jsymbol.setBounds(120, 38, 210, 20);

jexpiry.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
getContentPane().add(jexpiry);
jexpiry.setBounds(380, 38, 180, 20);

joption.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "PE", "CE" }));
getContentPane().add(joption);
joption.setBounds(10, 85, 66, 20);

jstrikeprice.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
getContentPane().add(jstrikeprice);
jstrikeprice.setBounds(120, 85, 56, 20);

instrument.setText("Instrument");
getContentPane().add(instrument);
instrument.setBounds(10, 13, 83, 14);

symbol.setText("Symbol");
getContentPane().add(symbol);
symbol.setBounds(120, 13, 218, 14);

expiry.setText("Expiry");
getContentPane().add(expiry);
expiry.setBounds(380, 13, 180, 14);

jLabel4.setText("Option Type");
getContentPane().add(jLabel4);
jLabel4.setBounds(10, 69, 83, 14);

jLabel5.setText("Strike Price");
getContentPane().add(jLabel5);
jLabel5.setBounds(120, 69, 56, 14);

jPanel2.setLayout(null);

jinstrument.addItemListener(new ItemListener() {

@Override
public void itemStateChanged(ItemEvent arg0) {
// TODO Auto-generated method stub
jsymbol.removeAllItems();
int enterloop=0;
if(jinstrument.getSelectedItem().equals("FUTSTK"))
{
enterloop =1;
}


if(enterloop == 1);
{ System.out.println("inside if");

}

}
});



getContentPane().add(jPanel2);
jPanel2.setBounds(10, 135, 577, 338);


}// </editor-fold>

/**
* @param args the command line arguments
*/
public static void main(String args[]) {

java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new AddSymbol().setVisible(true);
}
});
}

// Variables declaration - do not modify
private javax.swing.JLabel expiry;
private javax.swing.JLabel expiryLabel;
private javax.swing.JLabel instrument;
private javax.swing.JLabel instrumentLabel;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JPanel jPanel2;
private javax.swing.JComboBox jexpiry;
private javax.swing.JComboBox jinstrument;
private javax.swing.JComboBox joption;
private javax.swing.JComboBox jstrikeprice;
private javax.swing.JComboBox jsymbol;
private javax.swing.JLabel optionLabel;
private javax.swing.JLabel strikeLabel;
private javax.swing.JLabel symbol;
private javax.swing.JLabel symbolLabel;
// End of variables declaration
}

无论我在 jinstrument jcombobox 中选择什么,我都会得到“inside if”作为输出。我已经检查了 print 语句,但 Enterloop 的值为 0,它仍然进入 if 语句 block 。请帮我解决这个问题

最佳答案

这一行有一个; if(enterloop == 1);

; 表示语句结束。因此,即使 if 语句返回 false,也会执行下一行

关于java - 即使条件变为 false,代码也会在 if block 内运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21013028/

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