📄 top.java
字号:
//导入相关的类
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.TextField;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.rms.*;
import java.io.*;
public class TOP extends Form implements CommandListener{
TextField name = null;//接受用户名
TextField scorce = null;//接受用户分数
Command add = null;//添加命令
Command show = null;//显示积分榜命令
Command back = null;//返回命令
RecordStore rs = null;//记录仓库
String userName[] = new String[5];//记录用户名
int userScorce[] = new int[5];//记录用户分数
//构造方法
public TOP()
{
super("scorce");//设置标题
openRS("TOP-5");//打开纪录仓库
init();//初始化界面
}
//初始化界面
public void init()
{
deleteAll();//删除界面以前的组件
name = new TextField("用户名:","",15,TextField.ANY);
scorce = new TextField("分数:","",10,TextField.NUMERIC);
add = new Command("Add",Command.OK,1);
show = new Command("Top",Command.BACK,1);
append(name);
append(scorce);
addCommand(add);
addCommand(show);
setCommandListener(this);
}
//软键事件处理
public void commandAction(Command c,Displayable d)
{
//添加记录
if(c.equals(add))
{
//获得用户的用户名和分数
String name = this.name.getString();
int scorce = Integer.parseInt(this.scorce.getString());
if(isInsert(scorce))//判断是否需要加入到记录仓库中
{
setRS(name,scorce);//将纪录添加到纪录仓库中
}
this.name.setString("");
this.scorce.setString("");
}else if(c.equals(show))//显示所有记录
{
deleteAll();//删除上一界面的组建
getAllRecord();//获得记录仓库中的所有记录
//将记录仓库中的信息显示在当前容器中
append(" 积分榜\n");
for(int i = 0;i < 5;i ++)
{
append((i+1)+" "+userName[i]+" "+userScorce[i]+'\n');
}
removeCommand(add);
removeCommand(show);
back = new Command("Back",Command.BACK,1);
addCommand(back);
}else if(c.equals(back))//返回输入界面
{
removeCommand(back);
init();
}
}
//打开记录仓库
public void openRS(String s)
{
if(rs == null)
{
try
{
rs = RecordStore.openRecordStore(s, false);
}catch(Exception e)
{
try
{
rs = RecordStore.openRecordStore(s, true);//新建一个记录仓库
createRS();//创建初始记录
}catch(Exception e1)
{
e1.printStackTrace();
}
}
}
}
//创建5条初始记录
public void createRS()
{
if(rs != null)
{
for(int i = 0;i < 5;i ++)
{
addRecord("null",0);//向记录仓库中添加记录
}
}
}
//添加记录
public int addRecord(String name,int scorce)
{
if(rs != null)
{
ByteArrayOutputStream bos = new ByteArrayOutputStream();//创建数组输出流
DataOutputStream dos = new DataOutputStream(bos);//数据输出流
try
{
dos.writeUTF(name);//向流中写入用户名
dos.writeInt(scorce);//向流中写入用户成绩
int temp =
rs.addRecord(bos.toByteArray(), 0, bos.toByteArray().length);//添加记录
dos.close();//关闭数据流
bos.close();//关闭流
return temp;
}catch(Exception e)
{
e.printStackTrace();
}
}
return -1;
}
//获得仓库中所有记录
public void getAllRecord()
{
if(rs != null)
{
for(int i = 1;i <= 5;i ++)
{
try
{
ByteArrayInputStream bis = new ByteArrayInputStream(rs.getRecord(i));//byte数组输入流
DataInputStream dis = new DataInputStream(bis);//数据输入流
userName[i-1] = dis.readUTF();//读取用户名
userScorce[i-1] = dis.readInt();//读取用户成绩
dis.close();//关闭数据输入流
bis.close();//关闭输入流
}catch(Exception e)
{
e.printStackTrace();
}
}
}
}
//判断新的分数是否需要加入到记录仓库中
public boolean isInsert(int scorce)
{
if(rs != null)
{
try
{
//获得记录仓库中最后一条记录
ByteArrayInputStream bis = new ByteArrayInputStream(rs.getRecord(5));
DataInputStream dis = new DataInputStream(bis);
dis.readUTF();
int temp = dis.readInt();
//判断当前成绩是否能添加到记录仓库
if(scorce > temp)
{
return true;
}
}catch(Exception e)
{
e.printStackTrace();
}
}
return false;
}
//把新的分数添加到记录仓库中
public void setRS(String name,int scorce)
{
System.out.println(name+","+scorce);
int temp = 0;
String n = null;
getAllRecord();
for(int i = 0;i < 5;i ++)
{
//判断分数应该插入到哪个位置
if(scorce > userScorce[i])
{
//交换用户成绩
temp = userScorce[i];
userScorce[i] = scorce;
scorce = temp;
//交换用户名
n = userName[i];
userName[i] = name;
name = n;
try
{
ByteArrayOutputStream bos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(bos);
dos.writeUTF(userName[i]);
dos.writeInt(userScorce[i]);
rs.setRecord(i + 1, bos.toByteArray(), 0, bos.toByteArray().length);//修改当前位置记录数据
dos.close();
bos.close();
}catch(Exception e)
{
e.printStackTrace();
}
}
}
}
//关闭记录仓库
public void close()
{
if(rs != null)
{
try
{
rs.closeRecordStore();//关闭打开的记录仓库
}catch(Exception e)
{
e.printStackTrace();
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -