-1

我用java做了一个分解程序。我在 Eclipse 中有一个带有 2 个包的项目。一个只有一个类,我遇到问题的那个,另一个包里面什么都没有。当我单击运行时,什么也没有发生,我不知道为什么。

这是java代码:

    package com.frostbytedev.factor;

import java.util.ArrayList;
import java.util.Scanner;

public class factor {
    static ArrayList<Integer> notFactors = new ArrayList<Integer>();
    static ArrayList<Integer> factors = new ArrayList<Integer>();
    static int i = 0;

    /**
     * @param args
     */
    public static void main(String[] args){
        // TODO Auto-generated method stub
            Initialize();
    }

    private static void Initialize() {
        // TODO Auto-generated method stub
        System.out.println("Enter a number to factor below:");
        Scanner in = new Scanner(System.in);
        int number = in.nextInt();
        FactorNumber(number);
        PrintList(Factors);
    }

    private static void PrintList(ArrayList<Integer> factorsToPrint) {
        // TODO Auto-generated method stub
        for(i=0;i<factorsToPrint.size()-1;i++){
            System.out.print(factorsToPrint.get(i)+", ");
        }
            System.out.print(factorsToPrint.size());

    }

    private static void FactorNumber(int number) {
        // TODO Auto-generated method stub
        for(i=2;i<number/2;i++){
            if(number%i==0){
                FactorNumber(i);
                FactorNumber(number/i);
            } else {
                notFactors.add(i);
            }
        }
        if(notFactors.size()==number){
            factors.add(number);
        }
    };

}
4

2 回答 2

1

尝试通过右键单击项目资源管理器中的类文件 -> 运行方式... -> Java 应用程序来运行您的类

于 2013-07-06T15:24:29.300 回答
0

代码中有以下错误 1)由于我是静态的,您的代码中有一个无限循环。2) 因子应该是一个集合以避免重复 3)if (notFactors.size() == number / 2 - 1) as loop 从 2 开始 4)for (int i = 2; i <= number / 2; i++ ) 它应该是 <=

这不会打印 1 和数字本身,它们也是因素。如果您需要它们,您可以添加到集合中。

package com.frostbytedev.factor;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;

public class Test {
static ArrayList<Integer> notFactors = new ArrayList<Integer>();
static Set<Integer> factors = new HashSet<Integer>();

/**
 * @param args
 */
public static void main(String[] args) {
    // TODO Auto-generated method stub
    Initialize();
}

private static void Initialize() {
    // TODO Auto-generated method stub
    System.out.println("Enter a number to factor below:");
    Scanner in = new Scanner(System.in);
    int number = Integer.parseInt(in.nextLine());
    in.close();
    FactorNumber(number);
    PrintList(factors);
}

static void PrintList(Set<Integer> factorsToPrint) {
    // TODO Auto-generated method stub
    for (int factor : factorsToPrint) {
        System.out.print(factor + ", ");
    }
    System.out.println(factorsToPrint.size());

}

private static void FactorNumber(int number) {
    // TODO Auto-generated method stub
    for (int i = 2; i <= number / 2; i++) {
        System.out.println(i);
        if (number % i == 0) {
            factors.add(i);
            FactorNumber(i);
            FactorNumber(number / i);
        } else {
            notFactors.add(i);
        }
    }
    if (notFactors.size() == number / 2 - 1) {
        factors.add(number);
    }
};

}

于 2013-07-06T16:53:12.367 回答