181111

18111131

 

 

 

 

 

 

 

 

 

Java课程设计

(阶段一文档)

 

       
   
 
     

 

 

 

 

 

 

 

 

 


二〇二〇年七月

 

目    录

1 选题.......................................................................................................................... 1

3 系统需求分析.......................................................................................................... 2

3.1 系统可行性分析........................................................................................... 2

3.2 系统需求分析............................................................................................... 2

3.3 项目进度安排............................................................................................... 2

4 系统设计.................................................................................................................. 4

4.1 系统设计....................................................................................................... 4

4.2 数据库设计(有则写-无则删除,如果是文件直接写文件)................. 4

5 系统实现.................................................................................................................. 6

5.1 项目一实现................................................................................................... 6

5.2 项目二实现................................................. 6

6 系统测试.................................................................................................................. 8

7 结论和心得.............................................................................................................. 9

 

 

1 选题

选题一

算数运算测试

题目要求

实现10道随机加减法的计算与得分排序

使用Java知识

1.switch-case语句

2.for()循环语句

3. Java  I/O

4.Random随机数

 

选题二

猜数游戏

题目要求

实现产生随机数,然后用户猜数,告知猜的大了还是小了。最后得出时间

使用Java知识

  1. if-else语句
  2. while语句
  3. Random随机数
  4. Java I/O

 

 

 

 


2 系统需求分析

2.1 系统可行性分析

项目一:算数运算测试。只需使用生成的100以内的随机数通过组合生成计算式,通过if条件语句来判断正误给予打分。而排行榜功能,则只需将历次分数存入文件,在以数组的形式读取文件并排序再次存入文件中,此时通过打印文件内容,即可得到排行榜的内容,即实现排行榜的功能。

项目二:猜数游戏。用Random关键字使计算机产生一个100以内的随机数,用户录入数字,使录入的与正确答案做比较,猜中即胜,猜不中,提示是大了还是小了, 继续猜,猜到结束,给出所用时间和评语。

 

2.2 系统需求分析

项目一:算数运算测试。实现十道 100 以内随机加减法题,根据题目给出答案,与输入答案对比,判断做题是否正确,最后计算分数。

项目二:猜数游戏。计算机产生随机数,猜中即胜,猜不中,提示是大了还是小了, 继续猜,猜到游戏结束,给出所用时间。

 

2.3 进度安排

阶段一进度安排如表1-2和2-2所示。

表1-2 算数运算测试进度安排表

阶段

持续时间

阶段描述

输出

构思阶段

0.5小时

需求分析

需求说明,功能模块图

设计阶段

1小时

系统设计

设计说明-可以画流程图;数据库设计

实现阶段

3小时

编写代码

项目工程源代码

0.5小时

系统测试

进行黑盒测试(功能测试)-测试说明

运行阶段

1小时

部署、运行

系统使用说明、运维报告、录制视频

 

                        

 

 

 

 

表1-2 猜数游戏进度安排表

阶段

持续时间

阶段描述

输出

构思阶段

0.5小时

需求分析

需求说明,功能模块图

设计阶段

1小时

系统设计

设计说明-可以画流程图;数据库设计

实现阶段

3小时

编写代码

项目工程源代码

0.5小时

系统测试

进行黑盒测试(功能测试)-测试说明

运行阶段

1小时

部署、运行

系统使用说明、运维报告、录制视频


3 系统设计

3.1 系统设计

根据系统的实际功能需求分析,对系统功能部分进行了设计。

项目一:

       package ks1;

/*

*需求:完成10道100以内的加减法运算式,得到总分,并排行

 */

import java.io.*;

import java.util.*;

public class NumberTest {

