繁体   English   中英

在 For 循环中插入 IF 语句

[英]insert IF statements inside For loop

我的问题是这样的,有N个人。 考虑到 N=9,我必须找到这些人的应税收入。 我已经为 1 名员工完成了数学计算,但是,将它应用于其他 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 的所得税:

您应该将input.nextInt()代码粘贴到 for 循环中。 并从int i,Tax = 0,N = 1;删除 N 个变量int i,Tax = 0,N = 1; 宣言。 因为你已经在 for 循环中声明了它。

解决方案:

public class IncomeTax {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int i,Tax = 0;
        for( int N=1; N<=9; N++ ){
        System.out.print("Enter the Taxable Income of the Employee " +N+":");
        i = input.nextInt();
        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);
        }    
    }
}

您绝对可以在 for 循环中使用 if 语句 - 您会收到错误,因为您在 for 循环中重新声明了变量N 我建议要么使用不同的可变字母,要么根本不在扫描仪下方声明它。

/**
 * @param args the command line arguments
 */

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    int i,Tax = 0;
    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);
    }    
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM