Java正在尝试if if语句不是真的

时间:2014-07-25 15:10:16

标签: java button if-statement jtextfield void

我正在尝试执行一个程序,如果两个TextField s等于某个值,那么会发生两个if语句。当第一个if语句发生时,第二个if语句发生但我在代码中设置的值不匹配。为了更好地解释,这里是代码:

private void LevelUp(){
        float level = (float) Double.parseDouble(lvl.getText());
        float expmn = (float) Double.parseDouble(ExpMin.getText());
                    if(level == 1  && expmn == 500){
                    lvl.setText("2");
                    ExpMin.setText("501");
                    ExpMax.setText("1000");
                    JOptionPane.showMessageDialog(null,"" + "levevlup!" ,"Para Yok",JOptionPane.PLAIN_MESSAGE);
                    if(level == 2 && expmn == 1000); 
                        lvl.setText("3");
                        ExpMin.setText("1001");
                        ExpMax.setText("2500");

        }
        else{
            JOptionPane.showMessageDialog(null,"" + "leveladssadaup" ,"Para Yok",JOptionPane.PLAIN_MESSAGE);
        }
    }
}

3 个答案:

答案 0 :(得分:6)

你不仅应该在if-statement之后删除;,而是在你查找level2,但是如果我正确查看你的代码,你应该在请求level2之前关闭你的if语句如果是level2则使用else。

如果出现以下情况,请在level2之后选择花括号:

    if(level == 1  && expmn == 500){
                lvl.setText("2");
                ExpMin.setText("501");
                ExpMax.setText("1000");
                JOptionPane.showMessageDialog(null,"" + "levevlup!" ,"Para Yok",JOptionPane.PLAIN_MESSAGE);
    }// <-- close level1 here because you want to check for a new level afterwards
    else if(level == 2 && expmn == 1000){ //<-- here was your ;, which was wrong
                    lvl.setText("3");
                    ExpMin.setText("1001");
                    ExpMax.setText("2500");

    }
    else{
        JOptionPane.showMessageDialog(null,"" + "leveladssadaup" ,"Para Yok",JOptionPane.PLAIN_MESSAGE);
    }

答案 1 :(得分:0)

中删除;
 if(level == 2 && expmn == 1000); 

并添加{

 if(level == 2 && expmn == 1000) {

您还需要向此}语句

添加结束if

答案 2 :(得分:0)

试试这个:

private void LevelUp(){
    float level = (float) Double.parseDouble(lvl.getText());
    float expmn = (float) Double.parseDouble(ExpMin.getText());
    if(level == 1  && expmn == 500){
        lvl.setText("2");
        ExpMin.setText("501");
        ExpMax.setText("1000");
        JOptionPane.showMessageDialog(null,"" + "levevlup!" ,"Para Yok",JOptionPane.PLAIN_MESSAGE);


    }
    else{
    if(level == 2 && expmn == 1000)
    {
            lvl.setText("3");
            ExpMin.setText("1001");
            ExpMax.setText("2500");
    }
    else
            JOptionPane.showMessageDialog(null,"" + "leveladssadaup" ,"Para Yok",JOptionPane.PLAIN_MESSAGE);
    }
    }
}