Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/311.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 在不重复数字的情况下检查一系列数字的可除性_Java - Fatal编程技术网

Java 在不重复数字的情况下检查一系列数字的可除性

Java 在不重复数字的情况下检查一系列数字的可除性,java,Java,我编写了以下程序来计算可被12整除但不包含重复数字的数字。数字144不被考虑,即使它可以被12整除,因为存在数字4的重复。问题是我没有得到任何输出。我尝试将for循环的范围从12…54321更改为12…1000,甚至12…24。我仍然没有输出。我似乎找不到问题所在。有人能告诉我我的代码有什么问题吗。或者只是建议我一个更好的代码/解决方案。谢谢 class mod12 { public static void main(String[] args) { int co

我编写了以下程序来计算可被12整除但不包含重复数字的数字。数字144不被考虑,即使它可以被12整除,因为存在数字4的重复。问题是我没有得到任何输出。我尝试将for循环的范围从12…54321更改为12…1000,甚至12…24。我仍然没有输出。我似乎找不到问题所在。有人能告诉我我的代码有什么问题吗。或者只是建议我一个更好的代码/解决方案。谢谢

class mod12
{
    public static void main(String[] args)
    {
        int count=0;
        for(int num=12;num<=54321;num+=12)
        {
            boolean repetition=false;
            int digits[]=new int[10];
            int length=0;
            while (num!=0)
            {
                digits[length++]=num%10;
                num=num/10;
            }
            for(int i=0; i<length; i++)
            {
                for(int j=0; j<length; j++)
                {
                    if(i!=j)
                    {
                        if(digits[i]==digits[j])
                        {
                            repetition=true;
                            break;
                        }
                    }
                }
            }

            if(repetition==false)
            {count++;}
        }
        System.out.println("There are "+count+" numbers satisfying the condition");
    }
}
class mod12
{
公共静态void main(字符串[]args)
{
整数计数=0;

对于(int num=12;num,在while循环中减少
num
,有效地将
for
循环向后移动,因此它永远不会完成。改用临时变量可以解决问题:

public static void main(String[] args)
{
    int count=0;
    for(int num=12;num<=54321;num+=12)
    {
        boolean repetition=false;
        int digits[]=new int[10];
        int length=0;
        int tempNum = num; // Here
        while (tempNum !=0)
        {
            digits[length++]=tempNum %10;
            tempNum =tempNum /10;
        }
        for(int i=0; i<length; i++)
        {
            for(int j=0; j<length; j++)
            {
                if(i!=j)
                {
                    if(digits[i]==digits[j])
                    {
                        repetition=true;
                        break;
                    }
                }
            }
        }

        if(repetition==false)
        {count++;}
    }
    System.out.println("There are "+count+" numbers satisfying the condition");
}

非常感谢。这是一个愚蠢的错误,但却让我发疯。谢谢你的快速回复。。。
public static void main(String[] args) {
    int count = 0;
    NUMBERS:
    for (int num = 12; num <= 54321; num += 12) {
        Set<Integer> digits = new HashSet<>();
        int tempNum = num;
        while (tempNum != 0) {
            int digit = tempNum % 10;
            if (!digits.add(digit)) {
                continue NUMBERS;
            }
            tempNum = tempNum / 10;
        }
        ++count;
    }
    System.out.println("There are "+count+" numbers satisfying the condition");
}