<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ page contentType="text/html"%> <%@ page import="java.util.regex.Pattern"%> <%@ page import="java.util.regex.Matcher"%> <%!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; }%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> </head> <body bgcolor="pink"> <p style="font-family:黑体;font-size:20"> <br>张三的菜单: <br> <% String str = "麻婆豆腐:20.6元,红烧肉:68.9"; %> <br><%=str%><br> <br> <b><i>消费总额:<%=getPriceSum(str)%></i><br> </b> <br>李四的菜单:<br> <% str = "剁椒鱼头:62.9元,烤鸭:199元,红焖大虾:289.9元"; %> <br><%=str%><br> <br> <b><i>消费总额:<%=getPriceSum(str)%></i><br> </b> </p> </body> </html>