Java illegal start of expression

南笙酒味 提交于 2021-02-16 20:01:25

问题


I have done a google around but can't get my head around what's wrong with my code here.

I'm getting an Illegal start of expression error in the if statement at the first 'Else if'

I'm pretty new to Java but trying to quickly learn as its a requirement of my computing course I'm hoping to do.

Any help greatly appreciated.

import java.util.Scanner;

public class net_salary{

public static void main(String[]args){    
    Scanner in = new Scanner(System.in);    

    int income;
    int incometax;
    int afterincometax;
    int nationalinsurance;
    int afterni;
    int pension;

    income = in.nextInt();       

    if(income = 0) {
        incometax = 0;
    } else if (income >=9000 & <=21000) {
        incometax = income * 0.15;
    } else if (income >=21000 & <=31000) {
        incometax = income * 0.2;       
    } else if (income >=31000 & <=50000) {
        incometax = income * 0.225                

回答1:


You need else if (income >=9000 && income <=21000) instead of else if (income >=9000 & <=21000).

Similarly for the other else if expressions.

You have to make two separate comparisons to income. You can't say "if income is greater than or equal to 9000 or less than or equal to 21000". You must say "if income is greater than 9000 or income is less than or equal to 21000".

& is a bitwise AND. && is logical AND.

As stated by others, income = 0 should read income == 0.




回答2:


You need && not &, and repeat the variable 'income' :

else if (income >=9000 && income <=21000)

You are also using = where you probably mean ==

if(income == 0)

= assigns a value, == tests whether two values are equal.




回答3:


Your if/else if block expression always should results as boolean (either true/false). In Java, && represents AND, & is bitwise operator which manipulates bits.

if(income == 0)
    {
        incometax = 0;

    }
**else if (income >=9000 && income <=21000)**
    {
        incometax = income * 0.15;
    }

else if (income >=21000 && income <=31000)
    {
        incometax = income * 0.2;

    }

else if (income >=31000 && income <=50000)
    {
        incometax = income * 0.225



回答4:


if(income = 0)

should be

if(income == 0)

and

else if (income >=9000 & <=21000)

(and it's followers) should be

else if (income >=9000 && income <=21000)

and looks like you you need to make some of your variables double instead of int.




回答5:


Check the usage of "=", "==", "&", "&&" operators in Java.

public class net_salary{
  public static void main(String[]args){
    int income, ncometax, afterincometax, nationalinsurance, afterni, pension; 
    Scanner in = new Scanner(System.in);   
    income = in.nextInt();

    if(income == 0) {
       incometax = 0;
    } else if (income >= 9000 && income <= 21000) {
       incometax = income * 0.15;
    } else if (income >=21000 && income <= 31000) {
       incometax = income * 0.2;
    } else if (income >=31000 && income <= 50000) {
    }
  }
}


来源:https://stackoverflow.com/questions/9026044/java-illegal-start-of-expression

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!