生成一个因子数组并打印它的内容

Generate a factor array and print it contents

如何创建一个包含整数数组的方法,然后允许该特定方法在从 main class 调用该方法时打印该数组的所有元素?

下面是我做的一个程序作为例子: 我试图找到 int 输入的所有可能因素。我使用了一种名为 generateFactors() 的方法来存储数字的所有可能因数,但由于某种原因我无法打印数组元素。在 toString() 方法中,出现 void type not allowed here!

错误

public class FactorNumbers {

    private int number;

    public FactorNumbers(int number) {
        this.number = number;
    }

    public int getNumber() {
        return number;
    }

    public void setNumber(int number) {
        this.number = number;
    }

    public void generateFactors() {
        int[] factors = new int[number];

        for (int i = 0; i < number; i++) {
            if ((i + 1 % number) == 0) {
                factors[i] = i;
                System.out.println("Factor= " + i);
            } else {
                factors[i] = 0;
            }
        }
        for (int i = 0; i < number; i++) {
            System.out.println(factors[i] + "");
        }
    }

    @Override
    public String toString() {
        return "The factors of " + number + "are: " + generateFactors();
    }

}

toString() 方法不适用于数组和 void 为 return 的方法。而是使用 Arrays.toString(array) :

public static void main(String[] args) {
    FactorNumbers fn = new FactorNumbers(100);
    int[] factors = fn.generateFactors();
    System.out.println(Arrays.toString(factors));
}

在您的 main 方法中打印数组中包含的所有这些值,并将您的 generateFactors() 方法重构为:

public int[] generateFactors() {
        int[] factors = new int[number];

        for (int i = 0; i < number; i++) {
            if ((i + 1 % number) == 0) {
                factors[i] = i;
                System.out.println("Factor= " + i);
            } else {
                factors[i] = 0;
            }
        }
        return factors;
    }

注意: 但是,您的代码不会给您任何因素,因为:

if ((i + 1 % number) == 0) 

将始终 return 错误。将其替换为:

if((number % i+1) == 0)

以获得正确的输出。

为了完成您想做的事情,代码必须如下所示:

    public String generateFactors() {
    String ret = "";
    int[] factors = new int[number];

    for (int i = 0; i < number; i++) {
        if ((i + 1 % number) == 0) {
            factors[i] = i;
            ret += "Factor= " + i + "\n";
        } else {
            factors[i] = 0;
        }
    }
    for (int i = 0; i < number; i++) {
        ret += factors[i] + "\n";
    }
    return ret;
}

但是,您的代码不会 return 这些因素,我认为您真正想要做的是这样的:

 public String generateFactors() {
    String ret = "";
    int[] factors = new int[number];

    for (int i = 0; i < number; i++) {
        //The remainder needs to be reversed
        if ((number % (i + 1)) == 0) {
            factors[i] = i+1;
        } else {
            factors[i] = 0;
        }
    }
    for (int i = 0; i < number; i++) {
        //only print them if they are a factor
        if(factors[i] != 0)
            ret += factors[i] + "\n";
    }
    return ret;
}

改变你的方法如下: 如果需要,您可以使用 stringbuilder。

 public String generateFactors() {
    int[] factors = new int[number];
    String strPrint = "";
    for (int i = 0; i < number; i++) {
        if ((i + 1 % number) == 0) {
            factors[i] = i;
            strPrint = strPrint + "Factor= " + i + "\n"
           //System.out.println("Factor= " + i);
        } else {
            factors[i] = 0;
        }
    }
    for (int i = 0; i < number; i++) {
        strPrint = strPrint + factors[i] + "\n"
       // System.out.println(factors[i] + "");
    }
   return strPrint;
}