3

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

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

}

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

4

8 回答 8

18

;从此处的行尾删除:

if (option.equals("Multiplication"));

于 2013-07-24T12:19:39.383 回答
15

问题是 . 后面的分号if

语法是if ( 条件 ) 表达式或块{else 表达式或块}

由于;是一个空表达式,这适用于 if 子句,并且 then 子句中没有任何事情发生 - 在您的情况下,语法上没有 else 子句,因为您的 if 语句语句以分号结尾,后面跟着一个简单的块。然后分词器检测到一个悬空的else令牌并抱怨。

于 2013-07-24T12:23:00.113 回答
7

在发生错误的 If 语句的末尾有一个分号。

if (option.equals("Multiplication"));
于 2013-07-24T12:20:29.167 回答
3

改变

if (option.equals("Multiplication"));

if (option.equals("Multiplication"))

没有分号

于 2013-07-24T12:20:15.903 回答
1

删除语句末尾的分号 (;)if (option.equals("Multiplication"));

于 2013-07-24T12:22:51.747 回答
0

改变:

if (option.equals("Multiplication"))
于 2013-07-24T12:24:27.277 回答
0

不需要;在 if 条件结束时

if(option.equals("乘法"))

于 2013-07-24T12:30:40.747 回答
-1

从 if 语句中删除分号

if (option.equals("Multiplication"));

应该

if (option.equals("Multiplication"))
于 2017-03-12T09:39:05.547 回答