刚学Javaweb,老师布置的作业没有头绪。

在jsp上的文本框中输入要找零的金额(0-100),然后点击找零按钮,在按钮的下方显示可找零的50元/20元/10元/5元/1元
的纸币各有多少张;
要求:
1.使用JSP程序段/jsp表达式/jsp声明和form表单获取参数来实现
2.如果输入的金额不在0-100之间,则不显示找零的具体内容,并且使用JS弹框进行提示
在百度上搜过,但代码已经没有了。

weixin_42170456
weixin_42170456 回复毕小宝: 非常感谢
6 个月之前 回复
wojiushiwo945you
毕小宝 楼主看看我的实现对你有没有帮助,也算是对我花十分钟回答这个问题的肯定了。
6 个月之前 回复
weixin_42170456
weixin_42170456 <%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Insert title here</title> </head> <body> <form action="" method = "get"> <input type="text" name="money"><br> <input type="submit" value="找零" > </form> <% String a = request.getParameter("money");%> <%if(a !=null && !a.equals("")){ %> <% int jine = Integer.parseInt(a);%> <% int[] howmuch= {1, 5, 10, 20, 50}; %> <%if(jine>=0&&jine<=100) {%> <% for (int i = howmuch.length - 1; i >= 0; i--) {%> <% int num = jine/ howmuch[i];%> <% int mod = jine % howmuch[i];%> <% jine = mod;%> <% if (num > 0){%> <%=num%>张 <%=howmuch[i] %>元 <% } %> <% } %> <% }else{%> <script type="text/javascript"> alert("请输入0-100以内的整数!"); </script> <% } %> <% } %> </body> </html>
6 个月之前 回复
weixin_42170456
weixin_42170456 <% } %>
6 个月之前 回复
weixin_42170456
weixin_42170456 已解决
6 个月之前 回复

1个回答

先来无事,帮楼主简单实现一下,流程如下:创建一个 Java Dynamic Project 类型的项目 csdn_test。图片说明

在 scr 目录下创建一个类创建一个 Servlet 类 名为 ChangesServlet,实现为:

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet(description = "Change calculate Servlet", urlPatterns = { "/changeServlet"})
public class ChangesServlet extends HttpServlet {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String money = req.getParameter("money");
        System.out.println(money);
        resp.setContentType("text/html;charset=UTF-8");
        PrintWriter out = resp.getWriter();
        try{
            int moneyValue = Integer.parseInt(money);
            int fifty= moneyValue/50;
            int twenty = (moneyValue - fifty*50)/20;
            int ten = (moneyValue - fifty*50- twenty*20)/10;
            int five = (moneyValue - fifty*50- twenty*20-ten*10)/5;
            int one = moneyValue - fifty*50- twenty*20-ten*10 - five*5;
            out.println(buildResponsePage(moneyValue,fifty,twenty,ten,five,one));
        }catch(Exception e){
            out.println(buildResponsePage(0,0,0,0,0,0));
        }


        out.flush();
        out.close();
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }

    private String buildResponsePage(int money,int fifty,int twenty,int ten,int five,int one){
        StringBuffer buffer = new StringBuffer();
        buffer.append("<!DOCTYPE html>");
        buffer.append("<html>");
        buffer.append("<body>");
        buffer.append("<form action=\"changeServlet\">");
        if(money==0 || money>100){
            buffer.append("<span>金额格式错误,请输入输入要找零的金额(0-100)</span><input value=\""+money+"\" type=\"text\" name=\"money\"/>");
        }else{
            buffer.append("<span>请输入输入要找零的金额(0-100)</span><input value=\""+money+"\" type=\"text\" name=\"money\"/>");
        }
        buffer.append("<button type=\"submit\">提交</button>");
        buffer.append("<br/><span>找零 50 元"+fifty+"张,20元"+twenty+"张,10元"+ten+"张,5 元"+five+"张,1 元"+one+"张");
        buffer.append("</form></body>");
        buffer.append("</html>");
        return buffer.toString();
    }
}

其次,在 WetContent 目录下创建一个 index.jsp 文件,前端表单输入,内容为:

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<body>
<form action="changeServlet">
    <span>请输入输入要找零的金额(0-100)</span><input type="text" name="money"/>
    <button type="submit">提交</button>
</form>
</body>
</html>

启动项目运行,访问 应用结果如下:
图片说明

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问