如何使用textfield和button从if else语句中获取结果

时间:2014-05-08 12:00:47

标签: java netbeans

如何使用if else语句使用textfield作为输入然后使用按钮显示结果,我尝试使用下面的代码,但它不允许我使用"如果不是"第二个条件的条件,下面是我的代码;

int x = Integer.parseInt(jTextField1.getText());

if  (x>=120&x<200)
         {JOptionPane.showMessageDialog(null, "select drum : PPJ upwards")};

if else (x>=230&x<=300);
     {JOptionPane.showMessageDialog(null,"select drum :RRf Upwards");}
else
     {JOptionPane.showMessageDialog(null,"incorrect entry");}
}
}                                                     

1 个答案:

答案 0 :(得分:0)

if else条件的正确方法如下

if (condition1 && condition2) {

} else if (condition3 && condition4) {

} else {

}

如果我的答案在下面是正确的,那么你的代码应如何编写如下;

int x = Integer.parseInt(jTextField1.getText().toString()); // getText needs to be converted to String before parsing it to int.

if (x>=120 && x<200) { //in the if condition you need two && not one &
    JOptionPane.showMessageDialog(null, "select drum : PPJ upwards");//semicolon should be here 
}//putting semicolon here is wrong

else if (x>=230 && x<=300)//putting semicolon here is also wrong
{
    JOptionPane.showMessageDialog(null,"select drum :RRf Upwards");
}
else {
    JOptionPane.showMessageDialog(null,"incorrect entry");
 }