有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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的所得税:


共 (2) 个答案

  1. # 1 楼答案

    您应该将input.nextInt()代码粘贴到for循环中。并从int i,Tax = 0,N = 1;声明中删除N个变量。因为您已经在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);
            }    
        }
    }
    
  2. # 2 楼答案

    您完全可以在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);
        }    
    }