我是新来的,但让我们直接回答这个问题: 当我为类项目计算器编写以下代码时,我遇到了“令牌错误”。这是完整的错误消息:
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.
}
}
这是代码,我评论了令牌错误所在的区域。也许解决方案在我脸上。我希望我的代码是一个计算器,所以如果你们可以修复我的代码而不与数学运算冲突。谢谢,我感谢任何帮助。
答案 0 :(得分:18)
从此处的行尾删除;
:
if (option.equals("Multiplication"));
答案 1 :(得分:11)
问题是if
之后的分号。
语法为if
(
条件 )
表达式或阻止 {else
表达式或块式}
由于;
是一个空表达式,这适用于if子句,并且在then子句中没有任何反应 - 在你的情况下,语法上没有else子句,因为你的if语句语句以分号结尾并被跟随通过一个简单的块。然后,标记生成器检测到悬空else
标记并进行投诉。
答案 2 :(得分:7)
在错误发生之前的If语句末尾有一个分号。
if (option.equals("Multiplication"));
答案 3 :(得分:3)
更改
if (option.equals("Multiplication"));
到
if (option.equals("Multiplication"))
没有分号
答案 4 :(得分:1)
在语句if (option.equals("Multiplication"));
答案 5 :(得分:0)
更改:
if (option.equals("Multiplication"))
答案 6 :(得分:0)
不需要;在if条件结束时
如果(option.equals("乘法&#34))
答案 7 :(得分:-1)
从if语句中删除分号
if (option.equals("Multiplication"));
应该是
if (option.equals("Multiplication"))