Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在For循环中插入IF语句_Java_Loops - Fatal编程技术网

Java 在For循环中插入IF语句

Java 在For循环中插入IF语句,java,loops,Java,Loops,我的问题如下,有N个人。考虑到N=9,我必须找到这些人的应税收入。我已经为一名员工做了计算,但将其应用于其他8人太多重复代码了。我可以把IF语句放在FOR循环中吗?我已经尝试过了,但它在FOR循环中显示了一个错误(即,变量N已经在方法main(String[])中定义) 公共类IncomeTax{ /** *@param指定命令行参数 */ 公共静态void main(字符串[]args){ 扫描仪输入=新扫描仪(System.in); int i,Tax=0,N=1; 系统输出打印(“输入员工

我的问题如下,有N个人。考虑到N=9,我必须找到这些人的应税收入。我已经为一名员工做了计算,但将其应用于其他8人太多重复代码了。我可以把IF语句放在FOR循环中吗?我已经尝试过了,但它在FOR循环中显示了一个错误(即,变量N已经在方法main(String[])中定义)

公共类IncomeTax{
/**
*@param指定命令行参数
*/
公共静态void main(字符串[]args){
扫描仪输入=新扫描仪(System.in);
int i,Tax=0,N=1;
系统输出打印(“输入员工的应纳税所得额”+N+“:”);
i=input.nextInt();
对于(int N=1;N=0&i=18201&i=37001&i=87001&i=180001)
税项=(整数)((i-18200)*0.45)+54097);
System.out.println(“员工的所得税”+N+“是”+Tax);
}    
}
}
产量应为N=9,员工数量和税收应依次为。 输入员工的应纳税所得额1: 员工1的所得税: 输入员工的应纳税所得额2: 员工2的所得税: 输入员工的应纳税所得额3:
员工3的所得税:

您应该在for循环中粘贴
input.nextInt()
code。从
inti中删除N个变量,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);
        }    
    }
}
公共类IncomeTax{
公共静态void main(字符串[]args){
扫描仪输入=新扫描仪(System.in);
int i,税=0;
对于(int N=1;N=0&i=18201&i=37001&i=87001&i=180001)
税项=(整数)((i-18200)*0.45)+54097);
System.out.println(“员工的所得税”+N+“是”+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);
    }    
}
/**
*@param指定命令行参数
*/
公共静态void main(字符串[]args){
扫描仪输入=新扫描仪(System.in);
int i,税=0;
系统输出打印(“输入员工的应纳税所得额”+N+“:”);
i=input.nextInt();
对于(int N=1;N=0&i=18201&i=37001&i=87001&i=180001)
税项=(整数)((i-18200)*0.45)+54097);
System.out.println(“员工的所得税”+N+“是”+Tax);
}    
}

我想您希望“print”和“input.nextInt”就在for循环中,从第二个if开始,使用
else if
询问这9位员工的收入。然后,您可以删除一个已经定义了N的条件(但首先添加一个“<0”测试)。一旦在这一行
inti,Tax=0,N=1并且一旦进入for循环
for(int N=1;Nalso),最好将for循环中的所有if移到一个私有方法,并在循环中调用它。此外,除非变量是最终变量,否则不要将其大写…还试着使用更好的变量名,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);
    }    
}