为什么在 Java 中使用级联 ifs 时会出错?
Why do I get an error when I use cascading ifs in Java?
我有一个程序可以根据给定的收入将一个人的收入乘以一个系数。它还给出了一个代码 1 或 0,相应地定义了收入范围。
乍一看似乎是正确的,但我似乎得到了与我用来计算收入的方法中存在的 if 语句有关的错误。
代码如下:
import java.util.Scanner;
public class IncomeCalc{
public void Cal(double inc, int code)
{
if( code == 0 )
{
if((inc >= 0) && (inc <= 5,000))
{
System.out.println("Your tax declaration is "+ inc*0.1);
}else if((inc >= 5,001) && (inc <= 10,000))
{
System.out.println("Your tax declaration is "+ inc*0.15);
}else if((inc >= 10,001) && (inc <= 20,000))
{
System.out.println("Your tax declaration is "+ inc*0.25);
}else if(inc >= 20,001)
{
System.out.println("Your tax declaration is "+ inc*0.35);
}
}
else if( code == 1 )
{
if((inc >= 0) && (inc <= 10,000))
{
System.out.println("Your tax declaration is "+ inc*0.1);
}else if((inc >= 10,001) && (inc <= 20,000))
{
System.out.println("Your tax declaration is "+ inc*0.15);
}else if((inc >= 20,001) && (inc <= 30,000))
{
System.out.println("Your tax declaration is "+ inc*0.25);
}else if(inc >= 30,001)
{
System.out.println("Your tax declaration is "+ inc*0.35);
}
}
}
public static void main(String[] args)
{
Scanner obj = new Scanner(System.in);
System.out.println("Enter your annual income");
double income = obj.nextDouble();
Scanner obj2 = new Scanner(System.in);
System.out.println("If you are an individual choose 0 else choose 1 ");
int decl = obj2.nextInt();
Cal(income, decl);
}
}
谢谢!!!
你真的用逗号分隔数字吗?如果是这样,这是您的问题,也不要创建 2 个扫描仪对象,对两个操作使用相同的扫描仪对象。
我有一个程序可以根据给定的收入将一个人的收入乘以一个系数。它还给出了一个代码 1 或 0,相应地定义了收入范围。 乍一看似乎是正确的,但我似乎得到了与我用来计算收入的方法中存在的 if 语句有关的错误。
代码如下:
import java.util.Scanner;
public class IncomeCalc{
public void Cal(double inc, int code)
{
if( code == 0 )
{
if((inc >= 0) && (inc <= 5,000))
{
System.out.println("Your tax declaration is "+ inc*0.1);
}else if((inc >= 5,001) && (inc <= 10,000))
{
System.out.println("Your tax declaration is "+ inc*0.15);
}else if((inc >= 10,001) && (inc <= 20,000))
{
System.out.println("Your tax declaration is "+ inc*0.25);
}else if(inc >= 20,001)
{
System.out.println("Your tax declaration is "+ inc*0.35);
}
}
else if( code == 1 )
{
if((inc >= 0) && (inc <= 10,000))
{
System.out.println("Your tax declaration is "+ inc*0.1);
}else if((inc >= 10,001) && (inc <= 20,000))
{
System.out.println("Your tax declaration is "+ inc*0.15);
}else if((inc >= 20,001) && (inc <= 30,000))
{
System.out.println("Your tax declaration is "+ inc*0.25);
}else if(inc >= 30,001)
{
System.out.println("Your tax declaration is "+ inc*0.35);
}
}
}
public static void main(String[] args)
{
Scanner obj = new Scanner(System.in);
System.out.println("Enter your annual income");
double income = obj.nextDouble();
Scanner obj2 = new Scanner(System.in);
System.out.println("If you are an individual choose 0 else choose 1 ");
int decl = obj2.nextInt();
Cal(income, decl);
}
}
谢谢!!!
你真的用逗号分隔数字吗?如果是这样,这是您的问题,也不要创建 2 个扫描仪对象,对两个操作使用相同的扫描仪对象。