    public static void main(String[] args) throws FileNotFoundException {

        Scanner in = new Scanner(System.in);

        Random rd = new Random();

        //标准答案

        int d = 0;

        //您猜的数

        int guess = 0;

        //获得的分数

        int count = 0;

 

        boolean t;

 

        //控控制题目在10道以内

        for (int i = 0; i < 10; i++) {

            t = false;

            //若生成随机算式不合格则重新生成

            while (t == false) {

                //定义两个加数为100以内的随机数

                int a = rd.nextInt(100);

                int b = rd.nextInt(100);

                int c = rd.nextInt(2);

                //随机设置a和b的加/减法运算

                switch (c) {

                    //加法运算

                    case 0:

                        if (a + b <= 100) {

                            t = true;

                            d = a + b;

                            System.out.println(a + "+" + b + "= ?");

                        } else {

                            t = false;

                        }

                        break;

                    //减法运算

                    case 1:

                        //判断被减数与减数之间的大小,确保差是非负数

                        if (a > b) {

                            t = true;

                            d = a - b;

                            System.out.println(a + "-" + b + "= ?");

                        } else {

                            t = true;

                            d = b - a;

                            System.out.println(b + "-" + a + "= ?");

                        }

                        break;

                }

 

            }

            guess = in.nextInt();

            if (guess == d) {

                count += 10;

                System.out.println("Right!");

            } else {

                System.out.println("Wrong!");

            }

        }

             in.close();

        //得出总分

        System.out.println("Your scored a total of " + count + " runs. ");

 

        //将分数写进文件中

        FileWriter fw = null;

        try {

            fw = new FileWriter("F:\\Idea Info\\src\\com\\kcsj\\Scores.txt", true);

            String counts = Integer.toString(count);

            String info = counts;

            fw.write(info);

            //换行追加

            fw.write("\n");

            fw.flush();

        } catch (IOException e) {

            e.printStackTrace();

        }

 

 

 

        //读取电脑中的文本文件

        Scanner fenshu = new Scanner(new File("F:\\Idea Info\\src\\com\\kcsj\\Scores.txt"));

             int []arr = new int[100];

             while(fenshu.hasNext()){

                    if(fenshu.hasNextDouble()){

              arr[num++] = fenshu.nextInt();     

                    }

                    else fenshu.next();

             }

             try{

                    FileReader fr = new FileReader("F:\\Idea Info\\src\\com\\kcsj\\Scores.txt");

                    fw = new FileWriter("F:\\Idea Info\\src\\com\\kcsj\\Scores.txt"); 

              int ch; 

              while((ch = fr.read()) != -1){ 

                fw.write(ch); 

               }

              fr.close(); 

              fw.close(); 

                    FileOutputStream fos = new FileOutputStream("F:\\Idea Info\\src\\com\\kcsj\\Scores.txt",true); 

                    fos.write(sort(arr).getBytes());

                    fos.close();

             }catch(IOException ex){

                    System.out.println("无法打开文件!");

             }

      }

   //将成绩进行排序

    static int num=0;

    //排名

    static int n = 0;

   public static String sort(int[] arr){

             int i;

             String str = "\r\n成绩排名为: ";

       for(i = num - 2;i >= 0;i--){

                    for(int j = 0;j <= i;j++){

                           if(arr[j] < arr[j+1]){

                                  int temp;

                                  temp = arr[j];

                 arr[j] = arr[j+1];

                                  arr[j+1] = temp;

                           }

                    }

             }

             System.out.println("排行榜如下:");

             for(n = 0;n < num;n++){

                    str += arr[n] + " ";

                    System.out.println(arr[n]);

             }

             return str;

      }

}

 

项目二:

    package com.kcsj.ks2;

 

import java.io.*;

import java.util.Random;

import java.util.Scanner;

 

public class GuessTest {

