功能要求:实现十道100以内加减法数学题,能根据题目计算出答案,与输入答案对比,判断做题是否正确,最后计算分数。
添加排行榜功能存放到文件中。
文件存储技术
GUI
使用git进行代码管理
使用issue进行项目推进
使用了阿里巴巴代码扫描工具对全部代码进行了规范
面向对象程序设计
实现了排行榜文件功能
使用了git进行代码管理
郑颖 | 李存浩 |
---|---|
排行榜功能文件读写 | 计算界面功能 |
Git创建及用issue推进团队项目 | 注册功能 |
面向对象设计 | 登录功能 |
代码规范 | Git推进显目进度 |
登陆界面代码
@SuppressWarnings("serial")
class Login extends JFrame implements ActionListener{
JLabel userLa,pwdLa;
JTextField userTxt;
JPasswordField pwdTxt;
JButton sureBt;
JButton quitBt;
public static String name;
public static String D="登录";
public Login() {
init();
}
public void init() {
@SuppressWarnings("unused")
Frame frame=new Frame("登录");
userLa=new JLabel();
userLa.setText("账号:");
userLa.setSize(60,50);
userLa.setLocation(100,80);
pwdLa=new JLabel();
pwdLa.setText("密码:");
pwdLa.setSize(50,50);
pwdLa.setLocation(100, 120);
userTxt=new JTextField();
userTxt.setSize(100, 20);
userTxt.setLocation(170, 95);
pwdTxt=new JPasswordField();
pwdTxt.setSize(100,20);
pwdTxt.setLocation(170, 135);
sureBt=new JButton("登录");
sureBt.setSize(60, 25);
sureBt.setLocation(135, 260);
quitBt=new JButton("退出");
quitBt.setSize(60,25);
quitBt.setLocation(240, 260);
this.setLayout(null);
this.setSize(500,400);
this.add(userLa);
this.add(pwdLa);
this.add(userTxt);
this.add(pwdTxt);
this.add(sureBt);
this.add(quitBt);
sureBt.addActionListener(this);
quitBt.addActionListener(this);
setLocationRelativeTo(null);
this.setVisible(true);
}
@SuppressWarnings("deprecation")
@Override
public void actionPerformed(ActionEvent e) {
JButton bt=(JButton)e.getSource();
String str=bt.getText();
name = userTxt.getText().trim();
String pwd;
pwd= pwdTxt.getText().trim();
if(str.equals(D)) {
if(!checkisNull())
{
if(checkUserAndPwd(name,pwd))
{
this.setVisible(false);
JOptionPane pane = new JOptionPane("登录成功!");
JDialog dialog = pane.createDialog(this,"提示");
dialog.setVisible(true);
Game.over();
}
}else{
//如果错误则弹出一个显示框
JOptionPane pane = new JOptionPane("用户或密码错误");
JDialog dialog = pane.createDialog(this,"警告");
dialog.setVisible(true);
}
}
else {
System.exit(0);
}
}
public static String Space=" ";
@SuppressWarnings("deprecation")
private boolean checkisNull(){
boolean flag = false;
if(userTxt.getText().trim().equals(Space))
{
flag = true;
}
else{
if(pwdTxt.getText().trim().equals(Space)){
flag = true;
}
}
return flag;
}
运行结果
注册界面代码
public Resign() {
JPanel jp1=new JPanel();
JLabel jl1=new JLabel("账 号");
jtfName=new JTextField(15);
jp1.add(jl1);
jp1.add(jtfName);
JPanel jp2=new JPanel();
JLabel jl2=new JLabel("密 码");
jpf1=new JPasswordField(15);
jp2.add(jl2);
jp2.add(jpf1);
JPanel jp3=new JPanel();
JLabel jl3=new JLabel("确认密码");
jpf2=new JPasswordField(15);
jp3.add(jl3);
jp3.add(jpf2);
JPanel jpc=new JPanel(new GridLayout(3,1));
jpc.add(jp1);
jpc.add(jp2);
jpc.add(jp3);
add(jpc);
JPanel jps=new JPanel();
jbReset=new JButton("取消");
jbReset.addActionListener(this);
jbSingUp=new JButton("确定");
jbSingUp.addActionListener(this);
jps.add(jbReset);
jps.add(jbSingUp);
add(jps,BorderLayout.SOUTH);
setTitle("注册窗口");
setSize(300,285);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
@Override
public void actionPerformed(ActionEvent e) {
JButton jb=(JButton) e.getSource();
if(jb==jbReset) {
jtfName.setText("");
jpf1.setText("");
jpf2.setText("");
}
else if(jb==jbSingUp) {
String name=jtfName.getText().trim();
String pswd1=new String(jpf1.getPassword());
String pswd2=new String(jpf2.getPassword());
if(!pswd1.equals(pswd2)) {
JOptionPane.showMessageDialog(null,"两次密码不一致,请重新填写!");
}
String temp="";
if(name.equals(temp)||pswd1.equals(temp)||pswd2.equals(temp)) {
JOptionPane.showMessageDialog(null, "请完整填写所有的信息","提示",JOptionPane.WARNING_MESSAGE);
return ;
}
StringBuffer sb = new StringBuffer();
sb.append( name +","+ pswd1);
boolean flag = saveInfo(sb.toString());
if(flag) {
JOptionPane.showMessageDialog(null, "注册成功,保存成功");
this.setVisible(false);
new Login().setVisible(true);
}
else {
JOptionPane.showMessageDialog(null, "Sorry!保存失败,注册出了问题","IO出错",JOptionPane.ERROR_MESSAGE);
}
}
}
运行结果
登录文件读取代码
private boolean checkUserAndPwd(String name,String pwd){
boolean result = false;
try
{
FileReader file = new FileReader("D:\\user.txt");
BufferedReader bre = new BufferedReader(file);
String str = bre.readLine();
while(str!=null){
String[] strs = str.split(",");
if(strs[0].equals(name)){
if(strs[1].equals(pwd)) {
result = true;
}
}
str = bre.readLine();
}
file.close();
}
catch(Exception ex)
{
System.out.print("1");
}
return result;
}
注册界面代码
public boolean saveInfo(String info) {
BufferedWriter bw = null;
try {
// 在文件末尾自动追加
bw = new BufferedWriter(new FileWriter(FILE_PATH, true));
bw.write(info);
bw.newLine();
bw.flush();
} catch (IOException e) {
JOptionPane.showMessageDialog(null, "Sorry!保存失败,注册出了问题","IO出错",JOptionPane.ERROR_MESSAGE);
} finally {
if (bw != null) {
try {
bw.close();
} catch (IOException e) {
JOptionPane.showMessageDialog(null, "Sorry!保存失败,注册出了问题","IO出错",JOptionPane.ERROR_MESSAGE);
}
}
}
return true;
}
/**
* 随机创建算术题
*/
public static void createRandom() {
// int a,b,cint;
// String c = null;
Random random = new Random();
// list为题目(标签)集合,题目在标签中呈现
for (JLabel lable : list) {
a = random.nextInt(101);
b = random.nextInt(101);
cint = random.nextInt(2);
switch (cint) {
case 0:
c = "+";
randomRange();// 创建规定算数题范围函数,即和不大于100,差不小于0
lable.setText(a + c + b + "=");
// list2为所有题目正确答案的集合
list2.add(a + b);
break;
default:
c = "-";
randomRange();// 创建规定算数题范围函数,即和不大于100,差不小于0
lable.setText(a + c + b + "=");
// list2为所有题目正确答案的集合
list2.add(a - b);
break;
}
}
}
public static int number3=100;
public static void randomRange() {// 创建规定算数题范围函数,即和不大于100,差不小于0
Random r = new Random();
switch (c) {
case "+":
while (a + b > number3) {
a = r.nextInt(101);
b = r.nextInt(101);
if (a + b <= 100) {
}
}
break;
default:
while (a - b < 0) {
a = r.nextInt(101);
b = r.nextInt(101);
if (a - b >= 0) {
break;
}
}
break;
}
public Game() {
// 初始化按钮
Clearbutton = new JButton("清空");
Resetbutton = new JButton("重置");
Printfbutton = new JButton("提交");
Rankbutton = new JButton("排行");
// 设置按钮大小
Clearbutton.setSize(50, 100);
Resetbutton.setSize(50, 100);
Printfbutton.setSize(50, 100);
Printfbutton.setSize(50, 100);
// 初始化计数面板
Countpanel = new JPanel();
Countpanel.setLayout(new GridLayout(5, 4));
// 初始化按钮面板
buttonpanel = new JPanel();
buttonpanel.setLayout(new FlowLayout());
buttonpanel.setSize(600, 50);
buttonpanel.setBorder(BorderFactory.createLineBorder(Color.GREEN));
buttonpanel.add(Clearbutton);
CountLable = new JLabel(" ");
CountLable.setBorder(BorderFactory.createLineBorder(Color.BLACK));
// 将按钮添加至按钮及结果面板中
buttonpanel.add(Resetbutton);
buttonpanel.add(Printfbutton);
buttonpanel.add(CountLable);
buttonpanel.add(Rankbutton);
// 为计数面板添加标签和文本框
// CountLable();
// 设置窗体属性
setLayout(new BorderLayout());
setBounds(100, 100, 400, 300);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container container = getContentPane();
// 将计数面板,按钮面板放置在容器中指定位置
container.add(Countpanel, BorderLayout.NORTH);
container.add(buttonpanel, BorderLayout.SOUTH);
}
/**
* 设置CountPanel面板中的组件
*/
public static void countLable() {
for (i = 0; i < number1; i++) {
JLabel label = new JLabel();
// 重点:在设置布局的条件下,调用setPreferredSize(new Dimension(80, 20))可以设置标签的大小
// 重点:而不能调用setsize()方法
label.setPreferredSize(new Dimension(80, 20));
label.setFont(new Font("楷体", Font.BOLD, 20));
// 设置标签的边界
label.setBorder(BorderFactory.createLineBorder(Color.RED));
// list为题目(标签)集合,题目在标签中呈现
list.add(label);
JTextField textField = new JTextField(4);
// list1为用户输入计算结果的文本框集合
list1.add(textField);
}
for (i = 0; i < number2; i++) {
// 如果余数为0,则添加标签
if (i % 2 == 0) {
Countpanel.add(list.get(j));
j++;
}
// 如果余数为1,则添加文本框
else {
Countpanel.add(list1.get(k));
k++;
}
}
createRandom();// 调用创建随机题目的方法CreateRandom();
myaddactionListener();// 调用按钮的动作监听方法myAddActionLIstener()
}
/**
* 为按钮添加动作监听方法
*/
public static void myaddactionListener() {
CountLable.setText("");
// 为清空按钮添加动作监听
Clearbutton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
for (JLabel clearlable : list) {
clearlable.setText("");
}
for (JTextField cleartextField : list1) {
cleartextField.setText("");
}
}
});
// 为重置按钮添加动作监听
Resetbutton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
CountLable.setText("");
createRandom();
for (JTextField cleartextField : list1) {
cleartextField.setText("");
}
}
});
// 为打印按钮添加监听
Printfbutton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
Integer i;
Double result;
num = 0;
// 获取用户在文本框中输入的计算结果
for (i = 0; i < list1.size(); i++) {// list为用户输入计算结果的文本框集合
// Integer.parseInt()方法用于将字符串转换成数字
userlist.add(Integer.parseInt(list1.get(i).getText()));
}
// 计算用户做对的题目个数
for (i = 0; i < userlist.size(); i++) {
if (userlist.get(i) == list2.get(i)) {// list2为所有题目正确答案的集合
num++; // num为用户计算正确的个数
}
}
// 计算用户做题的正确率
result = ((num * 1.0) / (list.size()) * 100);
CountLable.setText(result + "%");
stu=new Stu(Login.name,result.toString());
}
});
// 为排行按钮添加动作监听
Rankbutton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
@SuppressWarnings("unused")
Rank rank=new Rank(stu);
}
});
}
运行结果
class ScoreComparator implements Comparator<Stu>{
@Override
public int compare(Stu o1, Stu o2) {
return (int) (Double.valueOf(o2.getScore())-Double.valueOf(o1.getScore()));
}
}
public Rank(Stu s) {
fileRead();
//判断strList是否有同名
int con=1;
for(Stu e:stuList) {
if(e.getName().equals(s.getName())) {
con=0;
e.setScore(s.getScore());
}
}
if(con==1) {
stuList.add(s);
}
Collections.sort(stuList, new ScoreComparator());
for(int z=1;z<=stuList.size();z++) {
stuList.get(z-1).setRank(z);
if(stuList.get(z-1).getName().equals(s.getName())) {
s.setRank(z);
}
}
// 确保一个漂亮的外观风格
JFrame.setDefaultLookAndFeelDecorated(true);
// 创建及设置窗口
JFrame frame = new JFrame("Rank");
frame.setSize(100, 50);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// 添加rank标签
JLabel label = new JLabel("你的排名为:"+s.getRank());
frame.getContentPane().add(label);
// 显示窗口
frame.setVisible(true);
fileWrite();
运行结果
public void fileRead() {
String name = null;
String score;
String rank = null;
File file = new File(filePath);
try {
if (!file.exists()) {
file.createNewFile();
}
// 创建文件输入流对象
FileReader fr = new FileReader(file);
//创建数组储存输入流字符
char c[]=new char[(int)file.length()];
// 写入数据到输出流
fr.read(c);
fr.close();
String str=String.valueOf(c);
str=str.replace(‘\n‘, ‘ ‘);
str=str.replace(‘\r‘, ‘ ‘);
String[] fstr=str.split("\\s+");
for(int i=0;i<fstr.length;i++) {
if(i%3==0) {
rank=fstr[i];
if(rank==null) {
rank="0";
}
}
else if(i%3==2){
score=fstr[i];
Stu s=new Stu(Integer.valueOf(rank),name,score);
stuList.add(s);
}
else if(i%3==1) {
name=fstr[i];
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
public void fileWrite() {
File file = new File(filePath);
try {
if (!file.exists()) {
file.createNewFile();
}
// 创建文件输出流
FileWriter fw = new FileWriter(file);
// 使用缓冲区数据流封装输出流
BufferedWriter bw = new BufferedWriter(fw);
for(int j=0;j<stuList.size();j++) {
// 写入数据到输出流
bw.write(stuList.get(j).toString());
// 写入换行符
bw.newLine();
// 刷新缓冲区
bw.flush();
}
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
https://gitee.com/ach333we/the-warehouse-of-ach.git
? 此次课设我们发现自己对GUI的创建十分不熟悉,希望今后能够多多加强这方面的学习,弥补自己在该方面的不足。同时此次课设未能实现多人联机挑战的功能,十分可惜,希望在今后生活能够完善该功能。还希望自己在以后学习生涯中能够多多运用git进行团队协作。我们组存储和读取数据的方式主要时文件存储和读取的,经过这次的课程设计。我们更加注重学习和掌握利用MyQSL对数据进行增删改查的功能,还有就是本次课程设计中计算时间可能要利用多线程技术,自己对多线程也并没完全的掌握,自己也希望能够掌握多线程实现基本的功能。
原文:https://www.cnblogs.com/Ach333we/p/14341938.html