首页 > 编程语言 >基于java的彩票选号系统的二次开发

基于java的彩票选号系统的二次开发

时间:2024-03-06 10:57:18浏览次数:30  
标签:WinNumbers java center color align req 彩票 int 二次开发

引言:在网上看到了一个较为简易的基于java的彩票选号系统,其主要通过后台生成中奖号码,用户选择并输入号码的方式进行,界面设计较为的简陋,源代码也只实现了较为基础的功能,并存在一些问题,比如用户输入错误时的逻辑问题;故而我在源代码的基础上进行了一定的界面美化和功能的补全,以下先附上源代码:

点击查看代码
package com.bigwork;

import java.io.*;
import java.util.TreeSet;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import javax.servlet.annotation.*;

@WebServlet("/LotteryNumberSelection")
public class LotteryNumberSelection extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        TreeSet<Integer> WinNumbers = new TreeSet<>();
        int k = 0;
        while(k != 7){
            int a= (int)(Math.random()*30 + 1);
            if(!WinNumbers.contains(a)) {
                WinNumbers.add(a);
                k++;
            }
        }

        int inum1= Integer.parseInt(req.getParameter("num1"));
        int inum2= Integer.parseInt(req.getParameter("num2"));
        int inum3= Integer.parseInt(req.getParameter("num3"));
        int inum4= Integer.parseInt(req.getParameter("num4"));
        int inum5= Integer.parseInt(req.getParameter("num5"));
        int inum6= Integer.parseInt(req.getParameter("num6"));
        int inum7= Integer.parseInt(req.getParameter("num7"));
        Integer[] CustomerNumbers = new Integer[7];
        CustomerNumbers[0] = inum1;
        CustomerNumbers[1] = inum2;
        CustomerNumbers[2] = inum3;
        CustomerNumbers[3] = inum4;
        CustomerNumbers[4] = inum5;
        CustomerNumbers[5] = inum6;
        CustomerNumbers[6] = inum7;

        int count = 0;
        for (int i = 0; i < 7; i++) {
            if(WinNumbers.contains(CustomerNumbers[i])) {
                count++;
            }
        }

        if(count < 5) {
            req.setAttribute("WinNumbers", WinNumbers);
            req.getRequestDispatcher("Fail.jsp").forward(req,resp);
        }
        if(count == 7) {
            req.setAttribute("WinNumbers", WinNumbers);
            req.getRequestDispatcher("FirstPrize.jsp").forward(req,resp);
        }
        if(count == 6) {
            req.setAttribute("WinNumbers", WinNumbers);
            req.getRequestDispatcher("SecondPrize.jsp").forward(req,resp);
        }
        if(count == 5) {
            req.setAttribute("WinNumbers", WinNumbers);
            req.getRequestDispatcher("ThirdPrize.jsp").forward(req,resp);
        }
    }
}
原项目截图: 选号界面:

image
中奖界面:

改进方向:
1、优化彩票选号的界面设计
2、优化彩票选号时的逻辑
3、优化中奖后的界面设计并显示中奖号码
4、使彩票选号更加的人性化
程序流程图:

改进结果:
1、优化了彩票选号的界面设计,使其更加的美观

2、增加了下拉框选择号码,避免数据的输入问题,也使彩票选号更加的人性化

3、优化中奖后的界面设计并显示中奖号码


改进后的代码:

点击查看代码
LotteryNumberSelection.java:
package com.bigwork;

import java.io.*;
import java.util.TreeSet;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import javax.servlet.annotation.*;

@WebServlet("/LotteryNumberSelection")
public class LotteryNumberSelection extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        TreeSet<Integer> WinNumbers = new TreeSet<>();
        int k = 0;
        while(k != 7){
            int a= (int)(Math.random()*30 + 1);
            if(!WinNumbers.contains(a)) {
                WinNumbers.add(a);
                k++;
            }
        }

        int inum1= Integer.parseInt(req.getParameter("num1"));
        int inum2= Integer.parseInt(req.getParameter("num2"));
        int inum3= Integer.parseInt(req.getParameter("num3"));
        int inum4= Integer.parseInt(req.getParameter("num4"));
        int inum5= Integer.parseInt(req.getParameter("num5"));
        int inum6= Integer.parseInt(req.getParameter("num6"));
        int inum7= Integer.parseInt(req.getParameter("num7"));
        Integer[] CustomerNumbers = new Integer[7];
        CustomerNumbers[0] = inum1;
        CustomerNumbers[1] = inum2;
        CustomerNumbers[2] = inum3;
        CustomerNumbers[3] = inum4;
        CustomerNumbers[4] = inum5;
        CustomerNumbers[5] = inum6;
        CustomerNumbers[6] = inum7;

        int count = 0;
        for (int i = 0; i < 7; i++) {
            if(WinNumbers.contains(CustomerNumbers[i])) {
                count++;
            }
        }

        if(count < 5) {
            req.setAttribute("WinNumbers", WinNumbers);
            req.getRequestDispatcher("Fail.jsp").forward(req,resp);
        }
        if(count == 7) {
            req.setAttribute("WinNumbers", WinNumbers);
            req.getRequestDispatcher("FirstPrize.jsp").forward(req,resp);
        }
        if(count == 6) {
            req.setAttribute("WinNumbers", WinNumbers);
            req.getRequestDispatcher("SecondPrize.jsp").forward(req,resp);
        }
        if(count == 5) {
            req.setAttribute("WinNumbers", WinNumbers);
            req.getRequestDispatcher("ThirdPrize.jsp").forward(req,resp);
        }
    }
}