    public static void main(String[] args) {

        Scanner in = new Scanner(System.in);

        Random rd = new Random();

        int p = rd.nextInt(100);

        System.out.println("已生成一个小于100的正整数,请输入您猜的数字:");

        //获取事件开始时间

        long startTime = System.currentTimeMillis();

        while(true) {

                int s = in.nextInt();

                if(s>100||s<0) {

                    System.out.println("游戏结束!");

                    break;

                }

                else if(s<p) {

                    System.out.println("猜小了!");

                }

                else if(s>p) {

                    System.out.println("猜大了!");

                }

                else {

                    System.out.println("猜对了!");

                    break;

                }

        }

        //获取事件结束时间

        long endTime = System.currentTimeMillis();

        long usedTime = (endTime-startTime)/1000;

        System.out.println("耗时"+usedTime+"s");

        if(usedTime<10){

            System.out.println("猜的又快又准!");

        }else{

            System.out.println("猜的有点慢,下次请争分夺秒!");

        }

 

 

        //将分数写进文件中

        FileWriter fw = null;

        try {

            //换行追加

 

            fw = new FileWriter("F:\\Idea Info\\src\\com\\kcsj\\Guess.txt", true);

            fw.write("\n");

            String counts = Integer.toString((int) usedTime);

            String info = counts;

            fw.write(info);

 

            fw.flush();

        } catch (IOException e) {

            e.printStackTrace();

        }

 

        //读取电脑中的文本文件

        Scanner mill = null;

        try {

            mill = new Scanner(new File("F:\\Idea Info\\src\\com\\kcsj\\Guess.txt"));

        } catch (FileNotFoundException e) {

            e.printStackTrace();

        }

        int []arr = new int[100];

        while(mill.hasNext()){

            if(mill.hasNextDouble()){

                arr[num++] = mill.nextInt();

            }

            else mill.next();

        }

        try{

            FileReader fr = new FileReader("F:\\Idea Info\\src\\com\\kcsj\\Guess.txt");

            fw = new FileWriter("F:\\Idea Info\\src\\com\\kcsj\\Guess.txt");

            int ch;

            while((ch = fr.read()) != -1){

                fw.write(ch);

            }

            fr.close();

            fw.close();

            FileOutputStream fos = new FileOutputStream("F:\\Idea Info\\src\\com\\kcsj\\Guess.txt",true);

            fos.write(sort(arr).getBytes());

            fos.close();

        }catch(IOException ex){

            System.out.println("无法打开文件!");

        }

    }

    //将历史时间进行排序

    static int num=0;

    static int n = 0;

    public static String sort(int[] arr){

        int i=0;

        String str = "\r时间排名为: ";

        for(i = num - 2;i >= 0;i--){

            for(int j = 0;j <= i;j++){

                if(arr[j] > arr[j+1]){

                    int temp;

                    temp = arr[j];

                    arr[j] = arr[j+1];

                    arr[j+1] = temp;

                }

            }

        }

        System.out.println("排行榜如下:");

        for(n = 0;n < num;n++){

            str += arr[n] + " ";

            System.out.println(arr[n]);

        }

        return str;

    }

}

 

4 系统实现

4.1 算数运算测试实现

项目一运行结果如图1-3所示。

 

图1-3 项目一控制台界面

4.2 猜数游戏实现 

 

图2-3  猜数游戏控制台界面

 

 

5 系统测试

算术运算模块测试中主要对生成随机计算式、给出得分、查看排行榜的功能模块进行测试,测试结果如表1-4所示。

表1-4 算术运算模块测试表

编号

测试功能

输入描述

预期结果

运行结果

HP01

功能

1.生成计算式

2.获取测试得分

3.查看排行榜

可以生成计算式,并且能够测试得分后显示排行榜

正常,与预期结果一致

HP02

生成计算式功能

1.生成随机数

2.生成随机加减号

3.生成随机运算式

.生成10道100以内的加减随机运算式

正常,与预期结果一致

HP03

生成分数功能

  1. .生成随机运算式
  2. 参与答题
  3. 得出总分

参与答题,得出总分

正常,与预期结果一致

 

表2-4 猜数游戏模块测试表

编号

测试功能

输入描述

预期结果

运行结果

HP01

功能

4.生成随机数

5.获取用时

6.查看排行榜

可以生成随机数,并且能够得到游戏用时后显示排行榜

正常,与预期结果一致

HP02

生成计算式功能

1.生成随机数

 

.生成一个100以内随机整数

正常,与预期结果一致

HP03

生成游戏用时功能

  1. .生成随机整数
  2. 参与游戏
  3. 得出用时

参与答题,得出总时间

正常,与预期结果一致

 

测试主要是针对用户的操作,从测试结果中可以看出该模块的所有功能均能正常实现,且测试结果与预期结果一致。

 

 

6 结论和心得

Java设计这门课程,让我把学习了一学期的java知识用到实际上,自己思考和设计题目解答,也让我知道了平时学习的短板之处,能够及时的弥补了缺少的部分。

posted on 2020-06-28 16:35  181111  阅读(21)  评论(0)    收藏  举报

导航