java实现猜数游戏 要求有用户登录等GUI界面,用户数据存在文件里,并体现面向对象编程思想。具体:计算机产生随机数,猜中即胜,猜不中,提示大了或小了,继续猜,直至猜到,给出所用时间。 (2)要求有用户登录,记录用户成绩; (3)给出排行榜。 分成几个类实现,给出完整代码
时间: 2024-03-22 13:42:37 浏览: 52
java实现猜数游戏
以下是一个简单的Java猜数字游戏的示例代码,它实现了GUI界面、用户登录、记录用户成绩和排行榜等功能,同时体现了面向对象编程思想:
1. User类:表示游戏中的用户,主要包含用户名和密码等信息。
```java
public class User {
private String username;
private String password;
private int score;
public User(String username, String password) {
this.username = username;
this.password = password;
this.score = 0;
}
public String getUsername() {
return username;
}
public String getPassword() {
return password;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
public String toString() {
return username + "\t" + score;
}
}
```
2. UserList类:表示用户列表,主要包含用户的增加、删除、查找和排序等功能。
```java
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
public class UserList {
private ArrayList<User> userList;
public UserList() {
userList = new ArrayList<User>();
}
public void addUser(User user) {
userList.add(user);
}
public void removeUser(User user) {
userList.remove(user);
}
public User findUser(String username) {
for (User user : userList) {
if (user.getUsername().equals(username)) {
return user;
}
}
return null;
}
public void sortUsers() {
Collections.sort(userList, new Comparator<User>() {
public int compare(User u1, User u2) {
return u2.getScore() - u1.getScore();
}
});
}
public ArrayList<User> getUsers() {
return userList;
}
}
```
3. Game类:表示游戏,主要包含产生随机数、判断猜测大小和计算时间等功能。
```java
public class Game {
private int answer;
private long startTime;
public Game() {
answer = (int) (Math.random() * 100) + 1;
startTime = System.currentTimeMillis();
}
public int getAnswer() {
return answer;
}
public String guess(int number) {
if (number < answer) {
return "猜小了!";
} else if (number > answer) {
return "猜大了!";
} else {
long endTime = System.currentTimeMillis();
int time = (int) (endTime - startTime) / 1000;
return "恭喜你,猜对了!用时" + time + "秒。";
}
}
}
```
4. Main类:表示程序入口,主要包含用户登录、游戏进行和排行榜显示等功能。
```java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Main {
private JFrame frame;
private JPanel loginPanel;
private JPanel gamePanel;
private JPanel scorePanel;
private JLabel usernameLabel;
private JTextField usernameField;
private JLabel passwordLabel;
private JPasswordField passwordField;
private JButton loginButton;
private JLabel gameLabel;
private JTextField gameField;
private JButton guessButton;
private JTextArea resultArea;
private JTextArea scoreArea;
private UserList userList;
private User currentUser;
private Game currentGame;
public Main() {
initGUI();
userList = new UserList();
}
private void initGUI() {
frame = new JFrame("猜数字游戏");
frame.setSize(500, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
loginPanel = new JPanel(new GridLayout(3, 2));
usernameLabel = new JLabel("用户名:");
usernameField = new JTextField();
passwordLabel = new JLabel("密码:");
passwordField = new JPasswordField();
loginButton = new JButton("登录");
loginButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String username = usernameField.getText();
String password = new String(passwordField.getPassword());
User user = userList.findUser(username);
if (user != null && user.getPassword().equals(password)) {
currentUser = user;
gamePanel.setVisible(true);
scorePanel.setVisible(false);
gameField.requestFocus();
} else {
JOptionPane.showMessageDialog(frame, "用户名或密码错误!");
}
}
});
loginPanel.add(usernameLabel);
loginPanel.add(usernameField);
loginPanel.add(passwordLabel);
loginPanel.add(passwordField);
loginPanel.add(new JLabel());
loginPanel.add(loginButton);
gamePanel = new JPanel(new BorderLayout());
gameLabel = new JLabel("猜一个1到100的整数:");
gameField = new JTextField();
guessButton = new JButton("猜");
guessButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String text = gameField.getText();
int number = 0;
try {
number = Integer.parseInt(text);
} catch (NumberFormatException ex) {
JOptionPane.showMessageDialog(frame, "请输入有效的整数!");
return;
}
String result = currentGame.guess(number);
resultArea.append(text + ":" + result + "\n");
if (result.startsWith("恭喜")) {
currentUser.setScore(currentGame.getAnswer() * 100 / (int) (System.currentTimeMillis() - currentGame.startTime));
userList.sortUsers();
scoreArea.setText(getScoreText());
gamePanel.setVisible(false);
scorePanel.setVisible(true);
} else {
gameField.setText("");
gameField.requestFocus();
}
}
});
resultArea = new JTextArea(10, 30);
resultArea.setEditable(false);
JScrollPane resultScrollPane = new JScrollPane(resultArea);
JPanel gameButtonPanel = new JPanel();
gameButtonPanel.add(guessButton);
gamePanel.add(gameLabel, BorderLayout.NORTH);
gamePanel.add(gameField, BorderLayout.CENTER);
gamePanel.add(gameButtonPanel, BorderLayout.SOUTH);
gamePanel.add(resultScrollPane, BorderLayout.EAST);
scorePanel = new JPanel(new BorderLayout());
scoreArea = new JTextArea(10, 30);
scoreArea.setEditable(false);
JScrollPane scoreScrollPane = new JScrollPane(scoreArea);
scorePanel.add(scoreScrollPane, BorderLayout.CENTER);
frame.add(loginPanel);
frame.add(gamePanel);
frame.add(scorePanel);
gamePanel.setVisible(false);
scorePanel.setVisible(false);
frame.setVisible(true);
}
private String getScoreText() {
StringBuilder sb = new StringBuilder();
sb.append("排行榜:\n");
sb.append("用户名\t得分\n");
for (User user : userList.getUsers()) {
sb.append(user.toString() + "\n");
}
return sb.toString();
}
public static void main(String[] args) {
new Main();
}
}
```
代码中使用了Swing组件库来实现GUI界面,同时使用了JOptionPane来显示错误提示信息和JScrollPane来实现滚动条功能。运行程序后,首先会显示登录界面,用户输入用户名和密码后点击登录按钮即可进入游戏界面。在游戏界面中,用户输入猜测的数字并点击猜按钮,程序会根据猜测的大小给出提示,并在猜对后显示用户得分并将其保存到用户列表中。在游戏结束后,用户可以查看排行榜来比较自己的得分。
阅读全文