亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Java 應用程序拒絕將輸出顯示為浮點數。

Java 應用程序拒絕將輸出顯示為浮點數。

森林海 2022-11-10 16:44:43
問題:我在計算整數除法并顯示為雙精度(浮點)時遇到問題。在較低的數字上,它顯示為浮點數,但似乎將值四舍五入為 11.0、9.0、28.0。在嘗試通過其他 StackO 帖子解決問題后,我似乎無法使其保持一致。我已經通過一些解決方案能夠將其顯示為浮點解決方案,但是通過測試命令運行,結果在它們是否顯示為浮點時不一致。作業要求:編寫一個程序 RandomWalkers.java,它接受兩個整數命令行參數“r”和“trials”。在每個試驗獨立實驗中,模擬隨機游走,直到隨機游走者與起點的曼哈頓距離為 r。打印平均步數。隨著 'r' 的增加,我們預計隨機游走者會采取越來越多的步驟。但是還有多少步驟?使用 RandomWalkers.java 制定關于平均步數如何作為“r”函數增長的假設。通過生成隨機樣本和聚合結果來估計未知量是蒙特卡羅模擬的一個例子——一種強大的計算技術,廣泛用于統計物理學、計算金融和計算機圖形學。您不能調用除 java.lang 中的庫函數(例如Integer.parseInt()和Math.sqrt())之外的庫函數。僅使用課程中已經介紹過的 Java 特性(例如,循環和條件,但不使用數組)。我試過的:閱讀至少 30 多個不同的 StackOverflow 和其他各種網站頁面,將整數除法顯示為浮點(雙)等,并嘗試了其中的所有內容。avgSteps = (double) totalNumSteps / trials;將部分/所有變量更改為 Double(totalNumSteps * 1.0) / trials;(totalNumSteps + 0.0) / trials;我有時會取得突破,但隨后我對其進行了更多測試,但由于部分或全部參數傳遞給應用程序而失敗。代碼:下面的代碼是代碼的清理后的基本版本,減去上面的任何測試。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貢獻1812條經驗 獲得超5個贊

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


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);



   }


}


查看完整回答
反對 回復 2022-11-10
?
慕慕森

TA貢獻1856條經驗 獲得超17個贊

當變量聲明遠離其賦值或使用站點時,往往會發生此類錯誤。

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

Math.Random但是,當替換為RANDOM.nextInt(4)和時,我能夠看到巨大的(超過 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 的結果

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

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

我們可以做得更好

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

此外,由于我們在單線程上下文中運行,我們可以將 替換java.util.Randomjava.util.concurrent.ThreadLocalRandom

此外,顯式轉換double比乘以更清晰,1.0并為我們節省了兩個字節碼。

P0.95 r = 40 的結果

  • 重新分配: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


查看完整回答
反對 回復 2022-11-10
  • 2 回答
  • 0 關注
  • 117 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號