For循环中的java insert IF语句
我的问题如下,有N个人。考虑到N=9,我必须找到这些人的应税收入。我已经为一名员工做了计算,但将其应用于其他8人太多重复代码了。我可以把IF语句放在FOR循环中吗?我已经尝试过了,但它在FOR循环中显示了一个错误(即,变量N已经在方法main(String[])中定义)
public class IncomeTax {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int i,Tax = 0,N = 1;
System.out.print("Enter the Taxable Income of the Employee " +N+":");
i = input.nextInt();
for( int N=1 ;N<=9 ; N++ ){
if( i >= 0 & i<18200)
Tax = 0;
if( i >= 18201 & i<37000)
Tax = (int) (( i - 18200) * 0.19);
if( i >= 37001 & i<87000)
Tax = (int) ((( i - 37000) * 0.325)+3572);
if( i >= 87001 & i<180000)
Tax = (int) ((( i - 18200) * 0.37)+19822);
if( i >= 180001 )
Tax = (int) ((( i - 18200) * 0.45)+54097);
System.out.println("The Income Tax for the employee "+N+" is " + Tax);
}
}
}
产量应为N=9,员工数量和税收应依次为。 输入员工的应纳税所得额1: 员工1的所得税: 输入员工的应纳税所得额2: 员工2的所得税: 输入员工的应纳税所得额3: 员工3的所得税:
# 1 楼答案
您应该将
input.nextInt()
代码粘贴到for循环中。并从int i,Tax = 0,N = 1;
声明中删除N个变量。因为您已经在for循环中声明了它解决方案:
# 2 楼答案
您完全可以在for循环中使用if语句,因为您在for循环中声明变量
N
,所以会出现错误。我建议您要么使用不同的变量字母,要么不要在扫描仪下方声明它