Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/google-maps/4.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循环需要帮助吗_Java - Fatal编程技术网

Java 运行此for循环需要帮助吗

Java 运行此for循环需要帮助吗,java,Java,请尽可能简单地解释,因为我是初学者。 接收整数n并返回大于的最小整数 n,其中数字之和可被11整除。 例如,nextCRC(100)=119,因为119是大于的第一个整数 100,其数字1+1+9=11之和可被11整除 1) 我不能理解的第一件事是,为什么在开始时for循环中有一个“true” 2) 如何计算下一个大于11且可被11整除的数字 3) 对于负数,如果负数小于0,该如何执行此操作 public static int nextCRC(int n) { try {

请尽可能简单地解释,因为我是初学者。 接收整数n并返回大于的最小整数 n,其中数字之和可被11整除。 例如,nextCRC(100)=119,因为119是大于的第一个整数 100,其数字1+1+9=11之和可被11整除

1) 我不能理解的第一件事是,为什么在开始时for循环中有一个“true”

2) 如何计算下一个大于11且可被11整除的数字

3) 对于负数,如果负数小于0,该如何执行此操作

public static int nextCRC(int n)
{
    try
    {
        **for (int i = n + 1; true; i++)**
        {
            String number = String.valueOf(Math.abs(i));

            int count = 0;
            for (int j = 0; j < number.length(); j++)
            {
                count += Integer.parseInt(String.valueOf(number.charAt(j)));
            }

            if (count > 0 && count % 11 == 0) return  i;
        }
    }
    catch (Exception e)
    {
        return 0;
    }
}

public static void main(String[] args)
{
    System.out.println(nextCRC(100));
    System.out.println(nextCRC(-100));
}
publicstaticintnextcrc(intn)
{
尝试
{
**for(int i=n+1;true;i++)**
{
字符串编号=String.valueOf(Math.abs(i));
整数计数=0;
对于(int j=0;j0&&count%11==0)返回i;
}
}
捕获(例外e)
{
返回0;
}
}
公共静态void main(字符串[]args)
{
系统输出println(nextCRC(100));
System.out.println(nextCRC(-100));
}

}

我在代码中添加了注释,解释代码的每个部分的功能:

public static int nextCRC(int n)
{
try
{
    for (int i = n + 1; true; i++) // the for loop will loop as long as the boolean is true. i starts at the inputted value and increments by 1 each iteration
//Since the value is "true" it will loop forever or until a value is returned
    {
        String number = String.valueOf(Math.abs(i)); // convert the number to string

        int count = 0;
        for (int j = 0; j < number.length(); j++) //iterate through each digit in the number
        {
            count += Integer.parseInt(String.valueOf(number.charAt(j))); // add the value of each digit to cout variable
        }

        if (count > 0 && count % 11 == 0) return  i; //return the number that was checked if the sum of its digits is divisible by 11
    }
}
catch (Exception e) // return a value of 0 if there is an exception
{
    return 0;
}
}

public static void main(String[] args)
{
System.out.println(nextCRC(100));
System.out.println(nextCRC(-100));
}
}
publicstaticintnextcrc(intn)
{
尝试
{
for(int i=n+1;true;i++)//只要布尔值为true,for循环就会循环。i从输入值开始,每次迭代递增1
//由于该值为“true”,它将永远循环或直到返回值为止
{
String number=String.valueOf(Math.abs(i));//将数字转换为字符串
整数计数=0;
for(int j=0;j0&&count%11==0)返回i;//如果其数字之和可被11整除,则返回已检查的数字
}
}
catch(异常e)//如果存在异常,则返回值0
{
返回0;
}
}
公共静态void main(字符串[]args)
{
系统输出println(nextCRC(100));
System.out.println(nextCRC(-100));
}
}
1)true的意思是“永不中断”。分号之间的表达式在每次循环迭代之前进行求值,如果为false,则循环中断,执行移动到循环之后的任何位置。实际上这是不必要的:
for(inti=n+1;;i++)
也会这样做。2)不需要转换为字符串并返回求和。在SO(和其他地方)上有很多问题可以说明如何做得更好。