1

現在、最初の条件テストの出力結果を小数点第 2 位に四捨五入するのに苦労しています。数量が 5 に等しく、2 つの選択肢のいずれかを掛けると、結果は小数点の右側に 6 個以上のスペースがある数値になります。.toFixed を何かにアタッチしようとすると、double にリンクする新しい文字列変数を配置しても、プリミティブ型 double に toFixed(int) を適用できないと表示されます。

出力を小数点第 2 位に丸めようとしています。どんな助けでも大歓迎です。

アップデート!!!支援してくれたムザヒベチャラに感謝します。コードを配置するという彼の提案

String result = String.valueOf(new BigDecimal(calc).setScale(2, BigDecimal.ROUND_HALF_UP));

何度かバタバタした後、muzahidbechara の追加の助けを借りて、私が知らなかった優先順位があるため、コードを削除する必要があることがわかりました。

助けてくれたmuzahidbecharaに再び感謝します。他の誰かがこの種の問題を抱えている場合は、彼の回答に投票してください。

以下の .jsp ファイルにコードを配置したので、どこに配置したかがわかります。全体的な出力は (html コードに関する限り) まだ完全ではありませんが、これは明日作業する予定です。

Tomcat webapp/root フォルダーに配置する HTML ファイル

<!doctype html>
<html>
    <head>
        <!--
            Name: Student
            Date: 07APR16
            Program Description: Movie Rental Scenario
        -->

        <style>
            body {font-family:arial;}

            table.inner {border:2px solid black;margin-left: auto;margin-right: auto;background-color:rgb(128,170,255);}

            table.outer {border:2px solid black;margin-left: auto;margin-right: auto;}

            th {font-weight:normal;font-variant:small-caps;}

            input {background-color:rgb(204,230,255);}

            input.number {text-align:center;}

            p.top {font-weight:bold;font-size:15px;font-variant:small-caps;}

            p.bottom {text-align:"center";font-weight:bold;font-style:italic;font-size:15px;}

            input.footer {margin-left: auto;margin-right: auto}

            h5 {text-decoration:underline;}

            h5.misc1 {margin:0px 0px -10px 0px;}

            h5.misc2 {margin:0px 0px -20px 0px;}


        </style>
    </head>

    <body>
                <table class="outer" cellpadding="10px" border="1">
                <tr>
                    <th>
        <form method="post" action="videorental.jsp">
        <p class="top">Please provide contact information in the fields below</p>
            <table class="inner" cellpadding="10px" border="1">
                <tr>
                    <th>
                        <input type="text" id="fname" name="fname" placeholder="Firstname" required />
                        <input type="text" id="lname" name="lname" placeholder="Lastname" required /><br />
                        <br />
                        <input type="text" id="email" name="email" placeholder="email@example.net" pattern="[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,3}$" title="The email address you entered is invalid, please check to ensure you're using a valid email address that fits the formate email@example.net" style="width:288px;" required /><br />
                    </th>
                </tr>
            </table>
            <br />
        <p class="bottom">Please provide additional information below</p>
            <table class="inner" cellpadding="10px" border="1">
                <tr>
                    <th>
                    <h5 class="misc1">Number of movies to rent</h5><br />
                    <input class="number" type="text" name="quant" placeholder="##" pattern="[0-9]{1,2}" title="Please input a number between 1-99" style="width:20px" required/><br />
                    <br />
                    <h5 class="misc2">Which type of movie<h5>
                    <input type="radio" id="type" name="type" value="1" required />DVD (Cost per: $2.99)<br />
                    <input type="radio" id="type" name="type" value="2" required />Blu-ray (Cost per: $3.99)<br />
                    </th>
                </tr>
            </table>
            <br />
                <input class="footer" type="submit" value="Submit" />
                <input type="reset" value="Reset" />
                                    </th>
                </tr>
            </table>
        </form>
    </body>
</html>

Tomcat webapp/root フォルダーに配置するサーバー側 Javascript ファイル

<!DOCTYPE html>
    <html>
    <head>
    <style>

        body {font-family:arial;}

        table {border:2px solid black;margin-left: auto;margin-right: auto;background-color:rgb(128,170,255);}

        th {font-weight:normal;font-variant:small-caps;padding-left:500px;text-align:right;}

        td {text-align:center;}

    </style>
    </head>

    <body>
    <%
        /*
            Name: Student
            Date: 07APR16
            Program Description: Movie Rental Scenario
        */

        //Received Data

        String first = request.getParameter("fname");
        String last = request.getParameter("lname");
        String mail = request.getParameter("email");

        double num1 = Double.parseDouble(request.getParameter("quant"));
        byte choice = Byte.parseByte(request.getParameter("type"));

        double calc = 0.0;
        double answer = 0.0;

        // Condition Testing 1
        if (choice == 1)
        {
            calc = num1 * 2.99;
        }
        else if (choice == 2)
        {
            calc = num1 * 3.99;
        }

        // Condition Testing 2

        if (choice == 1)
        {
            answer = 2.99;
        }
        else if (choice == 2)
        {
            answer = 3.99;
        }

        String result = String.valueOf(new java.math.BigDecimal(calc).setScale(2, java.math.BigDecimal.ROUND_HALF_UP));

        //Display Results

        out.print("First-name  = " + first);
        out.print("Last-name = " + last);
        out.println("<BR>");
        out.print("Email Address = " + mail);
        out.println("<table border=1>");
        out.println("<tr>");
        out.println("<th>");
        out.print("Price for type of rental:<td>" + answer);
        out.println("</td>");
        out.println("</th>");
        out.println("</tr>");
        out.println("<tr>");
        out.println("<th>");
        out.print("Number of movies to rent:<td>" + num1);
        out.println("</td>");
        out.println("</th>");
        out.println("</tr>");
        out.println("<tr>");
        out.println("<th>");
        out.println("<td>");
        out.println("==========");
        out.println("</td>");
        out.println("</th>");
        out.println("</tr>");
        out.println("<tr>");
        out.println("<th>");
        out.print("Total:<td>" + result);
        out.println("</td>");
        out.println("</th>");
        out.println("</tr>");
        out.println("</table>");
    %>
</body>

4

1 に答える 1

0

まずdouble calc、プリミティブ型の変数なので、オブジェクトではないため利用できるメソッドがありません。

Double calc第 2 に、 (ダブル プリミティブのラッパー クラス) を Object として宣言しても、という名前のメソッドはありませんtoFixed()

java.math.BigDecimalJavascript のような精度を実現するために使用できますtoFixed()

String result = String.valueOf(new BigDecimal(calc).setScale(2, BigDecimal.ROUND_HALF_UP));
于 2016-04-09T16:09:46.023 に答える