计算机产生随机数,猜中即胜,猜不中,提示是大了还是小了,继续猜,直至猜到,给出所用时间和评语。保留用户测试成绩,做出成绩排行榜。排行榜存放到文件中。
开始界面
游戏界面
运行界面
排行榜.txt
主要流程
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Guider {
public static void main(String[] args) {
JFrame frame = new JFrame("猜数字游戏");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500, 300);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
JPanel panel = new JPanel();
panel.setLayout(null);
frame.add(panel);
JButton startBtn = new JButton("A 开始游戏");
startBtn.setBounds(120, 100, 100, 30);
panel.add(startBtn);
JButton exitBtn = new JButton("B 开始游戏");
exitBtn.setBounds(280, 100, 100, 30);
panel.add(exitBtn);
startBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
frame.dispose();
new Main("A");
}
});
exitBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
frame.dispose();
new Main("B");
}
});
}
}
计算猜数次数,生成排行榜文件
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.util.ArrayList;
import java.util.Date;
public class Main {
int count = 1;
// 计时
Date startTime = new Date();
Main(String name) {
JFrame frame = new JFrame("猜数字游戏");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500, 300);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
JPanel panel = new JPanel();
panel.setLayout(null);
frame.add(panel);
JLabel label = new JLabel("请输入一个1-100的数字");
label.setBounds(180, 20, 200, 30);
panel.add(label);
JTextField textField = new JTextField();
textField.setBounds(180, 60, 100, 30);
panel.add(textField);
JButton submitBtn = new JButton("提交");
submitBtn.setBounds(180, 100, 100, 30);
panel.add(submitBtn);
JButton exitBtn = new JButton("退出游戏");
exitBtn.setBounds(180, 140, 100, 30);
panel.add(exitBtn);
JLabel resultLabel = new JLabel();
resultLabel.setBounds(180, 180, 200, 30);
panel.add(resultLabel);
int random = (int) (Math.random() * 100 + 1);
System.out.println(random);
submitBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// 开始计时
if (count == 1) {
startTime = new Date();
}
String text = textField.getText();
int num = Integer.parseInt(text);
if (num > random) {
resultLabel.setText("第" + count + "次回答,猜大了");
} else if (num < random) {
resultLabel.setText("第" + count + "次回答,猜小了");
} else {
resultLabel.setText("第" + count + "次回答,猜对了");
// 结束计时
Date endTime = new Date();
long time = endTime.getTime() - startTime.getTime();
JOptionPane.showMessageDialog(null, "恭喜你,猜对了!用时" + time / 1000 + "秒");
pushRankingList(name, time / 1000, count);
}
count++;
}
});
exitBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
frame.dispose();
new Guider().main(null);
}
});
}
// 生成排行榜文件
public void pushRankingList(String name, long time, int count) {
// 创建文件
File file = new File("排行榜.txt");
if (!file.exists()) {
try {
file.createNewFile();
FileWriter fw = new FileWriter(file, true);
BufferedWriter bw = new BufferedWriter(fw);
bw.write("排名-姓名-用时(秒)-次数\n");
bw.close();
fw.close();
} catch (Exception e) {
e.printStackTrace();
}
}
try {
// 获取行数
FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);
int line = 0;
while (br.readLine() != null) {
line++;
}
br.close();
fr.close();
FileWriter fw = new FileWriter(file, true);
BufferedWriter bw = new BufferedWriter(fw);
bw.write(line + "-" + name + "-" + time + "-" + count + "次\n");
bw.close();
fw.close();
} catch (Exception e) {
e.printStackTrace();
}
sortRankinList();
}
// 给排行榜排序
public void sortRankinList() {
// 读取文件
ArrayList<String> list = new ArrayList<>();
File file = new File("排行榜.txt");
int lineNumber = 1;
try {
FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);
String line = br.readLine();
while (line != null) {
if (lineNumber > 1 && !line.isEmpty()) {
list.add(line);
}
line = br.readLine();
lineNumber++;
}
br.close();
fr.close();
} catch (Exception e) {
e.printStackTrace();
}
// 给排行榜排序
list.sort((o1, o2) -> {
String[] split1 = o1.split("-");
String[] split2 = o2.split("-");
return Integer.parseInt(split1[2]) - Integer.parseInt(split2[2]);
});
// 重新写入文件
try {
FileWriter fw = new FileWriter(file);
BufferedWriter bw = new BufferedWriter(fw);
bw.write("排名-姓名-用时(秒)-次数\n");
for (int i = 0; i < list.size(); i++) {
String name = list.get(i).split("-")[1];
String time = list.get(i).split("-")[2];
String count = list.get(i).split("-")[3];
bw.write((i + 1) + "-" + name + "-" + time + "-" + count + "\n");
}
bw.close();
fw.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
还有很多难的内容实现不了,在实际设计中才发现,书本上理论性的东西与在实际运用中的还是有一定的出入的,所以有些问题不但要深入地理解,而且要不断地更正以前的错误思维。一切问题必须要靠自己一点一滴的解决,而在解决的过程当中你会发现自己在飞速的提升。