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

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

Java 應(yīng)用程序拒絕將輸出顯示為浮點(diǎn)數(shù)。

Java 應(yīng)用程序拒絕將輸出顯示為浮點(diǎn)數(shù)。

森林海 2022-11-10 16:44:43
問題:我在計(jì)算整數(shù)除法并顯示為雙精度(浮點(diǎn))時(shí)遇到問題。在較低的數(shù)字上,它顯示為浮點(diǎn)數(shù),但似乎將值四舍五入為 11.0、9.0、28.0。在嘗試通過其他 StackO 帖子解決問題后,我似乎無法使其保持一致。我已經(jīng)通過一些解決方案能夠?qū)⑵滹@示為浮點(diǎn)解決方案,但是通過測(cè)試命令運(yùn)行,結(jié)果在它們是否顯示為浮點(diǎn)時(shí)不一致。作業(yè)要求:編寫一個(gè)程序 RandomWalkers.java,它接受兩個(gè)整數(shù)命令行參數(shù)“r”和“trials”。在每個(gè)試驗(yàn)獨(dú)立實(shí)驗(yàn)中,模擬隨機(jī)游走,直到隨機(jī)游走者與起點(diǎn)的曼哈頓距離為 r。打印平均步數(shù)。隨著 'r' 的增加,我們預(yù)計(jì)隨機(jī)游走者會(huì)采取越來越多的步驟。但是還有多少步驟?使用 RandomWalkers.java 制定關(guān)于平均步數(shù)如何作為“r”函數(shù)增長(zhǎng)的假設(shè)。通過生成隨機(jī)樣本和聚合結(jié)果來估計(jì)未知量是蒙特卡羅模擬的一個(gè)例子——一種強(qiáng)大的計(jì)算技術(shù),廣泛用于統(tǒng)計(jì)物理學(xué)、計(jì)算金融和計(jì)算機(jī)圖形學(xué)。您不能調(diào)用除 java.lang 中的庫(kù)函數(shù)(例如Integer.parseInt()和Math.sqrt())之外的庫(kù)函數(shù)。僅使用課程中已經(jīng)介紹過的 Java 特性(例如,循環(huán)和條件,但不使用數(shù)組)。我試過的:閱讀至少 30 多個(gè)不同的 StackOverflow 和其他各種網(wǎng)站頁(yè)面,將整數(shù)除法顯示為浮點(diǎn)(雙)等,并嘗試了其中的所有內(nèi)容。avgSteps = (double) totalNumSteps / trials;將部分/所有變量更改為 Double(totalNumSteps * 1.0) / trials;(totalNumSteps + 0.0) / trials;我有時(shí)會(huì)取得突破,但隨后我對(duì)其進(jìn)行了更多測(cè)試,但由于部分或全部參數(shù)傳遞給應(yīng)用程序而失敗。代碼:下面的代碼是代碼的清理后的基本版本,減去上面的任何測(cè)試。public class RandomWalkers {    public static void main(String[] args) {        int r = Integer.parseInt(args[0]);        int trials = Integer.parseInt(args[1]);        int x = 0;        int xx = 0;        int y = 0;        int yy = 0;        int numSteps = 0;        int totalNumSteps = 0;        double randNum = 0.0;        double avgSteps = 0.0;        for (long i = 0; i < trials; i++) {            while (Math.abs(x - xx) + Math.abs(y - yy) != r) {                randNum = Math.random();                if (randNum <= .25) {                    // North                    yy++;                } else if (randNum <= .5) {                    // East                    xx++;                } else if (randNum <= .75) {                    // South                    yy--;                } else {                    // West                    xx--;                }                numSteps++;            }            totalNumSteps += numSteps;        }        avgSteps = totalNumSteps / trials;        System.out.println("average number of steps = " + avgSteps);    }}
查看完整描述

2 回答

?
慕雪6442864

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

所以這里有兩個(gè)問題。1) 正如 Carlos Heuberger 所指出的,每次循環(huán)時(shí)都需要重新初始化變量。2)正如您所指出的,將除法設(shè)置為實(shí)數(shù)除法,而不是整數(shù)的“div”運(yùn)算符需要一些注意。我對(duì)您的代碼進(jìn)行了這兩項(xiàng)更改(for 循環(huán)中的前 5 行;(1.0 * 試驗(yàn))),它似乎通過了所有測(cè)試。你很親密。


public class RandomWalkers {


public static void main(String[] args) {

    int r = Integer.parseInt(args[0]);

    int trials = Integer.parseInt(args[1]);

    int x = 0;

    int xx = 0;

    int y = 0;

    int yy = 0;

    int numSteps = 0;

    int totalNumSteps = 0;

    double randNum = 0.0;

    double avgSteps = 0.0;


    for (long i = 0; i < trials; i++) {

        x = 0;

        xx = 0;

        y = 0;

        yy = 0;

        numSteps = 0;

        while (Math.abs(x - xx) + Math.abs(y - yy) != r) {

            randNum = Math.random();

            if (randNum <= .25) {

                // North

                yy++;


            } else if (randNum <= .5) {

                // East

                xx++;


            } else if (randNum <= .75) {

                // South

                yy--;


            } else {

                // West

                xx--;


            }

            numSteps++;

        }

        totalNumSteps += numSteps;

    }


    avgSteps = totalNumSteps / (1.0 * trials);

    System.out.println("average number of steps = " + avgSteps);



   }


}


查看完整回答
反對(duì) 回復(fù) 2022-11-10
?
慕慕森

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

當(dāng)變量聲明遠(yuǎn)離其賦值或使用站點(diǎn)時(shí),往往會(huì)發(fā)生此類錯(cuò)誤。

使用Java Microbenchmark Harness(JMH)我無法看到重新分配和重新聲明變量之間的明顯性能優(yōu)勢(shì)。

Math.Random但是,當(dāng)替換為RANDOM.nextInt(4)和時(shí),我能夠看到巨大的(超過 2 倍的速度)switch

import java.util.Random;


public class RandomWalkers {


    static final Random RANDOM = new Random();


    public static void main(final String[] args) {


        int r = Integer.parseInt(args[0]);


        int trials = Integer.parseInt(args[1]);


        int totalNumSteps = 0;


        for (long i = 0; i < trials; i++) {

            int x = 0;

            int xx = 0;

            int y = 0;

            int yy = 0;

            int numSteps = 0;


            while (Math.abs(x - xx) + Math.abs(y - yy) != r) {


                switch (RANDOM.nextInt(4)) {

                    case 0:

                        // North

                        yy++;

                        break;

                    case 1:

                        // East

                        xx++;

                        break;

                    case 2:

                        // South

                        yy--;

                        break;

                    default:

                        // West

                        xx--;

                }

                numSteps++;

            }


            totalNumSteps += numSteps;

        }


        double avgSteps = totalNumSteps / (1.0 * trials);

        System.out.println("average number of steps = " + avgSteps);

    }

}

P0.95 r = 40 的結(jié)果

  • 重新分配:299.368 毫秒/操作

  • 重新聲明RandomIntSwitch:139.107 毫秒/操作

我們可以做得更好

顯式if條件雖然可讀性稍差,但(在這種情況下)比switch

此外,由于我們?cè)趩尉€程上下文中運(yùn)行,我們可以將 替換java.util.Randomjava.util.concurrent.ThreadLocalRandom。

此外,顯式轉(zhuǎn)換double比乘以更清晰,1.0并為我們節(jié)省了兩個(gè)字節(jié)碼。

P0.95 r = 40 的結(jié)果

  • 重新分配:299.368 毫秒/操作

  • 重新聲明RandomIntSwitch:139.107 毫秒/操作

  • 重新聲明ThreadLocalRandomIntIf:122.539 ms/op

下面的代碼快了將近 2.5 倍。

package com.stackoverflow.q56030483;


import java.util.concurrent.ThreadLocalRandom;


@SuppressWarnings("javadoc")

public class RandomWalker {


    public static void main(final String[] args) {


        int r = Integer.parseInt(args[0]);


        int trials = Integer.parseInt(args[1]);


        int totalNumSteps = 0;


        final ThreadLocalRandom threadLocalRandom = ThreadLocalRandom.current();


        for (long i = 0; i < trials; i++) {


            int x = 0;


            int xx = 0;


            int y = 0;


            int yy = 0;


            int numSteps = 0;


            while (Math.abs(x - xx) + Math.abs(y - yy) != r) {


                final int direction= threadLocalRandom.nextInt(4);


                // North

                if (direction == 0) {

                    yy++;


                // East

                } else if (direction == 1) {

                    xx++;


                // South

                } else if (direction == 2) {

                    yy--;


                // West

                } else {

                    xx--;

                }


                numSteps++;

            }


            totalNumSteps += numSteps;

        }


        System.out.println("average number of steps = " + totalNumSteps / (double) trials);


    }

}

Benchmark                                                                (arg)    Mode    Cnt    Score   Error  Units

RandomWalkers.reassign                                                       3  sample  37256    1.611 ± 0.002  ms/op

RandomWalkers.reassign:reassign·p0.00                                        3  sample           1.475          ms/op

RandomWalkers.reassign:reassign·p0.50                                        3  sample           1.593          ms/op

RandomWalkers.reassign:reassign·p0.90                                        3  sample           1.686          ms/op

RandomWalkers.reassign:reassign·p0.95                                        3  sample           1.780          ms/op

RandomWalkers.reassign:reassign·p0.99                                        3  sample           1.999          ms/op

RandomWalkers.reassign:reassign·p0.999                                       3  sample           2.507          ms/op

RandomWalkers.reassign:reassign·p0.9999                                      3  sample           4.367          ms/op

RandomWalkers.reassign:reassign·p1.00                                        3  sample          10.371          ms/op

RandomWalkers.reassign                                                      10  sample   3528   17.029 ± 0.063  ms/op

RandomWalkers.reassign:reassign·p0.00                                       10  sample          15.548          ms/op

RandomWalkers.reassign:reassign·p0.50                                       10  sample          16.712          ms/op

RandomWalkers.reassign:reassign·p0.90                                       10  sample          18.416          ms/op

RandomWalkers.reassign:reassign·p0.95                                       10  sample          18.842          ms/op

RandomWalkers.reassign:reassign·p0.99                                       10  sample          20.690          ms/op

RandomWalkers.reassign:reassign·p0.999                                      10  sample          27.636          ms/op

RandomWalkers.reassign:reassign·p0.9999                                     10  sample          36.176          ms/op

RandomWalkers.reassign:reassign·p1.00                                       10  sample          36.176          ms/op

RandomWalkers.reassign                                                      40  sample    227  268.714 ± 3.270  ms/op

RandomWalkers.reassign:reassign·p0.00                                       40  sample         251.134          ms/op

RandomWalkers.reassign:reassign·p0.50                                       40  sample         262.144          ms/op

RandomWalkers.reassign:reassign·p0.90                                       40  sample         296.223          ms/op

RandomWalkers.reassign:reassign·p0.95                                       40  sample         299.368          ms/op

RandomWalkers.reassign:reassign·p0.99                                       40  sample         303.416          ms/op

RandomWalkers.reassign:reassign·p0.999                                      40  sample         305.136          ms/op

RandomWalkers.reassign:reassign·p0.9999                                     40  sample         305.136          ms/op

RandomWalkers.reassign:reassign·p1.00                                       40  sample         305.136          ms/op

RandomWalkers.redeclareRandomIntSwitch                                       3  sample  69486    0.863 ± 0.001  ms/op

RandomWalkers.redeclareRandomIntSwitch:redeclareRandomIntSwitch·p0.00        3  sample           0.763          ms/op

RandomWalkers.redeclareRandomIntSwitch:redeclareRandomIntSwitch·p0.50        3  sample           0.843          ms/op

RandomWalkers.redeclareRandomIntSwitch:redeclareRandomIntSwitch·p0.90        3  sample           0.925          ms/op

RandomWalkers.redeclareRandomIntSwitch:redeclareRandomIntSwitch·p0.95        3  sample           1.028          ms/op

RandomWalkers.redeclareRandomIntSwitch:redeclareRandomIntSwitch·p0.99        3  sample           1.155          ms/op

RandomWalkers.redeclareRandomIntSwitch:redeclareRandomIntSwitch·p0.999       3  sample           1.721          ms/op

RandomWalkers.redeclareRandomIntSwitch:redeclareRandomIntSwitch·p0.9999      3  sample           5.181          ms/op

RandomWalkers.redeclareRandomIntSwitch:redeclareRandomIntSwitch·p1.00        3  sample           9.355          ms/op

RandomWalkers.redeclareRandomIntSwitch                                      10  sample   7072    8.485 ± 0.040  ms/op

RandomWalkers.redeclareRandomIntSwitch:redeclareRandomIntSwitch·p0.00       10  sample           7.668          ms/op

RandomWalkers.redeclareRandomIntSwitch:redeclareRandomIntSwitch·p0.50       10  sample           8.143          ms/op

RandomWalkers.redeclareRandomIntSwitch:redeclareRandomIntSwitch·p0.90       10  sample           9.650          ms/op

RandomWalkers.redeclareRandomIntSwitch:redeclareRandomIntSwitch·p0.95       10  sample          10.109          ms/op

RandomWalkers.redeclareRandomIntSwitch:redeclareRandomIntSwitch·p0.99       10  sample          11.960          ms/op

RandomWalkers.redeclareRandomIntSwitch:redeclareRandomIntSwitch·p0.999      10  sample          20.399          ms/op

RandomWalkers.redeclareRandomIntSwitch:redeclareRandomIntSwitch·p0.9999     10  sample          25.919          ms/op

RandomWalkers.redeclareRandomIntSwitch:redeclareRandomIntSwitch·p1.00       10  sample          25.919          ms/op

RandomWalkers.redeclareRandomIntSwitch                                      40  sample    466  130.302 ± 0.872  ms/op

RandomWalkers.redeclareRandomIntSwitch:redeclareRandomIntSwitch·p0.00       40  sample         123.732          ms/op

RandomWalkers.redeclareRandomIntSwitch:redeclareRandomIntSwitch·p0.50       40  sample         128.844          ms/op

RandomWalkers.redeclareRandomIntSwitch:redeclareRandomIntSwitch·p0.90       40  sample         135.083          ms/op

RandomWalkers.redeclareRandomIntSwitch:redeclareRandomIntSwitch·p0.95       40  sample         139.107          ms/op

RandomWalkers.redeclareRandomIntSwitch:redeclareRandomIntSwitch·p0.99       40  sample         155.153          ms/op

RandomWalkers.redeclareRandomIntSwitch:redeclareRandomIntSwitch·p0.999      40  sample         182.452          ms/op

RandomWalkers.redeclareRandomIntSwitch:redeclareRandomIntSwitch·p0.9999     40  sample         182.452          ms/op

RandomWalkers.redeclareRandomIntSwitch:redeclareRandomIntSwitch·p1.00       40  sample         182.452          ms/op


RandomWalkers.redeclareThreadLocalRandomIntIf                                               40  sample   96  107.953 ± 2.148  ms/op

RandomWalkers.redeclareThreadLocalRandomIntIf:redeclareThreadLocalRandomIntIf·p0.00          40  sample        99.746          ms/op

RandomWalkers.redeclareThreadLocalRandomIntIf:redeclareThreadLocalRandomIntIf·p0.50          40  sample       107.676          ms/op

RandomWalkers.redeclareThreadLocalRandomIntIf:redeclareThreadLocalRandomIntIf·p0.90          40  sample       113.797          ms/op

RandomWalkers.redeclareThreadLocalRandomIntIf:redeclareThreadLocalRandomIntIf·p0.95          40  sample       122.539          ms/op

RandomWalkers.redeclareThreadLocalRandomIntIf:redeclareThreadLocalRandomIntIf·p0.99          40  sample       130.810          ms/op

RandomWalkers.redeclareThreadLocalRandomIntIf:redeclareThreadLocalRandomIntIf·p0.999         40  sample       130.810          ms/op

RandomWalkers.redeclareThreadLocalRandomIntIf:redeclareThreadLocalRandomIntIf·p0.9999     40  sample       130.810          ms/op

RandomWalkers.redeclareThreadLocalRandomIntIf:redeclareThreadLocalRandomIntIf·p1.00       40  sample       130.810          ms/op


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

添加回答

舉報(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)