実現したいこと
確率5%のくじをn回(変数charange)ひきr回(変数win)当たるときの確立を求めるprobilityAtlesst→probility
を実行した後のanswerをJspに出力するようにするとき、
①jspに出力する際、%表示を小数点第二位までの表記にしたい
②doPostメソッド内でリクエストパラメータを受け取った値を大小比較してif文の中に組み込みたい
if(){}
else if(win>charange)
(入力された文字が大きすぎます)
~
発生している問題・分からないこと
①jspに出力する際、MathemaクラスのgetAnswerメソッドで
return String.format("%.2f", this.answer);
としたがjspでは小数点二位で正しく入力されない(デバッグで検証済み)
② math.setWin(Integer.parseInt(win));
math.setCharange(Integer.parseInt(charange));
おそらくmathインスタンスのフィールドにwinの値が入っているのでdoPostメソッド内でcomparewin、comparecharangeの変数に入れても入らないとはわかるがmath.win(charange)とやっても参照できない
該当のソースコード
package Model; public class Mathema { private int per, nper; private int win,charange; double answer; public int getCharange() { return charange; } public void setCharange(int charange) { this.charange = charange; } public int getWin() { return win; } public void setWin(int win) { this.win = win; } public int getPer() { return per; } public void setPer(int per) { this.per = per; } public double getNPer() { return nper; } public void setNPer(int nper) { this.nper = nper; } public void setAnswer(double answer) { this.answer=answer; } public String getAnswer() { return String.format("%.2f", this.answer); } }
package Servlet; import java.io.IOException; import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import Model.Mathema; import Model.ProbilityLogic; @WebServlet("/ProbilityCheck") public class ProbilityCheck extends HttpServlet { private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { RequestDispatcher dispatcher = request.getRequestDispatcher("WEB-INF/ProbilityCheck.jsp"); dispatcher.forward(request, response); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { Mathema math = new Mathema(); String charange = request.getParameter("charange"); String win = request.getParameter("win"); math.setWin(Integer.parseInt(win)); math.setCharange(Integer.parseInt(charange)); if (charange != "" && win != "") { ProbilityLogic probilityLogic = new ProbilityLogic(); //probilityLogic.execute(math);以前 probilityLogic.probilityAtLeast(math); request.setAttribute("math", math); RequestDispatcher dispatcher = request.getRequestDispatcher("WEB-INF/ProbilityCheckResult.jsp"); dispatcher.forward(request, response); } else if (comparewin > comparecharange) { request.setAttribute("errorMsg", "数字が大きすぎます"); RequestDispatcher dispatcher = request.getRequestDispatcher("WEB-INF/ProbilityCheck.jsp"); //Beansのインスタンス(loginUser)が入ってると、main画面(main.jsp)に飛ぶ dispatcher.forward(request, response); } else { //エラーメッセージをリクエストスコープに保存 request.setAttribute("errorMsg", "数値が入力されていません"); RequestDispatcher dispatcher = request.getRequestDispatcher("WEB-INF/ProbilityCheck.jsp"); //Beansのインスタンス(loginUser)が入ってると、main画面(main.jsp)に飛ぶ dispatcher.forward(request, response); } } }
package Model; public class ProbilityLogic { public double probility(int n ,int r) { double ans = 1; double answer; for (int i = n; i > n - r; --i) { ans = ans * i;//分子階乗 } for (int i = 1; i < r + 1; ++i) { ans = ans / i;//分母階乗 } double result = Math.pow(0.05, r); double result2 = Math.pow(0.95, n - r); answer = 100 * (result2 * result * ans); return answer; } public void probilityAtLeast(Mathema math) { double totalprobility = 0; int r = math.getWin(); int n = math.getCharange(); for (int k = r; k <= n; k++) { totalprobility += probility(n, k);//rではない } //System.out.println("計算結果: " + totalprobility); math.setAnswer(totalprobility); } }
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <% String errorMsg = (String) request.getAttribute("errorMsg"); %> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <h1>サイトへようこそ</h1> <h1>計算</h1> <form action="ProbilityCheck" method="post"> <input type="text" name="charange">(回試行して)<br> <input type="text" name="win">(回当たる)<br> <input type="submit" value="送信"> </form> <% if (errorMsg != null) { %> <p><%=errorMsg%></p> <% } %> </body> </html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ page import="Model.Mathema"%> <% Mathema math = (Mathema) request.getAttribute("math"); %> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <p> <%= math.getAnswer() %>% (デバッグ: <%= math.getAnswer() %>) <%=math.getCharange()%>回試行して<br> <%=math.getWin()%>回以上当たる確率は<br> <%=math.getAnswer()%>%です<br> </p> </body> </html>
試したこと・調べたこと
- teratailやGoogle等で検索した
- ソースコードを自分なりに変更した
- 知人に聞いた
- その他
上記の詳細・結果
①double型変数answerを、String.format("%.2f", this.answer);
としてstringに変え、小数点第二位までの表示にしようとしていますが、実際のJSPファイルの表示
(
<%= math.getAnswer() %>% (デバッグ: <%= math.getAnswer() %>)
)
には変わらず小数点が二位以上の値が表示されてしまっています
②リクエストパラメータの値を直接if文の中に組み込みelse if(win>charange)とすると、Stringクラスでは"<"演算子は定義されていないといった注釈が付けられています
math.setWin(Integer.parseInt(win));
math.setCharange(Integer.parseInt(charange));
にある通りこれをint型に変換しようとして、この値をif文の中で使おうと思ったのですが上手くいきません(Mathemaクラスのsetメソッドに行っているため?)
補足
Mathemaクラスの
per,nperは現在はまだ使用してないです
<環境>
eclipse
Version: 2024-12 (4.34.0)
Build id: 20241128-0757
java version "23.0.2" 2025-01-21
Java(TM) SE Runtime Environment (build 23.0.2+7-58)
Java HotSpot(TM) 64-Bit Server VM (build 23.0.2+7-58, mixed mode, sharing)
TomCat9
Java17

回答1件
あなたの回答
tips
プレビュー