実現したいこと
確率5%のくじをn回引いた時のあたりの本数をrとしたときの確率answerをビューで出力したい
発生している問題・分からないこと
エラーメッセージはでませんでしたが、送信ボタンを推してもresult画面までいかず、また最初の画面に戻ってしまいます
どこかにエラーがあるのかもわからない状況です
追伸)controller(クラス:ProbilityCheck)のimportがjakartaではなくjavaxになってしまっているのが気がかりです、これが原因ということは考えられないでしょうか
エラーメッセージ
error
1エラーメッセージはなし(入力画面にリセットされる)
該当のソースコード
package model; import java.io.Serializable; public class Mathma implements Serializable { private int charange, per, nper, win; private 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 double getAnswer() { return this.answer; } } ※per,nper.getPer.setPer.getnPre,setnPerは使用せず(%を設定するまでにいたっていないので)
package model; public class ProbilityLogic { public void execute(Mathma math) { double answer; int r = math.getWin(); int n = math.getCharange(); long ans = 1; for (int i = n; i > n - r; --i) { ans = ans * i; } for (int i = 1; i < r + 1; ++i) { ans = ans / i; } System.out.println(ans); double result = Math.pow(0.05, r); double result2 = Math.pow(0.95, n - r); answer=100*(result2 * result * ans); math.setAnswer(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.Mathma; import model.ProbilityLogic; /** * Servlet implementation class ProbilityCheck */ @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 { String charange =request.getParameter("charange"); String win=request.getParameter("win"); Mathma math=new Mathma(); math.setWin(Integer.parseInt(win)); math.setCharange(Integer.parseInt(charange)); ProbilityLogic probilityLogic=new ProbilityLogic(); probilityLogic.execute(math); request.setAttribute("math",math); RequestDispatcher dispatcher = request.getRequestDispatcher("WEB-INF/ProbilityCheckResult.jsp"); dispatcher.forward(request, response); } }
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <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> </body> </html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ page import="model.Mathma" %> <% Mathma math =(Mathma)request.getAttribute("math"); %> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <p> <%= math.getWin() %>回試行して<br> <%= math.getCharange() %>回当たる確率は<br> <%= math.getAnswer() %>%です<br> </p> </body> </html>
試したこと・調べたこと
- teratailやGoogle等で検索した
- ソースコードを自分なりに変更した
- 知人に聞いた
- その他
上記の詳細・結果
スッキリわかるサーブレット&JSpの本から自作で作ってみましたがうまくいきませんでした
nCrをうまくアルゴリズムに落とし込むことはできましたが、その後がうまくいってないです
補足
<環境>
エクリプス
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)
TomCat10 java17
回答1件
あなたの回答
tips
プレビュー