Java表达式非法开始
我已经进行了谷歌搜索,但无法弄清楚我的代码有什么问题。
我在第一个“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 技术交流群。

发布评论
评论(5)
死开点丶别碍眼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
裸钻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) {
}
}
}
~没有更多了~
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
您需要
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 ofelse 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 readincome == 0
.