当前位置: 首页 > 面试题库 >

如何解决Java中令牌“ else”上的“语法错误”?

梁鸣
2023-03-14
问题内容

我在这里很陌生,但让我们直接解决这个问题:当我为一个类项目计算器编写以下代码时,遇到了“令牌错误”。这是完整的错误消息

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
Syntax error on token "else", delete this token

at calculatorApplet.main(calculatorApplet.java:42)

我写了这段代码:

import java.util.Scanner;
import javax.swing.JOptionPane;
public class calculatorApplet
{
    Scanner sc = new Scanner(System.in);
    public static void main(String[] args)
    {
        JOptionPane.showMessageDialog(null, "Welcome to the Calculator!");
        String option = JOptionPane.showInputDialog(null, "Which calculator mode do you want?");
        if (option.equals("Addition"))
        {
            Double add1 = Double.parseDouble(JOptionPane.showInputDialog(null, "Okay type the first number(s) of your addition problem."));
            Double add2 = Double.parseDouble(JOptionPane.showInputDialog(null, "Now type the second number(s) of your addition problem."));
            Double preAdd = add1+add2;
            Double Add = preAdd;
            JOptionPane.showMessageDialog(null, "The sum is " + Add + ".");
        }
        else
        {
            JOptionPane.showMessageDialog(null, "Huh?");
        }
        if (option.equals("Subtraction"))
        {
            Double sub1 = Double.parseDouble(JOptionPane.showInputDialog(null, "Okay type the first number(s) of your subtraction problem."));
            Double sub2 = Double.parseDouble(JOptionPane.showInputDialog(null, "Now type the second number(s) of your subtraction problem."));
            Double preSub = sub1-sub2;
            Double Sub = preSub;
            JOptionPane.showMessageDialog(null, "The difference is " + Sub + ".");
        }
        else
        {
            JOptionPane.showMessageDialog(null, "Huh?");
        }
        if (option.equals("Multiplication"));
        {
            Double mult1 = Double.parseDouble(JOptionPane.showInputDialog(null, "Okay type the first number(s) of your multiplication problem."));
            Double mult2 = Double.parseDouble(JOptionPane.showInputDialog(null, "Now type the second number(s) of your multiplication problem."));
            Double preMult = mult1*mult2;
            Double Mult = preMult;
            JOptionPane.showMessageDialog(null, "The product is " + Mult + ".");
        }
        else //Here is the error.
        {
            JOptionPane.showMessageDialog(null, "Huh?");
        } //Here ends the error.
        if (option.equals("Division"))
        {
            Double div1 = Double.parseDouble(JOptionPane.showInputDialog(null, "Okay type the first number(s) of your division problem."));
            Double div2 = Double.parseDouble(JOptionPane.showInputDialog(null, "Now type the second number(s) of your division problem."));
            Double preDiv = div1/div2;
            Double Div = preDiv;
            JOptionPane.showMessageDialog(null, "The quotient is " + Div + ".");
        }
        else
        {
            JOptionPane.showMessageDialog(null, "Huh?");
        }
        //End of if statements.
    }

}

这就是代码,我注释了令牌错误所在的区域。也许解决方案就对了。我希望我的代码成为一个计算器,以便你们可以修正我的代码,使其不与数学运算冲突。谢谢,感谢您的帮助。


问题答案:

问题是分号之后的分号if

语法为if ( 条件 ) expression-or-block { else expression-or-block }

因为;是一个空表达式,所以它对if子句有效,而then子句中什么也没有发生-
在您的语法上,没有else子句,因为您的if语句语句以分号结尾并且后面是一个简单的块。然后分词器检测到悬挂的else令牌并抱怨。



 类似资料: