为什么我会得到“非法的表达开始”在java? [英] Why am I getting "illegal start of expression" in java?

查看:251
本文介绍了为什么我会得到“非法的表达开始”在java?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是java的新手并试图建立一个计算器,因为教练给了我这个项目。

我在if语句中得到了非法的表达式错误启动。

以下是我的代码请帮我找到我的错。









  / *   
*要更改此许可证标题,请选择项目属性中的许可标头。
*要更改此模板文件,请选择工具|模板
*并在编辑器中打开模板。
* /

package 计算器;

import java.util.Scanner;

/ * *
*
* @author barnia data entry
* /

public class 计算器{

/ * *
* @param args命令行参数
* /

public static void main( String [] args){
// TODO代码应用程序逻辑
扫描仪scanner1 = 扫描仪(System.in);
System.out.println( 输入第一个数字);
int a = scanner1.nextInt();
扫描仪scanner2 = 扫描仪(System.in);
System.out.println( 输入第二个数字);
int b = scanner2.nextInt();
扫描仪操作= 扫描仪(System.in);
System.out.println( 输入操作符号);
String op = operation.next();
if (op == - ){
System.out.println(a-b);
}
else if (op = +){

System.out.println(a + b);
}
其他 如果(op == *){
的System.out.println(A * b);
}
其他 if (op == /){
的System.out.println(A / b);
}
else {
System.out.println( 操作错误);
}



}

}





我尝试过:



我正在尝试构建一个简单的计算器。

解决方案

根据这里发布的内容,您需要更新代码以使用.equals()测试值相等而字符( - + * /)是字符串类型,您需要更新代码用双引号包装它。见下文。



如何比较Java中的字符串? - 堆栈溢出 [ ^ ]



  public   static   void  main( String  args []){
扫描仪scanner1 = 扫描仪(System.in);
System.out.println( 输入第一个数字);
int a = scanner1.nextInt();
扫描仪scanner2 = 扫描仪(System.in);
System.out.println( 输入第二个数字);
int b = scanner2.nextInt();
扫描仪操作= 扫描仪(System.in);
System.out.println( 输入操作符号);
String op = operation.next();
if (op.equals( - )){
System.out.println(ab);
}
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( 操作错误);
}
}


您需要将用于比较op的每个值换行为双引号:



例如:

 如果(op == - )



必须是:

 如果(op ==    - 





告诉编译器你正在将op字符串变量与双引号字符串常量进行比较。



使用你的代码,编译器认为你试图从其他东西中减去某些东西 - 因此在减法表达式中就是非法表达式。



你需要修复所有这些比较语句。



比较字符串你不应该在Java中使用==。

相反你应该使用equals字符串对象的方法。



你的每个陈述应该如下所示(显然要替换你试图检查的角色):



  if (op.equals(   - ))





检查错误信息的重要性

另外,当您正在学习编码时,非常重要的是要仔细检查您得到的错误,然后在寻求帮助时,在向可能有帮助的其他人解释这些错误时重现这些错误。



这就是我的意思。我重现了你的错误,让我们仔细看看错误是什么样的:



 C:\dev\tools\JavaProjects>。 .\JavaJDK17 \bin\javac first.java 
first.java:4:错误:表达式的非法开始
if(op == - ){
^
1错误





第一行是我运行javac(java编译器)的地方。

第二行为您提供源文件(first.java)第4行中发生错误的确切行 - 请参阅:4?那意味着什么。

另请注意,有一个插入符号^指向编译器认为发生问题的位置。我们知道它就在附近,所以它确实开始帮助你猜测。



包括那种错误信息使得其他人更容易帮助你找到问题


您好!! 
你得到了这个错误,因为操作的验证是错误的:
  if(op == - ){
             System.out.println(a-b);
         }
        否则if(op = +){
        
         System.out.println(a + b);
         }
        否则if(op == *){
             System.out.println(a * b);
         }
        否则if(op == /){
             System.out.println(a / b);
         }

正确的表格应为:
  if(op == - ){
             System.out.println(a-b);
         }
        否则if(op =+){
        
         System.out.println(a + b);
         }
        否则if(op ==*){
             System.out.println(a * b);
         }
        否则if(op == /){
             System.out.println(a / b);
         }

另外,我建议使用开关来避免嵌套ifs和ifs的累积,而不是使用if。

问候


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


这篇关于为什么我会得到“非法的表达开始”在java?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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