📄 voter.jsp
字号:
<%!
private int totalVotes = 0;
private double average = 0;
private int[] votes = new int[5];
private String[] choices = new String[5];
%><%
// initialize array
choices[0] = "Poor";
choices[1] = "Fair";
choices[2] = "Good";
choices[3] = "Great";
choices[4] = "Awesome";
String sChoice = request.getParameter("choice");
if ( sChoice != null )
{
int iChoice = 0;
try
{
iChoice = Integer.parseInt(sChoice);
if ( iChoice < choices.length)
{
// increment the vote tally for that choice
votes[iChoice]++;
totalVotes++;
int sum = 0;
for ( int i = 0; i < choices.length; i++)
{
sum = sum + (i+1) * votes[i];
}
average = (1.0 * sum) / totalVotes;
}
}
catch (NumberFormatException nfe)
{
// bad votes are not counted
}
}
// output the vote results
StringBuffer voteResults = new StringBuffer();
String sAverage = ""+ average;
voteResults.append("<vote-example>\n");
voteResults.append(" <votes>\n");
voteResults.append(" <number>"+ totalVotes +"</number>\n");
voteResults.append(" <average>"+ sAverage.substring(0,3) +"</average>\n");
voteResults.append(" <choices>\n");
for ( int j = 0; j < choices.length; j++)
{
voteResults.append(" <choice>\n");
voteResults.append(" <name>"+ choices[j] +"</name>\n");
voteResults.append(" <number>"+ votes[j] +"</number>\n");
voteResults.append(" <value>"+ j +"</value>\n");
voteResults.append(" </choice>\n");
}
voteResults.append(" </choices>\n");
voteResults.append(" </votes>\n");
voteResults.append("</vote-example>\n");
out.println(voteResults.toString() );
%>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -