猜数游戏和模拟简易计算器
目 录
1 选题 2
2 系统需求分析 3
3.1 系统可行性分析 3
3.2 系统需求分析 3
3.3 项目进度安排 5
3 系统设计 4
3.1 系统设计 5
3.2 数据库设计(有则写-无则删除,如果是文件直接写文件) 5
4 系统实现 6
4.1代码实现 6
4.2运行界面实现 16
7 结论和心得 22
1 选题
|
选题一 |
猜数游戏 |
|
题目要求 |
|
|
功能基本要求:输入用户名、密码,进行猜数游戏:计算机产生随机数(1-100之间),猜中即胜,猜不中,提示是大了还是小了,继续猜,直至猜到;猜对了输入Y继续进行第二轮猜数,输入N退出游戏(没人最多进行五次猜数)。连接数据库将用户名、密码、每一次猜数几次猜对进行存储。 能力要求:1.沟通表达能力;2:资料搜集能力;3. 自主学习能力;4.时间规划能力5.独立完成项目能力;6.文档撰写能力;7.面向对象编程思维和软件工程思想。 |
|
|
使用Java知识 |
|
|
Java基本输入输出、运算符、随机数、while循环、for循环、System.exit(-1)、break、数据类型转换、选择分支、数据库连接。 |
|
|
选题二 |
简易计算器模拟程序 |
|
题目要求 |
|
|
功能基本要求:仿照Windows计算器,编写一具有GUI的计算器,点击对应的数字计算符号按钮,进行相对应的整数的加、减、乘、除四则运算。每次计算存储到文件test.txt中。 能力要求:1.沟通表达能力;2:资料搜集能力;3. 自主学习能力;4.时间规划能力5.独立完成项目能力;6.文档撰写能力;7.面向对象编程思维和软件工程思想。
|
|
|
使用Java知识 |
|
|
GUI图形用户界面编程(布局、组件、事件处理)、类和对象、封装继承、IO流、异常等。
|
|
2 系统需求分析
2.1 系统可行性分析
一、猜数游戏
1技术可行性
本次程序运用的是eclipse这个软件中的JDBC数据库连接,采用的java语言,java面向对象,MySQL建立表 ,所以技术上是可行的。
2.经济可行性
因为本次程序使用的eclipse软件是免费的,在这个软件上就能满足我们的需求,我们不需要额外的开销,所以经济上也是可行的。
3.操作可行性
本次程序只需要用户有这个软件并配置好了环境变量并且安装windowbuilder插件就可以使用,操作简单便捷,符合大部用户的使用习惯。因此在操作上是可行的。
二、简易计算器模拟程序
1技术可行性
本次程序运用的是eclipse这个软件中的GUI插件技术,采用的java语言,java面向对象,GUI基础语言,所以技术上是可行的。
2.经济可行性
因为本次程序使用的eclipse软件是免费的,在这个软件上就能满足我们的需求,我们不需要额外的开销,所以经济上也是可行的。
3.操作可行性
本次程序只需要用户有这个软件并配置好了环境变量并且安装windowbuilder插件就可以使用,操作简单便捷,符合大部用户的使用习惯。因此在操作上是可行的。
2.2 系统需求分析
一、猜数游戏
1、需求分析
输入用户名、密码,进行猜数游戏:计算机产生随机数(1-100之间),猜中即胜,猜不中,提示是大了还是小了,继续猜,直至猜到;猜对了输入Y继续进行第二轮猜数,输入N退出游戏(没人最多进行五次猜数)。连接数据库将用户名、密码、每一次猜数几次猜对进行存储。
2、功能模块图
二、简易计算器模拟程序
1、需求分析
仿照Windows计算器,编写一具有GUI的计算器,点击对应的数字计算符号按钮,进行相对应的整数的加、减、乘、除四则运算。每次计算存储到文件test.txt中。
3、功能模块图
2.3 进度安排
阶段一进度安排如表3-1所示。
表3-1 进度安排表
|
阶段 |
持续时间 |
阶段描述 |
输出 |
|
构思阶段 |
2小时 |
需求分析 |
需求说明,功能模块图 |
|
设计阶段 |
2小时 |
系统设计 |
设计说明-可以画流程图;数据库设计 |
|
实现阶段 |
2天 |
编写代码 |
项目工程源代码 |
|
1天 |
系统测试 |
进行黑盒测试(功能测试)-测试说明 |
|
|
运行阶段 |
1天 |
部署、运行 |
系统使用说明、运维报告、录制视频 |
3 系统设计
3.1 系统设计
一、猜数游戏
根据系统的实际功能需求分析,对系统功能部分进行了设计。
随机数生成模块:运行过后,系统自动生成1-100以内的随机数。
判断随机数与输入模块:判断随机数与输入的数字是否相等,给出猜大或猜小提示,直到猜对为止。
次数记录模块:每猜对一次记录一次,记录每次猜对次数。
退出功能模块:三种方式退出程序:
1、输入不在1-100以内的数字退出。
2、输入N不在继续进行猜数。
3、猜数次数达到上限5次自动退出。
二、简易计算器模拟程序
根据系统的实际功能需求分析,对系统功能部分进行了设计。
计算模块:运行过后,弹出GUI图形界面,点击相对应的数字和运算符按钮,进行加减乘除简单四则运算。
保存模块:将相对应的运算和运算结果保存在文件中
退出功能模块:点击小程序上关闭按钮,退出程序
3.2 数据库(文件)设计
一、猜数游戏
表3.2-1 玩家信息表
|
序号 |
字段名 |
数据类型 |
是否为空 |
说明 |
|
1 |
id |
int |
否 |
玩家ID |
|
2 |
username |
varchar |
是 |
玩家账号 |
|
3 |
password |
varchar |
是 |
玩家密码 |
|
4 |
count |
int |
是 |
玩家几次猜对 |
二、简易计算器模拟程序
为了让用户清楚的知道自己进行了那些计算,创建了test.txt保存用户的运算及结果。
4 系统实现
4.1代码实现
1、猜数游戏
import java.sql.*;
import java.util.Scanner;
/*输入用户名、密码,进行猜数游戏:计算机产生随机数(1-100之间),
* 猜中即胜,猜不中,提示是大了还是小了,继续猜,直至猜到;
* 猜对了输入Y继续进行第二轮猜数,输入N退出游戏(没人最多进行五次猜数)。
* 连接数据库将用户名、密码、每一次猜数几次猜对进行存储。
* */
public class Connect {
public static void main(String[] args) {
Scanner input = new Scanner(System.in); //键盘录入一个数
System.out.println("请输入用户名:");
String name = input.next();
System.out.println("请输入密码:");
String password = input.next();
System.out.println("请输入1-100以内的整数:");
//保存内容
saveContent(input, name, password);
}
public static void saveContent(Scanner input, String name, String password) {
int guest = input.nextInt();
int count = 1;
int x = 1;
int num = 0;//自动生成的数
boolean bl = true; //是否继续进行猜数
while (bl == true) {
num = (int) (Math.random() * 100 + 1); //自动生成1-100之间的数字
while (guest > 1 && guest < 100 && guest != num) { //猜的数在1-100之间且不等于生成的数时
if (guest < num) {
System.out.println("数字偏小,请重新输入");
} else if (guest > num) {
System.out.println("数字偏大,请重新输入");
}
count++;
guest = input.nextInt();
}
if (guest < 1 || guest > 100) { //当猜的数不在1-100范围门内,结束
System.out.println("输入错误");
bl = false;
}
if (guest == num) {
System.out.println("恭喜,猜对了!,猜了" + count + "次");
System.out.println("答案:" + num);
//存储猜数次数
save(name, password, count);
System.out.println("是否继续进行猜数游戏?Y为继续输入,N为退出,最多可猜5次");
String str = input.next(); //键盘输入一个数组类型的值
String str1 = "Y"; //定义一个数组等于Y
if (str.equals(str1)) {
bl = true;
x++;
while (x > 5) {
System.out.println("已到达最大次数,退出程序!");
System.exit(-1);
}
System.out.println("请重新输入1-100以内的整数:");
guest = input.nextInt();
count = 1;
} else {
System.out.println("一共进行了" + x + "次猜数游戏");
System.out.println("退出程序!");
break;
}
}
}
}
private static void save(String name, String pwd, int count) {
Connection connection = null;
Statement statement = null;
try {
//设置参数
String driverClass = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/guessgames?useUnicode=true&characterEncoding=utf8";
String username = "root";
String password = "yyfjiayou0";
//加载数据库驱动
Class.forName(driverClass);
//获取Conection对象
connection = DriverManager.getConnection(url, username, password);
//获取Statement对象
statement = connection.createStatement();
//添加次数
String sql = "insert into guess_number(`username`, `password`, `count`) values (" + name + "," + pwd + "," + count +")";
statement.execute(sql);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (statement != null) {
statement.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
if (connection != null) {
connection.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
二、简易计算器模拟程序
package com.ts.personal;
/*仿照Windows计算器,编写一具有GUI的计算器,点击对应的数字计算符号按钮,
* 进行相对应的整数的加、减、乘、除四则运算。每次计算存储到文件test.txt中。
* */
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JLabel;
import java.awt.event.ActionListener;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.awt.event.ActionEvent;
import java.awt.Font;
import java.awt.Color;
public class Calculator extends JFrame {
private JPanel contentPane;
//定义自己
private JFrame jframe;
private JTextField textField;
static int sum = 0;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Calculator frame = new Calculator();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Calculator() {
setTitle("计算器");
//构造器赋值
jframe=this;
//窗体关闭,退出整个程序
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//尺寸设置
setBounds(100, 100, 500, 500);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
textField = new JTextField();
textField.setFont(new Font("宋体", Font.PLAIN, 24));
textField.setBackground(Color.WHITE);
textField.setBounds(10, 63, 296, 143);
contentPane.add(textField);
textField.setColumns(10);
JPanel panel = new JPanel();
panel.setBounds(8, 213, 213, 238);
contentPane.add(panel);
JButton button_8 = new JButton("7");
button_8.setFont(new Font("宋体", Font.PLAIN, 32));
panel.add(button_8);
button_8.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText()+7);
}
});
JButton button_9 = new JButton("8");
button_9.setFont(new Font("宋体", Font.PLAIN, 32));
panel.add(button_9);
button_9.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText()+8);
}
});
JButton button_10 = new JButton("9");
button_10.setFont(new Font("宋体", Font.PLAIN, 32));
panel.add(button_10);
JButton button_5 = new JButton("4");
button_5.setFont(new Font("宋体", Font.PLAIN, 32));
panel.add(button_5);
button_5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText()+4);
}
});
JButton button_6 = new JButton("5");
button_6.setFont(new Font("宋体", Font.PLAIN, 32));
panel.add(button_6);
button_6.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText()+5);
}
});
JButton button_7 = new JButton("6");
button_7.setFont(new Font("宋体", Font.PLAIN, 32));
panel.add(button_7);
JButton button_2 = new JButton("1");
button_2.setFont(new Font("宋体", Font.PLAIN, 32));
panel.add(button_2);
button_2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText()+1);
}
});
JButton button_3 = new JButton("2");
button_3.setFont(new Font("宋体", Font.PLAIN, 32));
panel.add(button_3);
button_3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText()+2);
}
});
JButton button_4 = new JButton("3");
button_4.setFont(new Font("宋体", Font.PLAIN, 32));
panel.add(button_4);
JButton btnNewButton = new JButton("0");
btnNewButton.setFont(new Font("宋体", Font.PLAIN, 32));
panel.add(btnNewButton);
JButton btnC_1 = new JButton("c");
panel.add(btnC_1);
btnC_1.setFont(new Font("宋体", Font.PLAIN, 32));
//加法
JButton button_11 = new JButton("=");
panel.add(button_11);
button_11.setFont(new Font("宋体", Font.PLAIN, 32));
button_11.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText()+"=");
String str=textField.getText();
save();
for(int i=0;i<str.length();i++) {
String a=str.substring(i, i+1);
if(a.equals("/")) {
sum=Integer.parseInt((String) str.subSequence(0, i))/Integer.parseInt(str.substring(i+1,str.length()-1));
textField.setText(sum+"\r\n");
}
if(a.equals("×")) {
sum=Integer.parseInt((String) str.subSequence(0, i))*Integer.parseInt(str.substring(i+1,str.length()-1));
textField.setText(sum+"\r\n");
}
if(a.equals("+")) {
sum=Integer.parseInt((String) str.subSequence(0, i))+Integer.parseInt(str.substring(i+1,str.length()-1));
textField.setText(sum+"\r\n");
}
if(a.equals("-")) {
sum=Integer.parseInt((String) str.subSequence(0, i))-Integer.parseInt(str.substring(i+1,str.length()-1));
textField.setText(sum+"\r\n");
}
}
save();
}
});
btnC_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText("");
}
});
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
textField.setText(textField.getText()+0);
}
});
JPanel panel_1 = new JPanel();
panel_1.setBounds(196, 218, 105, 243);
contentPane.add(panel_1);
JButton button_13 = new JButton("+");
button_13.setFont(new Font("宋体", Font.PLAIN, 32));
panel_1.add(button_13);
button_13.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText()+"+");
}
});
JButton button_12 = new JButton("-");
button_12.setFont(new Font("宋体", Font.PLAIN, 32));
panel_1.add(button_12);
JButton button_1 = new JButton("/");
button_1.setFont(new Font("宋体", Font.PLAIN, 32));
panel_1.add(button_1);
JButton btnC = new JButton("\u00D7");
panel_1.add(btnC);
btnC.setFont(new Font("宋体", Font.PLAIN, 23));
btnC.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText()+"×");
}
});
button_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText()+"/");
}
});
button_12.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText()+"-");
}
});
button_4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText()+3);
}
});
button_7.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText()+6);
}
});
button_10.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText()+9);
}
});
}
public void save() {
FileOutputStream fos =null;
try {
String str=textField.getText();
fos =new FileOutputStream("C:\\Users\\Administrator\\Desktop\\1\\java\\test.txt",true);
byte[] words =str.getBytes();
fos.write(words,0,words.length);
System.out.println("已写入文件");
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
4.2运行界面实现
一、猜数游戏
二、简易计算器模拟程序
5 结论和心得
心得:通过这次的学习真的让自己成长的不少,学会了很多新知识,复习了很多旧知识。虽然过程非常的艰辛,期间遇到了很多难题,但是通过老师的指导以及自己上网百度,翻阅资料顺利的做了下来。我相信只要有付出就会有收获。今后我一定会继续学习,丰富自己的知识。

浙公网安备 33010602011771号