检查整数是否是其素因子的乘积

时间:2015-11-15 20:44:05

标签: java

我写了一个带正整数的程序,如果所有主要因素(不包括1和数字本身)的乘积等于其值,则打印“我的除数的乘积”,否则打印“不是我的乘积”除数”。

public static void main(String[] args) 
{
    Scanner scan = new Scanner(System.in);

    int x = scan.nextInt();
    int product = 1;
    int count = 0;
    for(int i = 2; i < x; i++)
    {
        if(x % i == 0)
        {   for(int j = 1; j <= i; j++)
                if(i%j == 0)
                    count++;
            if(count == 2)
                product *= i;
        }
    }

    if(product == x)
        System.out.println("Product of my divisors");
    else
        System.out.println("Not the product of my divisors");       
}

我似乎无法找到此代码的错误!整数10应该可以工作但它一直给我“不是我的除数的产物”。

1 个答案:

答案 0 :(得分:2)

这是因为你永远不会重置==> D:\bat\SO\33721424.bat Folder Name: Enter File Type: Copy or Move [C,M]?C DIR: "01exclam!ation_1.txt" "02exc!lam!ation_1.txt" "11per%cent_1.txt" "12per%cent%_1.txt" "13per%OS%cent_1.txt" "14per%%OS%%cent_1.txt" "15per%3cent_1.txt" "16per%%3cent_1.txt" "17per%Gcent_1.txt" "18per%%Gcent_1.txt" "21ampers&nd_1.txt" "21ampers^&nd_1.txt" "22ampers&&nd_1.txt" "22ampers^&^&nd_1.txt" "31(left_parenthesis_1.txt" "32(both)parentheses_1.txt" "32_rght)parenthesis_1.txt" "41a~tilde_1.txt" "51a^caret_1.txt" "AÄaá^ cč^DĎ(1!)&°~!.foo~bar_1.txt" "AÄzž^ yýS%OS%Š%%OS%%(%1!)&°~%%G!^%~2.foo~bar_1.txt" "BÄaá^ cčD%OS%Ď%%OS%%(%1!)&°~%%G!^%~2.foo~bar_1.txt" "elpfile_2.txt" "new - 01exclam!ation_1.txt" "scripts‹lpfile.txt" "Unicode the flag or check_1.txt" "Unicode the ⚑ or ✔ char_1.txt" "elpfile_3.txt" ==> count应该在count块内声明。正确的版本是

if