第三次jsp作业
<%@ page contentType="text/html" language="java" import="java.util.regex.*" pageEncoding="utf-8"%> <%!public double getPriceSum(String input) { Pattern pattern; Matcher matcher; String regex = "-?[0-9][0-9]*[.]?[0-9]*"; pattern = Pattern.compile(regex); matcher = pattern.matcher(input); double sum = 0; while (matcher.find()) { String str = matcher.group(); sum += Double.parseDouble(str); } return sum; }%> <html> <body bgcolor=cyan> <p style="font-family:黑体;font-size:20"> <br>我的菜单:</br> <% String str = "麻辣豆腐:20.6元,红烧肉:20.9元"; %> <br><%=str%></br> <br> <b><i>消费总和:<%=getPriceSum(str)%></i> </b> </br> <br>我的菜单2:</br> <% str = "剁椒鱼头:62.9元,鸭:200元,虾:289元"; %> <br><%=str%></br> <br> <b><i>消费总和:<%=getPriceSum(str)%></i> </b></br> </p> </body> </html>