Java 即使条件变为false,代码仍在if块内运行
我不知道发生了什么。if块中的代码正在运行,即使条件为false。我已经在netbeans ide 7.4中设计了我的接口,只是在eclipse中复制粘贴了该设计。这是我的代码Java 即使条件变为false,代码仍在if块内运行,java,Java,我不知道发生了什么。if块中的代码正在运行,即使条件为false。我已经在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
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
}
无论我在JinInstrument jcombobox中选择什么,我都会在if中作为输出。我已经检查了print语句,但enterloop的值为0,它仍然会进入if语句块。请帮助我解决这个问题。有一个错误;在这一行中,ifenterloop==1
);表示语句结束。因此,即使if语句返回false,也将执行下一行删除ifenterloop==1行中的分号[;] 尝试这些更新的代码::
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")
}
}
});
instrumentLabel.setText("Instrument");
jPanel2.add(instrumentLabel);
instrumentLabel.setBounds(21, 11, 102, 14);
symbolLabel.setText("Symbol");
jPanel2.add(symbolLabel);
symbolLabel.setBounds(129, 11, 126, 14);
expiryLabel.setText("Expiry");
jPanel2.add(expiryLabel);
expiryLabel.setBounds(261, 11, 64, 14);
optionLabel.setText("OptionType");
jPanel2.add(optionLabel);
optionLabel.setBounds(377, 11, 56, 14);
strikeLabel.setText("Strike Price");
jPanel2.add(strikeLabel);
strikeLabel.setBounds(462, 11, 53, 14);
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
}
在if条件之后不应终止。
如果在您的条件之后出现分号终止,那么您在{//大括号}中编写的任何代码都将永远不会执行。您的代码有一个致命缺陷,即
if(enterloop == 1) // ; bad semi-colon. Which, made this a single line if.
{ // And, as a result this was "just" an anonymous code block.
System.out.println("inside if");
}
也许pastebin是一个更适合你的论坛,请在你将代码发布到SOF之前检查这些小错误……是的,对不起……我忘记看到分号了……因此,我期待着这些错误的出现
if(enterloop == 1) // ; bad semi-colon. Which, made this a single line if.
{ // And, as a result this was "just" an anonymous code block.
System.out.println("inside if");
}