Java表达式非法开始

发布于 12-29 00:01 字数 815 浏览 4 评论 0原文

我已经进行了谷歌搜索,但无法弄清楚我的代码有什么问题。

我在第一个“Else if”处的 if 语句中遇到表达式开始非法错误

我对 Java 还很陌生,但试图快速学习,因为这是我的计算课程的要求我希望能做到。

非常感谢任何帮助。

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                

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                

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

深海少女心2025-01-05 00:01:59

您需要 else if (venue >=9000 && Income <=21000) 而不是 else if (venue >=9000 & <=21000) >。

对于其他 else if 表达式也是如此。

您必须对收入进行两次单独的比较。你不能说“如果收入大于或等于9000或小于或等于21000”。您必须说“如果收入大于 9000 或收入小于或等于 21000”。

& 是按位与。 && 是逻辑与。

正如其他人所述,收入= 0应该读取收入== 0

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.

-黛色若梦2025-01-05 00:01:59

你需要 &&不是 &,并重复变量“收入”:

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

您还使用 =,您可能的意思是 ==

if(income == 0)

= 分配一个值,== 测试两个值是否相等。

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.

死开点丶别碍眼2025-01-05 00:01:59

您的 if/else if 块表达式的结果应始终为布尔值(true/false)。在 Java 中,&&代表AND,&是操作位的按位运算符。

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

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
靖瑶2025-01-05 00:01:59

if(收入 = 0)

应该是

if(收入 == 0)

else if (收入 >=9000 & <=21000) code>

(及其关注者)应该是

else if (venue >=9000 && Income <=21000)

并且看起来您需要做一些你的变量是 double 而不是 int 。

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.

裸钻2025-01-05 00:01:59

检查“=”、“==”、“&”、“&&”的用法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) {
    }
  }
}

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) {
    }
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文