猿问

需要帮助找出我的代码中的错误

所以我需要帮助弄清楚为什么我的代码不包括数字 2 而它在主要印刷行上包括数字 99。我需要更改 findPrime() 上的某些内容吗?我尝试使用索引,结果变得更糟。


    class Sieve {

    private int max;

    private boolean[] numbers;


    public Sieve(int max) {

        if (max < 2) {

            throw new IllegalArgumentException();

        }


        this.max = max;

        numbers = new boolean[max];

        numbers[0] = false;

        numbers[1] = false;

        numbers[2] = true;

        for (int i = 2; i < max-1; i++) {

        numbers[i] = true;


        }

    }

    public void findPrimes() {

        for (int num = 2; num < max-1; num++) {

            int multiples = num + num;

            while (multiples < max-1) {

                numbers[multiples-1] = false;

                multiples += num;

            }

        }

    }

    @Override

    public String toString() {

        StringBuilder builder = new StringBuilder();

        for (int num = 2; num < max; num++) {

            if (numbers[num]) {

                builder.append(num+1).append(" ");

            }

        }

        return builder.toString();

        }

    }


class Driver

{


//  MAIN. Find some primes.


  public static void main(String [] args)

  {

    Sieve sieve = null;  //  We must initialize SIEVE or Java will cry.


//  5 points. This must print "Sieve size must be at least 2." but without the

//  quotes.


    try

    {

      sieve = new Sieve(0);

    }

    catch (IllegalArgumentException oops)

    {

      System.out.println("Sieve size must be at least 2.");

    }


//  5 points. This must print nothing.


    try

    {

      sieve = new Sieve(100);

    }

    catch (IllegalArgumentException oops)

    {

      System.out.println("Sieve size must be at least 2.");

    }


//  10 points. This must print integers from 2 to 99, separated by blanks.


    System.out.println(sieve);


//  10 points. This must print the prime numbers between 2 and 99, separated by

//  blanks. They are:

//

//  2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97


    sieve.findPrimes();

    System.out.println(sieve);

  }

}



慕哥6287543
浏览 142回答 2
2回答

心有法竹

您的toString()方法从 num = 2 开始循环(将 num+1 附加到输出)。你的循环应该从 1 开始。public String toString() {&nbsp; &nbsp; StringBuilder builder = new StringBuilder();&nbsp; &nbsp; for (int num = 1; num < max; num++) { . // used to start at 2&nbsp; &nbsp; &nbsp; &nbsp; if (numbers[num]) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; builder.append(num+1).append(" ");&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; return builder.toString();&nbsp; &nbsp; }}加上你的代码集numbers[1] = false。那应该是numbers[1] = true。您也在循环直到< max - 1. 考虑循环直到< max。

胡子哥哥

我对您的代码进行了一些更改,要指出的是您不需要 max。您的 findPrimes() 看起来不错,但难以阅读且难以验证其正确性。您的 toString() 方法应该迭代每个元素,如果该元素为真,则将其附加到列表中。1 也不是素数,所以numbers[1] = false;它应该是。为什么1不是质数?class Sieve {&nbsp; &nbsp; // private int max; // max is superfluous use numbers.length instead&nbsp; &nbsp; private boolean[] numbers;&nbsp; &nbsp; public Sieve(int max) {&nbsp; &nbsp; &nbsp; &nbsp; if (max < 2) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; throw new IllegalArgumentException();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; numbers = new boolean[max];&nbsp; &nbsp; &nbsp; &nbsp; numbers[0] = false;&nbsp; &nbsp; &nbsp; &nbsp; numbers[1] = false;&nbsp; &nbsp; &nbsp; &nbsp; numbers[2] = true;&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 2; i <numbers.length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; numbers[i] = true;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; public void findPrimes() {&nbsp; &nbsp; &nbsp; &nbsp; // a bit more compact and neater in my opinion&nbsp; &nbsp; &nbsp; &nbsp; for (int num=2; num<numbers.length; num++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int multiple=num+num; multiple<numbers.length; multiple+=num) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; numbers[multiple] = false;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public String toString() {&nbsp; &nbsp; &nbsp; &nbsp; StringBuilder builder = new StringBuilder();&nbsp; &nbsp; &nbsp; &nbsp; // you should be iterating from 0 to 99&nbsp; &nbsp; &nbsp; &nbsp; for (int i=0; i<numbers.length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (numbers[i]) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; builder.append(i).append(" ");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return builder.toString();&nbsp; &nbsp; }}class Driver{&nbsp; &nbsp; //&nbsp; MAIN. Find some primes.&nbsp; &nbsp; public static void main(String [] args)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; Sieve sieve = null;&nbsp; //&nbsp; We must initialize SIEVE or Java will cry.&nbsp; &nbsp; &nbsp; &nbsp; //&nbsp; 5 points. This must print "Sieve size must be at least 2." but without the&nbsp; &nbsp; &nbsp; &nbsp; //&nbsp; quotes.&nbsp; &nbsp; &nbsp; &nbsp; try&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sieve = new Sieve(0);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; catch (IllegalArgumentException oops)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Sieve size must be at least 2.");&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; //&nbsp; 5 points. This must print nothing.&nbsp; &nbsp; &nbsp; &nbsp; try&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sieve = new Sieve(100);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; catch (IllegalArgumentException oops)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Sieve size must be at least 2.");&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; //&nbsp; 10 points. This must print integers from 2 to 99, separated by blanks.&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(sieve);&nbsp; &nbsp; &nbsp; &nbsp; //&nbsp; 10 points. This must print the prime numbers between 2 and 99, separated by&nbsp; &nbsp; &nbsp; &nbsp; //&nbsp; blanks. They are:&nbsp; &nbsp; &nbsp; &nbsp; //&nbsp; &nbsp; &nbsp; &nbsp; //&nbsp; 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97&nbsp; &nbsp; &nbsp; &nbsp; sieve.findPrimes();&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(sieve);&nbsp; &nbsp; }}
随时随地看视频慕课网APP

相关分类

Java
我要回答