?? scoreservlet.java
字號:
import java.io.*;
import java.util.Vector;
import java.util.Collections;
import javax.servlet.*;
import javax.servlet.http.*;
public class ScoreServlet extends HttpServlet
{
private static Vector topScores = new Vector();
public void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException
{
// open up the input stream and convert it into a string
InputStream in = req.getInputStream();
int requestLength = req.getContentLength();
if (requestLength < 1) throw new IOException("invalid request length");
StringBuffer sb = new StringBuffer(requestLength);
for (int i = 0; i < requestLength; i++)
{
int c = in.read();
if (c == -1)
break;
else
sb.append((char) c);
}
in.close();
String rs = sb.toString();
// process things
System.out.println("New score uploaded: " + rs);
int newScore = Integer.parseInt(rs);
Integer newEntry = new Integer(newScore);
topScores.add(newEntry);
Collections.sort(topScores);
int rank = topScores.indexOf(newEntry) + 1;
String rankString = rankedInt(rank);
System.out.println("Rank: " + rankString);
// send back a response
res.setContentType("text/plain");
PrintWriter out = res.getWriter();
out.write(rankString);
out.close();
}
static public String rankedInt(int i)
{
String result = "";
String n = "" + i;
int lastNum = Integer.parseInt("" + n.charAt(n.length() - 1));
int lastTwoNums = 0;
if (n.length() > 1)
lastTwoNums = Integer.parseInt("" + n.charAt(n.length() - 2) + n.charAt(n.length() - 1));
if (lastNum >= 1 && lastNum <= 3)
{
if (lastTwoNums < 10 || lastTwoNums > 13)
{
if (lastNum == 1) result = "st";
if (lastNum == 2) result = "nd";
if (lastNum == 3) result = "rd";
}
}
else
result = "th";
return "" + i + result;
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -