說明:雙色球就是6個紅球,從1到33,1個藍球,從1到16,之間選擇:
servlet中代碼如下:
public class DoubleColorBallServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//業務邏輯代碼
Set<Integer> set = new HashSet<Integer>();
Random random = new Random();
//紅球
while(set.size()!=6){
int randomValue = random.nextInt(33)+1;
set.add(randomValue);
}
//藍球
int randomValue = random.nextInt(16)+1;
String result = "red Ball:";
for (Integer i : set) {
result += i +" ";
}
result +="<br>blue Ball:"+randomValue ;
//頁面輸出代碼
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.print("<html><head><title>result</title></head>");
out.print("<body><h1>"+ result +"</h1></body></html>");
out.flush();
}
}