创建“if-else”语句时出现问题

时间:2015-02-25 04:58:59

标签: if-statement

嗨,我对Java很陌生我在尝试" if-else"时遇到了问题。声明。我得到语法错误,我不知道为什么。感谢您的投入。

 import javax.swing.JOptionPane;

public class MagicDate {

    public static void main(String [] args){

        int month;
        int day;
        int year;
        String input;
        String message = ("This program will find a secret date");

        JOptionPane.showMessageDialog(null, message);

        input = JOptionPane.showInputDialog( " Enter a month of the in numeric "
                                            + "form");
        month= Integer.parseInt(input);

        input = JOptionPane.showInputDialog( " Enter a day of the in numeric"
                                            + " form");
        day= Integer.parseInt(input);

        input = JOptionPane.showInputDialog( " Enter a two digit year");
        year = Integer.parseInt(input);

        if(month * day == year);
        {
            JOptionPane.showMessageDialog(null, "The Date is Magic!");
        }
        else 
        {
            (month * day != year);
            JOptionPane.showMessageDialog(null, "The Date is not Magic");
        }   
        System.exit(0);

    }
}

3 个答案:

答案 0 :(得分:2)

您应该删除包含if语句的行末尾的分号。基本上,分号的效果是添加一个空语句。所以你的代码对于编译器来说是这样的:

If (...)
   Do nothing;
// can't have an brace here since the if statement has already terminated.
{
   ...
}

答案 1 :(得分:0)

删除分号:

if(month * day == year)

答案 2 :(得分:0)

在条件

之后删除;
if(month * day == year); // remove ;

并且缺少if

else 
{
    (month * day != year); // add if before line and remove ;
    JOptionPane.showMessageDialog(null, "The Date is not Magic");
}