如何解决“令牌上的语法错误”,“否则”,“在Java? [英] How to resolve "Syntax error on token "else"" in Java?

查看:1435
本文介绍了如何解决“令牌上的语法错误”,“否则”,“在Java?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的,但让我们直接回答这个问题:
当我为类项目计算器编写以下代码时,我遇到了令牌错误。这是完整的错误消息:

I am new around here but let's get straight to the question: When I was writing the following code for a class project calculator I came across a "token error". This is the full error message:

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.
    } 

}

这是代码而我评论了令牌错误所在的区域。也许解决方案就在我面前。我希望我的代码成为一个计算器,所以如果你们可以修复我的代码而不与数学运算冲突。谢谢,我感谢任何帮助。

That's the code and I commented the area where the token error is. Maybe the solution is right in my face. I want my code to be a calculator so if you guys can fix my code to not conflict with the math operations. Thanks and I appreciate any help.

推荐答案

问题是之后的分号,如果

语法如果 条件 expression-or-block { else expression-or-block }

The grammar is if ( condition ) expression-or-block { else expression-or-block }

由于; 是一个空表达式,这适用于一个if子句并且在then子句中没有任何反应 - 在你的情况下,语法上没有else子句,因为你的if语句语句以分号结尾,后跟一个简单的块。然后,tokenizer会检测到悬挂的 else 令牌并投诉。

Since ; is an empty expression, this works for an if clause and nothing happens in the then clause - in your case syntactically there is no else clause, since your if statement statements ends with the semicolon and is followed by a simple block. Then the tokenizer detects a dangling else token and complains.

这篇关于如何解决“令牌上的语法错误”,“否则”,“在Java?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