index.jsp:
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<!DOCTYPE html>
<html>
<head>
    <title>彩票选号</title>
    <style>
        body {
            background: linear-gradient(to bottom right, #A0D468, #4BB2D0);
            font-family: Arial, sans-serif;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            margin: 0;
        }
        .container {
            background-color: #fff;
            padding: 20px;
            border-radius: 5px;
            box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
            display: flex;
            flex-direction: column;
            align-items: center;
        }
        h1 {
            text-align: center;
            margin-bottom: 20px;
            color: #FF2222;
        }
        form {
            display: flex;
            flex-direction: column;
            align-items: center;
        }
        select {
            width: 300px;
            padding: 10px;
            margin-bottom: 10px;
            border: 1px solid #ccc;
            border-radius: 3px;
        }
        button {
            background-color: #4CAF50;
            color: #fff;
            border: none;
            border-radius: 5px;
            padding: 10px;
            width: 100%;
            font-size: 18px;
            cursor: pointer;
            transition: background-color 0.3s;
        }
        button:disabled {
            background-color: #cccccc;
        }
        button:hover:enabled{
            background-color: #45A049;
        }
        .rules {
            text-align: center;
            color: #FF3333;
            margin-top: 20px;
        }
        .rule-text {
            margin: 0;
            text-align: left;
            padding-left: 20px;
        }
        .reminder {
            text-align: center;
            font-size: 14px;
            color: #666;
        }
    </style>
    <script>
        function checkForm() {
            var selects = document.getElementsByTagName("select");
            var button = document.getElementById("submitBtn");
            var selectedCount = 0;
            for (var i = 0; i < selects.length; i++) {
                if (selects[i].value !== "") {
                    selectedCount++;
                }
            }
            if (selectedCount === selects.length) {
                button.disabled = false;
            } else {
                button.disabled = true;
            }
        }
    </script>
</head>
<body>
<div class="container">
    <h1>彩票选号,赢取丰厚大奖!</h1>
    <form  action="LotteryNumberSelection"  method="post">
        <select  name="num1" onchange="checkForm()">
            <option  value=""  selected  disabled  hidden>请选择第一个号码</option>
            <% for (int i = 1; i <= 30; i++) { %>
            <option value="<%= i %>"><%= i %></option>
            <% } %>
        </select>
        <select  name="num2" onchange="checkForm()">
            <option  value=""  selected  disabled  hidden>请选择第二个号码</option>
            <% for (int i = 1; i <= 30; i++) { %>
            <option value="<%= i %>"><%= i %></option>
            <% } %>
        </select>
        <select  name="num3" onchange="checkForm()">
            <option  value=""  selected  disabled  hidden>请选择第三个号码</option>
            <% for (int i = 1; i <= 30; i++) { %>
            <option value="<%= i %>"><%= i %></option>
            <% } %>
        </select>
        <select  name="num4" onchange="checkForm()">
            <option  value=""  selected  disabled  hidden>请选择第四个号码</option>
            <% for (int i = 1; i <= 30; i++) { %>
            <option value="<%= i %>"><%= i %></option>
            <% } %>
        </select>
        <select  name="num5" onchange="checkForm()">
            <option  value=""  selected  disabled  hidden>请选择第五个号码</option>
            <% for (int i = 1; i <= 30; i++) { %>
            <option value="<%= i %>"><%= i %></option>
            <% } %>
        </select>
        <select  name="num6" onchange="checkForm()">
            <option  value=""  selected  disabled  hidden>请选择第六个号码</option>
            <% for (int i = 1; i <= 30; i++) { %>
            <option value="<%= i %>"><%= i %></option>
            <% } %>
        </select>
        <select  name="num7" onchange="checkForm()">
            <option  value=""  selected  disabled  hidden>请选择第七个号码</option>
            <% for (int i = 1; i <= 30; i++) { %>
            <option value="<%= i %>"><%= i %></option>
            <% } %>
        </select>
        <button id="submitBtn" type="submit" disabled>提交</button>
    </form>
    <div class="rules">
        <p class="rule-text"><span style="text-align: left">中奖规则:</span></p>
        <p class="rule-text"><span style="text-align: left">一等奖:</span>七个号码全部选中</p>
        <p class="rule-text"><span style="text-align: left">二等奖:</span>选中七个号码中的六个号码</p>
        <p class="rule-text"><span style="text-align: left">三等奖:</span>选中七个号码中的五个号码</p>
        <p class="rule-text"><span style="text-align: left">其他情况为:</span>未中奖!</p>
    </div>
    <p class="reminder">温馨提示:理性购彩,切勿沉迷</p>
</div>
</body>
</html>

FirstPrize.jsp:
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<!DOCTYPE html>
<html>
<head>
    <title>一等奖</title>
    <style>
        body {
            background-color: #FFF5E6;
            font-family: Arial, sans-serif;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            margin: 0;
        }
        .container {
            background-color: #FF4B33;
            padding: 20px;
            border-radius: 5px;
            box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
            display: flex;
            flex-direction: column;
            align-items: center;
            color: #FFFFFF;
        }
        h1 {
            text-align: center;
            margin-bottom: 20px;
            font-size: 36px;
            color: #FFFFFF;
        }
        .message {
            font-size: 18px;
            color: #FFFFFF;
        }
    </style>
</head>
<body>
<div class="container">
    <h1>恭喜您中奖了!</h1>
    <p   class="message">您的号码中了一等奖!</p>
    <p>中奖号码为:${requestScope.WinNumbers}</p>
</div>
</body>
</html>

SecondPrize.jsp:
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<!DOCTYPE html>
<html>
<head>
    <title>二等奖</title>
    <style>
        body {
            background-color: #FFF5E6;
            font-family: Arial, sans-serif;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            margin: 0;
        }
        .container {
            background-color: #FF4B33;
            padding: 20px;
            border-radius: 5px;
            box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
            display: flex;
            flex-direction: column;
            align-items: center;
            color: #FFFFFF;
        }
        h1 {
            text-align: center;
            margin-bottom: 20px;
            font-size: 36px;
            color: #FFFFFF;
        }
        .message {
            font-size: 18px;
            color: #FFFFFF;
        }
    </style>
</head>
<body>
<div class="container">
    <h1>恭喜您中奖了!</h1>
    <p   class="message">您的号码中了二等奖!</p>
    <p>中奖号码为:${requestScope.WinNumbers}</p>
</div>
</body>
</html>

ThirdPrize.jsp:
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<!DOCTYPE html>
<html>
<head>
    <title>三等奖</title>
    <style>
        body {
            background-color: #FFF5E6;
            font-family: Arial, sans-serif;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            margin: 0;
        }
        .container {
            background-color: #FF4B33;
            padding: 20px;
            border-radius: 5px;
            box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
            display: flex;
            flex-direction: column;
            align-items: center;
            color: #FFFFFF;
        }
        h1 {
            text-align: center;
            margin-bottom: 20px;
            font-size: 36px;
            color: #FFFFFF;
        }
        .message {
            font-size: 18px;
            color: #FFFFFF;
        }
    </style>
</head>
<body>
<div class="container">
    <h1>恭喜您中奖了!</h1>
    <p   class="message">您的号码中了三等奖!</p>
    <p>中奖号码为:${requestScope.WinNumbers}</p>
</div>
</body>
</html>

Fail.jsp:
<%@  page  contentType="text/html;  charset=UTF-8"  pageEncoding="UTF-8"  %>
<!DOCTYPE  html>
<html>
<head>
    <title>未中奖</title>
    <style>
        body  {
            background-color:  #F5F5F5;
            font-family:  Arial,  sans-serif;
            display:  flex;
            justify-content:  center;
            align-items:  center;
            height:  100vh;
            margin:  0;
        }
        .container  {
            background-color:  #fff;
            padding:  20px;
            border-radius:  5px;
            box-shadow:  0  2px  5px  rgba(0,  0,  0,  0.1);
            display:  flex;
            flex-direction:  column;
            align-items:  center;
        }
        h1  {
            text-align:  center;
            margin-bottom:  20px;
            color:  #FF2222;
        }
        .message  {
            font-size:  18px;
            color:  #333;
        }
    </style>
</head>
<body>
<div  class="container">
    <h1>未中奖!</h1>
    <p  class="message">很遗憾,您的号码没有中奖!请继续努力,祝您好运!</p>
    <p>中奖号码为:${requestScope.WinNumbers}</p>
</div>
</body>
</html>

开发感想:
在二次开发的过程中,我首先遇到的是对原有代码的理解和消化,需要消耗一定时间去理解原作者的编码思路和想法。而且在开发过程中我也遇到了一些技术难题,有些问题是在原项目中未曾遇到的,我通过不断地查阅资料并尝试不同的解决方案,在每一次解决问题的过程,都是对我软件开发与创新能力的提升和锻炼。同时,在开发的过程中,我也发现了原项目中的一些不足和可以优化的地方。通过二次开发,我们不仅对这些问题进行了修复,还增加了一些新的功能和特性。这让我意识到,二次开发不仅是对原有代码的修改和完善,更是一个创新和提升的过程。

标签:WinNumbers,java,center,color,align,req,彩票,int,二次开发
From: https://www.cnblogs.com/xiangjunjie/p/18056033

相关文章

  • 基于Struts2 MVC的人事管理系统的二次开发
    引言这系统是一个基于JavaWeb开发和Struts2框架的简单用户管理系统。主要功能包括用户的登录、注册、查看用户列表、更新用户信息、以及删除用户等操作。系统使用了MySQL数据库存储用户信息,通过DAO模式实现了数据访问逻辑的分离。通过Struts2框架,实现了前后端的交互,通过XML配置......
  • Java 实现全链路日志跟踪唯一ID
    Java实现全链路日志跟踪唯一ID日志痛点:使用Spring-Aop切面的时候,只能切控制层或者服务层的开始位置与结束位置的数据(也就是请求出入参),对于逻辑日志无法定位跟踪普通打印日志的时候是这样子的1.如果参数里面没有seq传递过来LOGGER.error("xxx不能为空");2.参数里面有se......
  • Java 8 Supplier函数式接口介绍及代码样例
    介绍供应商接口(SupplierInterface)是Java8引入的java.util.function包的一部分,用于在Java中实现函数式编程。它表示一个函数,该函数不接收任何参数,但会产生一个类型为T的值。T:表示结果的类型分配给Supplier类型对象的lambda表达式用于定义其get(),最终产生一个值。......
  • Reference management in Java and Rust, and, how faster Rust can be?
    Hi,thisisablogcomparingthereferenceinrustandjava.IreallylovejavaandIhavespendsometimelearningtheframeworklikespringandothers.AftertakingCOMP6991,Ihavegotthisthink:Howjavamanagethereferenceinmyprogram?WhycanI......
  • java.util.Arrays 快速学习教程
    在Java中,java.util.Arrays类提供的多种数组操作功能,可以有效地执行各种数组相关的操作,使得数组处理变得简单和高效。打印数组String[]arr=newString[]{"a","b","c","d"};System.out.println(Arrays.toString(arr));//输出[a,b,c,d]Arrays.toString(arr),不过......
  • Java核心内容面试题详解
    前言随着经济的复苏,市场逐渐回暖,曾经的金三银四,金九银十也慢慢回归,在这个节骨眼上,我们要努力学习,做好知识储备,准备随时接收这泼天的offer。而我利用摸鱼(不是,是工作之余)时间也整理了一份关于Java核心知识的面试题,大家有兴趣,有需要的可以看看,希望能够给大家提供一些帮助Java基础面......
  • 关于Java并发多线程的一点思考
    写在开头在过去的2023年双11活动中,天猫的累计访问人次达到了8亿,京东超60个品牌销售破10亿,直播观看人数3.0亿人次,订单支付频率1分钟之内可达百万级峰值,这样的瞬间高并发活动,给服务端带来的冲击可想而知,就如同医院那么多医生,去看病挂号时,有时候都需要排队,对于很多时间就是金钱的场......
  • 【转】[Java]接口的 VO 使用内部类的写法
    参考:https://www.cnblogs.com/hyperionG/p/15602642.html以下代码段是向阿里的通义灵码提问得到的:importlombok.Data;@DatapublicclassOuterVO{//外部类的属性privateStringouterAttribute;//定义内部类并添加@Data注解@Datapublicst......
  • Java中的对象克隆
    对象克隆复制一个一模一样的新对象出来浅克隆拷贝出的新对象,与原对象中的数据一模一样(引用类型拷贝的只是地址)深克隆对象中基本类型的数据直接拷贝。对象中的字符串数据拷贝的还是地址。对象中包含的其他对象,不会拷贝地址,会创建新对象packagecom.aiit.itcq;imp......
  • Java进制之间的转换
    进制:我们生活中使用的是十进制计算机中使用的是二进制在Java中的进制的分类?十进制:逢十进一二进制:逢二进一八进制:逢八进一十六进制:逢十六进一10->A11->B12->C13->D14->E15->F在计算机中,数据......