控制台实现10道100以内的加减法算数运算测试
程序实现了 10道100以内的算术运算测试,答对得10分,答错不得分,选手答题结束可以把数据保存到数据库中,可以选择查询排行榜
程序涉及的技术栈有Java基础知识,MVC三层架构,JDBC,MySQL数据库。
项目结构
模型图
mapper层
userMapper接口
public interface UserMapper {
//添加比赛用户信息
int addUser(Connection connection, String username, int score);
//查询所有用户信息
List<User> getUsers(Connection connection);
}
userMapperImpl实现类
public class UserMapperImpl implements UserMapper {
@Override
public int addUser(Connection connection, String username, int score) {
PreparedStatement pstm = null;
int execute = 0;
try {
connection.setAutoCommit(false);
String sql = "insert into user_one values (?,?,?)";
Object[] params = {null, username, score};
execute = JdbcUtil.execute(connection, pstm, sql, params);
if (execute>0){
connection.commit();
}
} catch (SQLException e) {
e.printStackTrace();
try {
connection.rollback();
} catch (SQLException e1) {
e1.printStackTrace();
}
}finally {
JdbcUtil.closeResource(connection,pstm);
}
return execute;
}
@Override
public List<User> getUsers(Connection connection) {
PreparedStatement pstm = null;
ResultSet rs = null;
ArrayList<User> userList = new ArrayList<>();
User user;
try {
//asc
String sql = "select username,score from user_one order by score desc";
Object[] params = {};
rs = JdbcUtil.execute(connection, rs, pstm, sql, params);
while (rs.next()){
user = new User();
user.setUserName(rs.getString("username"));
user.setScore(rs.getInt("score"));
userList.add(user);
}
} catch (SQLException e) {
e.printStackTrace();
}
return userList;
}
}
service层
userService接口
public interface UserService {
//添加比赛用户信息
boolean addUser(String username, int score);
//查询所有用户信息
List<User> getUsers();
}
userServiceImpl实现类
public class UserServiceImpl implements UserService {
private UserMapper userMapper;
public UserServiceImpl() {
this.userMapper = new UserMapperImpl();
}
@Override
public boolean addUser(String username, int score) {
boolean flag = false;
Connection connection = JdbcUtil.getConnection();
if (userMapper.addUser(connection, username, score) > 0) {
flag = true;
}
JdbcUtil.closeResource(connection,null);
return flag;
}
@Override
public List<User> getUsers() {
Connection connection = JdbcUtil.getConnection();
List<User> userList = userMapper.getUsers(connection);
return userList;
}
}
controller层
conttroller
public class UserController {
UserServiceImpl userService = new UserServiceImpl();
/**
* 保存个人信息
* @param username
* @param score
*/
public boolean operation(String username,int score){
return userService.addUser(username, score);
}
/**
* 查询排行榜
*/
public List<User> getUser(){
List<User> userList = userService.getUsers();
return userList;
}
}
pojo实体类
public class User {
private String userName;
private int score;
public User() {
}
public User(String userName, int score) {
this.userName = userName;
this.score = score;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
@Override
public String toString() {
return "User{" +
"userName='" + userName + '\'' +
", score=" + score +
'}';
}
}
view层
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Random random = new Random();
UserController userController = new UserController();
//名次
int ranking = 1;
//分数
int score = 0;
int num1;
int num2;
int sum;
//用户运算结果
int userSum;
System.out.println("欢迎来到运算测试比赛!");
System.out.println("请输入你的姓名:");
String username = sc.nextLine();
for (int i = 1; i <= 5; i++) {
num1 = random.nextInt(101);
num2 = random.nextInt(101);
sum = num1-num2;
System.out.println(num1+"-"+num2+"=");
System.out.println("请输入你的运算结果");
userSum = sc.nextInt();
if (userSum==sum){
score+=10;
System.out.println("答对了,得10分");
}else {
System.out.println("答错了不得分!");
}
}
for (int j = 1; j <= 5; j++) {
num1 = random.nextInt(101);
num2 = random.nextInt(101);
sum = num1+num2;
System.out.println(num1+"+"+num2+"=");
System.out.println("请输入你的运算结果");
userSum = sc.nextInt();
if (userSum==sum){
score+=10;
System.out.println("答对了,得10分");
}else {
System.out.println("答错了不得分!");
}
}
//保存用户信息
boolean isSuccess = userController.operation(username, score);
if (isSuccess){
System.out.println("分数保存成功!");
}else {
System.out.println("分数保存失败!");
}
//是否查询排行榜
System.out.println("您是否查询排行榜! 是/否");
String isExcute = sc.next();
if ("是".equals(isExcute)){
List<User> userList = userController.getUser();
System.out.println("名次 姓名 分数");
for (User user : userList) {
System.out.println("第"+ranking+"名:"+user.getUserName()+"\t"+user.getScore());
ranking++;
}
}else {
System.out.println("感谢参加此次比赛!!");
}
}
}