java非法表达的开始,为什么我会得到“非法的表达开始”在java?

I am new to java and trying to build a calculator as the instructor gave me the project.

I am getting the illegal start of expression error in else if statement.

below is my code please help me to find my fault./** To change this license header, choose License Headers in Project Properties.

* To change this template file, choose Tools | Templates

* and open the template in the editor.

*/

package calculators;

import java.util.Scanner;

/**

*

* @author barnia data entry

*/

public class Calculators {

/**

* @param args the command line arguments

*/

public static void main(String[] args) {

//TODO code application logic here

Scanner scanner1 = new Scanner(System.in);

System.out.println("enter 1st number");

int a = scanner1.nextInt();

Scanner scanner2 = new Scanner(System.in);

System.out.println("enter 2nd number");

int b = scanner2.nextInt();

Scanner operation = new Scanner(System.in);

System.out.println("enter operation sign");

String op = operation.next();

if (op == -){

System.out.println(a-b);

}

else if (op = +){

System.out.println(a+b);

}

else if (op == *){

System.out.println(a*b);

}

else if (op == /){

System.out.println(a/b);

}

else {

System.out.println("error in operation");

}

}

}

What I have tried:

I am trying to build a simple calculator.

解决方案

Based on what posted here, you need to update the code to use .equals() tests for value equality and the character (-+*/) are string type, you need to update the code to wrap it with double quote. See below.

How do I compare strings in Java? - Stack Overflow[^]

public static void main(String args[]) {

Scanner scanner1 = new Scanner(System.in);

System.out.println("enter 1st number");

int a = scanner1.nextInt();

Scanner scanner2 = new Scanner(System.in);

System.out.println("enter 2nd number");

int b = scanner2.nextInt();

Scanner operation = new Scanner(System.in);

System.out.println("enter operation sign");

String op = operation.next();

if (op.equals("-") ){

System.out.println(a-b);

}

else if (op.equals("+")){

System.out.println(a+b);

}

else if (op.equals("*")){

System.out.println(a*b);

}

else if (op.equals("/")){

System.out.println(a/b);

}

else {

System.out.println("error in operation");

}

}

You need to wrap each of the values you are comparing op to in double-quotes:

For example:

if (op == -)

has to be:

if (op == "-")

That tells the compiler that you are comparing the op string variable to the double-quoted string constant.

With your code, the compiler thinks you are attempting to subtract something from something else -- thus the "illegal start of expression" as in a subtraction expression.

You'll need to fix all of those comparison statements.

And to compare strings you should not use the == in Java.

Instead you should use the equals method of the string object.

Each of your statements should look like the following (obviously replacing the character you are attempting to check for):

if (op.equals("-"))

The Importance of Examining Error Messages

Also as you're learning to code it is really important to examine the errors you get very closely and then when you are looking for help to reproduce those exact errors when explaining them to others who may be of help.

Here's what I mean. I reproduced your error and let's look closely at what the error looks like:

C:\dev\tools\JavaProjects>..\JavaJDK17\bin\javac first.java

first.java:4: error: illegal start of expression

if (op == -){

^

1 error

the first line is where I ran the javac (java compiler).

The second line gives you the exact line where the error occurred in the source file (first.java) line 4 -- see the :4? That's what that means.

Also notice there is a caret ^ pointing up at the location where the compiler thinks the problem occurred. We know it is near there so it does begin to help you guess.

Including that kind of error information makes it far easier for someone else to help you find the problem.

Hello!!

you get that error because the validations of the operations are wrong:

if (op == -) {

System.out.println (a-b);

}

else if (op = +) {

System.out.println (a + b);

}

else if (op == *) {

System.out.println (a * b);

}

else if (op == /) {

System.out.println (a / b);

}

The correct form should be:

if (op == "-") {

System.out.println (a-b);

}

else if (op = "+") {

System.out.println (a + b);

}

else if (op == "*") {

System.out.println (a * b);

}

else if (op == /) {

System.out.println (a / b);

}

Also, I suggest that instead of using if, you use a switch to avoid the accumulation of nested ifs and else ifs.

regards

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值