2.3 进度安排
阶段一进度安排如表3-1所示。
表3-1 进度安排表
阶段
持续时间
阶段描述
输出
构思阶段
30min
需求分析
需求说明,功能模块图
设计阶段
30min
系统设计
设计说明-可以画流程图;数据库设计
实现阶段
3h
编写代码
项目工程源代码
5h
系统测试
进行黑盒测试(功能测试)-测试说明
运行阶段
1h
部署、运行
系统使用说明、运维报告、录制视频
3 系统设计
3.1 系统设计
项目二:猜数游戏
import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;
import java.io.FileWriter;
import java.io.IOException;
public
class Guess{
public
static void main(String[] args){
int
n=1;
int
[]a=new int[2];
while(n<=2)
{
int
cishu=0;
for(int
i=0;i<2;i++) {
System.out.println("请输入"+n+"号玩家猜的数字:");
Random
random = new Random(); //生成一个随机数
int
suiji = random.nextInt(100);
Scanner
input = new Scanner(System.in);
long
startTime = System.currentTimeMillis();
while(true) {
int
num =input.nextInt();
if(num>100||num<0)
{
System.out.println("游戏结束");
break;
}
else
if(num
System.out.println("您猜的数偏小了");cishu++;
}
else
if(num>suiji) {
System.out.println("您猜的数偏大了");cishu++;
}
else
{
System.out.println("恭喜您猜对了");cishu++;
break;
}
}n++;
long
endTime = System.currentTimeMillis();
long
Time = endTime-startTime;
System.out.println("共用"+cishu+"次猜对");
System.out.println("用时"+Time+"毫秒猜对");
a[i]=cishu;
startTime=0;endTime=0;
FileWriter fw = null;
try {
fw = new FileWriter("d:\\程序测试\\测试.txt", true);
fw.write("\n");
fw.write(+(n-1)+"号玩家所用次数为:");
String data =
Integer.toString(cishu);
fw.write(data);
fw.write("\n");
fw.write(+(n-1)+"号玩家所用时间为:");
String counts =
Integer.toString((int) Time);
String info = counts;
fw.write(info);
fw.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
}
System.out.println("根据次数排行榜为:");
Arrays.sort(a);
for(int
m=0;m
System.out.println(+a[m]);}
System.out.println("保存成功");
}
}
项目三:简易计算器
package com.gui.test;import javax.swing.*;
import javax.swing.JTextField;
import java.awt.*;
import java.awt.event.*;
import java.lang.*;
import java.awt.Color;
public class CalculateTest extends
JFrame implements ActionListener
{
private JPanel p1 = new JPanel();
//建立面板p1
private JPanel p2 = new JPanel();
//建立面板p2
private JTextField t1; //建立文本框t1
private JTextField t2; //建立文本框t2
private JLabel label; //声明标签
StringBuffer str; //设置显示屏为字符串形式
double x,y;
int z;
private JButton b[] = new
JButton[13]; //用数组创建13个按钮的空间
private JButton b1,b2,b3,b4,b5,b6,b7,b8,b9;
public CalculateTest() //构造方法
{
super("简易计算器"); //设置窗口的名称
Container c = getContentPane(); //创建面板对象
t1 = new JTextField(30); //给文本框t1分配空间
t1.setEditable(false); //使t1编辑框不能编辑
t2 = new JTextField(30); //给文本框t2分配空间
t2.setEditable(false); //使t2编辑框不能编辑
label = new JLabel("郑思聪设计的计算器"); //设置标签的名字
label.setForeground(Color.blue); //将前景设为蓝色
str=new StringBuffer();
p2.add(label); //在p2面板上添加标签
p2.add(t2); //在p2面板上添加文本框
p2.add(t1);
p2.setLayout(new GridLayout(5,1)); //设置四行一列的布局
for(int i=0;i<10;i++) //将数组中0到9设置为标签
{
String s=""+i;
b[i]= new JButton(s);
b[i].addActionListener(this); //添加监听对象
}
b[10]= new
JButton("-/+"); b[11]= new
JButton(".");
b1= new JButton("/"); b2= new JButton("Back");
b3= new JButton("*"); b4= new JButton("C");
b5= new JButton("+"); b6= new JButton("Sqrt");
b7= new JButton("-"); b8= new JButton("=");
b9= new JButton("^2");
for(int i=0;i<12;i++)
{
b[i].setForeground(Color.blue);
//设置前景颜色
}
b1.setForeground(Color.red); b3.setForeground(Color.red);
b5.setForeground(Color.red); b7.setForeground(Color.red);
b2.setForeground(Color.blue); b4.setForeground(Color.blue);
b6.setForeground(Color.red); b8.setForeground(Color.blue);
b9.setForeground(Color.red);
p1.add(b[7]); p1.add(b[8]);
p1.add(b[9]); p1.add(b1); p1.add(b2);
p1.add(b[4]); p1.add(b[5]);
p1.add(b[6]); p1.add(b3); p1.add(b4);
p1.add(b[1]); p1.add(b[2]);
p1.add(b[3]); p1.add(b5); p1.add(b6);
p1.add(b[0]); p1.add(b[10]);
p1.add(b[11]);p1.add(b7);p1.add(b8);
p1.add(b9); //添加面板
p1.setLayout(new
GridLayout(5,5,5,5)); //给按钮面板设置网格布局
b[10].addActionListener(this); b[11].addActionListener(this);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
b6.addActionListener(this);
b7.addActionListener(this);
b8.addActionListener(this);
b9.addActionListener(this);
//添加监听对象
c.add(p2);
c.add(p1);
c.setLayout(new FlowLayout()); //设置为顺序布局
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //设置按X时退出程序
setVisible(true); //将窗口隐藏
setResizable(false); //生成窗口的大小是固定的
setSize(400,400); //设定窗口的尺寸
}
public static void main(String[] args) //创建一个方法实现一个窗口
{ CalculateTest f = new
CalculateTest(); }
public void actionPerformed(ActionEvent e) //实现按钮的事件处理
{
try
{
if(e.getSource()==b4) //调用e.getSource()如果得到Jbutton点击的是b4
{
t1.setText("0"); //清零
t1.setHorizontalAlignment(JTextField.RIGHT); //设置水平对齐
str.setLength(0); //清空字符串缓冲区
}
else if(e.getSource()==b[10])
{
x=Double.parseDouble(t1.getText().trim()); //getText().trim():在获得得的文本中去除空格
t1.setText(""+(-x));
t1.setHorizontalAlignment(JTextField.RIGHT); //设置水平对齐
}
else if (e.getSource()==b5)
{
x=Double.parseDouble(t1.getText().trim()); //Double.parseDouble:把其他数据转化为double类型
str.setLength(0);
y=0d; //给y赋初始值0且这个数字类型为double
z=0;
}
else if(e.getSource()==b7)
{
x=Double.parseDouble(t1.getText().trim());
str.setLength(0); //setLength:设置动态数组长度
y=0d;
z=1;
}
else if(e.getSource()==b3)
{
x=Double.parseDouble(t1.getText().trim());
str.setLength(0);
y=0d;
z=2;
}
else if(e.getSource()==b1)
{
x=Double.parseDouble(t1.getText().trim());
str.setLength(0);
y=0d;
z=3;
}
else if(e.getSource()==b8)
{
str.setLength(0);
switch(z)
{
case 0:
t1.setText(""+(x+y)); t1.setHorizontalAlignment(JTextField.RIGHT);break;
case 1:
t1.setText(""+(x-y));
t1.setHorizontalAlignment(JTextField.RIGHT);break;
case 2:
t1.setText(""+(x*y));
t1.setHorizontalAlignment(JTextField.RIGHT);break;
case 3:
t1.setText(""+(x/y));
t1.setHorizontalAlignment(JTextField.RIGHT);break;
}
}
else if(e.getSource()==b9) {
x=Double.parseDouble(t1.getText().trim());
if(x<0)
{
t1.setText("数字格式异常");
t1.setHorizontalAlignment(JTextField.RIGHT); //设置水平对齐
}
else
{
t1.setText(""+Math.pow(x,2)); //添加平方
t1.setHorizontalAlignment(JTextField.RIGHT);
}
str.setLength(0);
y=0d;
}
else if(e.getSource()==b[11])
{
if(t1.getText().trim().indexOf('.')!=-1) //判断字符串中是否包含小数
{
}
else
{
if(t1.getText().trim().equals("0")) //初始值显示为0
{
t1.setText(str.append(e.getActionCommand()).toString());
t1.setHorizontalAlignment(JTextField.RIGHT);
}
else
if(t1.getText().trim().equals(""))
//初始值显示为空不做任何操作
{}
else
{
t1.setText(str.append(e.getActionCommand()).toString());
t1.setHorizontalAlignment(JTextField.RIGHT);
}
}
y=0d;
}
else if(e.getSource()==b6)
{
x=Double.parseDouble(t1.getText().trim());
if(x<0)
{
t1.setText("数字格式异常");
t1.setHorizontalAlignment(JTextField.RIGHT); //设置水平对齐
}
else
{
t1.setText(""+Math.sqrt(x)); //添加平方根
t1.setHorizontalAlignment(JTextField.RIGHT);
}
str.setLength(0);
y=0d;
}
else
{
if(e.getSource()==b[0]) //零的数字键
{
if(t1.getText().trim().equals("0"))//如果显示为零不做任何操作
{}
else
t1.setText(str.append(e.getActionCommand()).toString());
t1.setHorizontalAlignment(JTextField.RIGHT);
y=Double.parseDouble(t1.getText().trim());
}
else if (e.getSource()==b2) //back按键
{
if(!t1.getText().trim().equals("0")) //如果显示不为零
{
if(str.length()!=1)
{
t1.setText(str.delete(str.length()-1,str.length()).toString());
t1.setHorizontalAlignment(JTextField.RIGHT);
}
else
{
t1.setText("0");
t1.setHorizontalAlignment(JTextField.RIGHT);
str.setLength(0);
}
}
y=Double.parseDouble(t1.getText().trim());
}
else
{
t1.setText(str.append(e.getActionCommand()).toString());
t1.setHorizontalAlignment(JTextField.RIGHT);
y=Double.parseDouble(t1.getText().trim()); //把其他数据转化为double类型
}
}
}
catch(NumberFormatException e1){
t1.setText("数字格式异常");
t1.setHorizontalAlignment(JTextField.RIGHT); }
catch(StringIndexOutOfBoundsException
e1){t1.setText("字符串索引越界");
//Java API指出StringIndexOutOfBoundsException异常
t1.setHorizontalAlignment(JTextField.RIGHT);} //设置水平对齐
}
}