代码无法正确识别数组中的素数

时间:2017-02-28 19:48:10

标签: java

这段代码应该首先运行一个获取数字因子的代码,然后运行一个代码来检查数组中的数字并删除任何素数。

import java.util.*;
public class ArrayListFunHouse {
Scanner keyboard = new Scanner(System.in);
private static int input;
private int keepOnlyCompositeNumbersSize;
private static ArrayList<Integer> onlyCompositeNumbers = new ArrayList<Integer>();
private static ArrayList<Integer> getListOfFactors = new ArrayList<Integer>();
private static ArrayList<Integer> keepOnlyCompositeNumbers = new ArrayList<Integer>();
private static ArrayList<Integer> getListOfFactorsComposite = new ArrayList<Integer>();
public void setInput() {
    System.out.print("Please input the input you want to check the factors of :: ");
    input = keyboard.nextInt();
    System.out.print("Please enter the size of the array you wish to find the composite numbers in :: ");
    keepOnlyCompositeNumbersSize = keyboard.nextInt();
    for(int spot = 0; spot < keepOnlyCompositeNumbersSize; spot++) {
        keepOnlyCompositeNumbers.add(keyboard.nextInt());
    }
}
public static ArrayList<Integer> getListOfFactors() {
        for(int i = 1; i <= input; i++) {
        if(input % i == 0 && i!= 1 && i != input) {
            getListOfFactors.add(i);
        }
    }
    return getListOfFactors;
}
public static ArrayList<Integer> getListOfFactorsCompositeList(int number) {
    for(int i = 1; i <= number; i++) {
        if(number % i == 0 && i!= 1 && i != number) {
            getListOfFactorsComposite.add(i);
        }
    }
    return getListOfFactorsComposite;
}
public static ArrayList<Integer> keepOnlyCompositeNumbers() {
    for(int i = 0; i < keepOnlyCompositeNumbers.size();i++) {
        if(getListOfFactorsCompositeList(keepOnlyCompositeNumbers.get(i)).isEmpty() == false) {
            onlyCompositeNumbers.add(keepOnlyCompositeNumbers.get(i));
        }
    }
    return onlyCompositeNumbers;
}
public static void print() {
    System.out.println(getListOfFactors);
    System.out.println("Original list");
    System.out.println(keepOnlyCompositeNumbers);
    System.out.println("Composite List");
    System.out.println(onlyCompositeNumbers);
    }
}

这是跑步者:

public class ArrayListFunHouseRunner {

public static void main(String[] args) {
    // TODO Auto-generated method stub
    ArrayListFunHouse A = new ArrayListFunHouse();
    A.setInput();
    ArrayListFunHouse.getListOfFactors();
    ArrayListFunHouse.keepOnlyCompositeNumbers();
    ArrayListFunHouse.print();
}

}

输入:

Array Size: 15
Numbers: 2 6 8 9 10 12 13 15 17 24 55 66 78 77 79

预期输出

[6, 8, 9, 10, 12, 15, 24, 55, 66, 78, 77]

实际输出

[6, 8, 9, 10, 12, 13, 15, 17, 24, 55, 66, 78, 77, 79]

2 个答案:

答案 0 :(得分:2)

更改

public static ArrayList<Integer> getListOfFactorsCompositeList(int number) {
    for(int i = 1; i <= number; i++) {
        if(number % i == 0 && i!= 1 && i != number) {
            getListOfFactorsComposite.add(i);
        }
    }
    return getListOfFactorsComposite;
}

public static ArrayList<Integer> getListOfFactorsCompositeList(int number) {
    getListOfFactorsComposite = new ArrayList<Integer>();
    for(int i = 1; i <= number; i++) {
        if(number % i == 0 && i!= 1 && i != number) {
            getListOfFactorsComposite.add(i);
        }
    }
    return getListOfFactorsComposite;
}

基本上,您只需在方法getListOfFactorsComposite = new ArrayList<Integer>();

的开头添加一行getListOfFactorsCompositeList

<强>原因

您已将getListOfFactorsComposite声明为List类型的实例字段。因此,在方法调用之间,它保留了在先前方法调用中添加的元素。在新用途之前,需要添加此行以重置列表。

答案 1 :(得分:0)

我认为您不了解如何传递参数以及如何使用对象。

以下代码有一个方法可以知道数字是否为素数,如果该方法返回true,那么我们不会将这些数字添加到新列表中。

它也使用内部类,我认为理解发生的事情比在代码中更清楚。

请注意,我删除了System.out.println("");来电,因此您的输入就像这样:

输入:

2 6 8 9 10 12 13 15 17 24 55 66 78 77 79

输出:

[6, 8, 9, 10, 12, 15, 24, 55, 66, 78, 77]

如您所见,输出正确且代码更短更清晰

代码:

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

public class FunHouseRunner {
    public static void main(String[] args) {
        FunHouse funHouse = new FunHouse();
        ArrayList<Integer> userInputList = funHouse.getUserInput(15);
        ArrayList<Integer> listWithoutPrimeNumbers = funHouse.getListWithoutPrimeNumbers(userInputList);
        System.out.println(listWithoutPrimeNumbers);
    }
}

class FunHouse {

    private Scanner sc = new Scanner(System.in);

    public boolean isPrime(int number) {
        for (int i = 2; i < number; i++) {
            if (number % i == 0) {
                return false;
            }
        }
        return true;
    }

    public ArrayList <Integer> getUserInput(int size) {
        ArrayList <Integer> userInputList = new ArrayList<Integer>();

        for (int i = 0; i < size; i++) {
            userInputList.add(sc.nextInt());
        }

        return userInputList;
    }

    public ArrayList <Integer> getListWithoutPrimeNumbers(ArrayList <Integer> userInputList) {
        ArrayList <Integer> listWithoutPrimeNumbers = new ArrayList<Integer>();

        for (int n : userInputList) {
            if (!isPrime(n)) {
                listWithoutPrimeNumbers.add(n);
            }
        }

        return listWithoutPrimeNumbers;
    }

    public void printList(ArrayList<Integer> listWithoutPrimeNumbers) {
        System.out.println(listWithoutPrimeNumbers);
    }
}