第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

需要幫助找出我的代碼中的錯(cuò)誤

需要幫助找出我的代碼中的錯(cuò)誤

慕哥6287543 2021-11-24 15:31:08
所以我需要幫助弄清楚為什么我的代碼不包括數(shù)字 2 而它在主要印刷行上包括數(shù)字 99。我需要更改 findPrime() 上的某些內(nèi)容嗎?我嘗試使用索引,結(jié)果變得更糟。    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);  }}
查看完整描述

2 回答

?
心有法竹

TA貢獻(xiàn)1866條經(jīng)驗(yàn) 獲得超5個(gè)贊

您的toString()方法從 num = 2 開始循環(huán)(將 num+1 附加到輸出)。你的循環(huán)應(yīng)該從 1 開始。


public String toString() {

    StringBuilder builder = new StringBuilder();

    for (int num = 1; num < max; num++) { . // used to start at 2

        if (numbers[num]) {

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

        }

    }

    return builder.toString();

    }

}

加上你的代碼集numbers[1] = false。那應(yīng)該是numbers[1] = true。


您也在循環(huán)直到< max - 1. 考慮循環(huán)直到< max。


查看完整回答
反對(duì) 回復(fù) 2021-11-24
?
胡子哥哥

TA貢獻(xiàn)1825條經(jīng)驗(yàn) 獲得超6個(gè)贊

我對(duì)您的代碼進(jìn)行了一些更改,要指出的是您不需要 max。您的 findPrimes() 看起來不錯(cuò),但難以閱讀且難以驗(yàn)證其正確性。您的 toString() 方法應(yīng)該迭代每個(gè)元素,如果該元素為真,則將其附加到列表中。


1 也不是素?cái)?shù),所以numbers[1] = false;它應(yīng)該是。為什么1不是質(zhì)數(shù)?


class Sieve {

    // private int max; // max is superfluous use numbers.length instead

    private boolean[] numbers;


    public Sieve(int max) {

        if (max < 2) {

            throw new IllegalArgumentException();

        }


        numbers = new boolean[max];

        numbers[0] = false;

        numbers[1] = false;

        numbers[2] = true;

        for (int i = 2; i <numbers.length; i++) {

            numbers[i] = true;

        }

    }

    public void findPrimes() {

        // a bit more compact and neater in my opinion

        for (int num=2; num<numbers.length; num++) {

            for (int multiple=num+num; multiple<numbers.length; multiple+=num) {

                numbers[multiple] = false;

            }

        }

    }

    @Override

    public String toString() {

        StringBuilder builder = new StringBuilder();

        // you should be iterating from 0 to 99

        for (int i=0; i<numbers.length; i++) {

            if (numbers[i]) {

                builder.append(i).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);

    }

}



查看完整回答
反對(duì) 回復(fù) 2021-11-24
  • 2 回答
  • 0 關(guān)注
  • 176 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)