性格测试数据统计(JAVA实现)

新开了一门外教课程,Object-oriented Programming(JAVA), 记录一些学习经验,以及部分和c++的区别感悟。本文为性格测试数据统计作业demo, 大概要求是对一个.txt文件里的数据进行统计并打印输出要求格式。
text输入文件格式如下:

Zhang San
BABAAAABAAAAAAABAAAABBAAAAAABAAAABABAABAAABABABAABAAAAAABAAAAAABAAAAAA

text输出文件格式如下:

Zhang San: [90, 15, 10, 10] = ISTJ

注:数字为选项B的比例,后边的字母为对应的性格,详见:Keirsy Temperament Sorter

相比上一篇文章(降雨绘图(JAVA实现)),本次作业的主要学习总结如下:

  • class constant可以强增代码的可读性,对于编写文档有帮助,但有时也会使程序不那么灵活
  • 数组array(包括int[] String[]等)是用引用方式references传递的
  • 一条一条读进数据,再一条一条写到文件中,个人觉得更加合理。(因为当数据量较大时,内存可能无法一次读进来所有数据,有点像深度学习里训练网络需要用个生成器,边训练边送数据)

自己的总结与思考,目前水平有限,若有建议欢迎指教! 代码如下:

package demo;import java.util.*;
import java.io.*;public class Personality {/** Some magic number of this program* */// most of arrays' capacity.private static final int FOUR = 4;// question group of personality.private static final int QUEGROUP = 10;// question number of one group.private static final int QUENUM = 7;// dimension index for combine i loop.private static final int[] DIM = {0, 1, 1, 2, 2, 3, 3};/** Four dimension of personality test* X mean 50% result* */private static final String[] DIM0 = {"E", "I"};private static final String[] DIM1 = {"S", "N"};private static final String[] DIM2 = {"T", "F"};private static final String[] DIM3 = {"J", "P"};private static final String[][] PERDIM= {DIM0, DIM1, DIM2, DIM3};private static final String X = "X";// relative pathprivate static final String PATH = "./";public static void main(String[] args) throws FileNotFoundException {Scanner console_input = new Scanner(System.in);openMenu();Scanner input_file = readInString(console_input);stasticDbFile(input_file, console_input);closeMenu();}/** short introduction* */private static void openMenu(){System.out.println("You can obtain statistics data of the personality test in this program");}/** Ask the user for the name of the input.* If not exist, ask again.* */private static Scanner readInString(Scanner console_input){while (true){System.out.print("personality test database file name? ");String file_name = console_input.nextLine().trim();String path = PATH + file_name;try {Scanner input_file = new Scanner(new File(path));return input_file;}catch ( Exception e ) {System.out.println("Please check the file name!");}}}/** Read the input file.* Name the output file.* */private static void stasticDbFile(Scanner input_file, Scanner console_input) throws FileNotFoundException{String ouput_file = readOutString(console_input);PrintStream output = new PrintStream(new File(ouput_file));;while ( input_file.hasNextLine() ) {String name = input_file.nextLine();String data = input_file.nextLine();statistics(name, data, output);}input_file.close();}/** Ask the user for the name of the output.* */private static String readOutString(Scanner console_input){System.out.print("statistics data file name(ouput)? ");String file_name = console_input.nextLine().trim();return file_name;}/** statistics the input file.* three main parts(methods) all in this part.* compareAB: estimate A or B or X.* percentB: B percent of every dimension.* perType: dimension chose.* */private static void statistics(String name, String data, PrintStream output){// counts of A and Bint A_count[] = new int[FOUR]; int B_count[] = new int[FOUR];for (int i = 0; i < QUEGROUP; i++){// every group have the same structure of questions.int group = i * QUENUM;for (int num = 0; num < QUENUM; num++){// DIM use here to combine the i and question dimension.// For example, i = 1 and 2, are the DIM1("S" or "N") questioncompareAB(A_count, B_count, data.charAt(num+group), DIM[num]);}      }int[] B_percent =  percentB(A_count, B_count);String pertype = perType(B_percent);// print the data of One person in the output file.printAnswer(name, B_percent, pertype, output);}private static void compareAB(int[] A_count, int[] B_count, char data, int dim){if (data == 'A' || data == 'a'){A_count[dim]++;}if (data == 'B' || data == 'b'){B_count[dim]++;}}private static int[] percentB(int[] A_count, int[] B_count){int[] B_percent = new int[FOUR];for (int i = 0; i < FOUR; i++){// turn in the double, than back to int.B_percent[i] = (int) Math.round( ( ((double)B_count[i]) / ((double)(A_count[i]+B_count[i])) ) * 100 );}return B_percent;}private static String perType(int[] B_percent){String pertype = " ";for (int i = 0; i < FOUR; i++){if (B_percent[i] > 50) { pertype = pertype + PERDIM[i][1]; }else if (B_percent[i] < 50) { pertype = pertype + PERDIM[i][0]; }else { pertype = pertype + X; }}return pertype;}/** print the data of One person in the output file.* */private static void printAnswer(String name, int[] B_percent, String pertype, PrintStream output){output.println(name + ": [" + B_percent[0]+", "+ B_percent[1]+", "+B_percent[2]+", "+B_percent[3] + "] =" + pertype);}/** Say goodbye to user.* a flag of program is finished* */private static void closeMenu(){System.out.println("statistics finshed! Bye!");}}

性格测试数据统计(JAVA实现)相关推荐

  1. 统计java文件中的代码行数

    统计Java代码行数工具类  --  CodeCounterUtil.java 统计指定目录下的java文件中代码行数  --  public static int  getCodeNumFromFo ...

  2. linux中统计java数量,linux 统计当前目录下文件数

    查看当前目录下文件的个数 ls -l | grep "^-" | wc -l 查看当前目录下文件的个数,包括子目录里的. ls -lR| grep "^-" | ...

  3. 体育测试数据绘图软件,原创健身运动体育测试数据统计app界面

    原创设计原创健身运动体育测试数据统计app...素材是用户QQ173F1B6A上传到我图网,素材大小为0.65 MB,素材的尺寸为1024px×1780px,图片的编号是27962446,颜色模式为R ...

  4. 7-4 统计Java程序中关键词的出现次数 (25 分)

    ** 7-4 统计Java程序中关键词的出现次数 (25 分) ** 编写程序统计一个输入的Java源码中关键字(区分大小写)出现的次数.说明如下: Java中共有53个关键字(自行百度) 从键盘输入 ...

  5. 运动会分数统计 java语言

    运动会分数统计java语言 运动会分数统计* 设计内容: 参加运动会有n个学校,学校编号为1--n.比赛分成m个男子项目,和w个女子项目.项目编号为男子1--m,女子m叶+1--m+w.不同的项目取前 ...

  6. Python编程 统计Java源文件代码行数,注释行数,空白行数

    每次学习新的语言,就想重新实现一遍做过的课设=,= 这里实现的是"综合性实验 Java源代码分析程序"的第三部分 第二部分见:[Python编程]统计目录下Java源文件的关键字出 ...

  7. 统计Java进程中的线程状态(jstack+linux)

    统计Java进程中的线程状态 第一步:查看Java进程ID 第二步:使用jstack命令dump线程信息,看看pid=45890进程中的线程都是什么状态 /usr/local/java/jdk1.8. ...

  8. java 网络流量统计_IP数据包的流量统计(JAVA语言)

    如果不显示图片,或者出现排版错误,抽风,崩坏,节操丢失等一系列不正常情况,请点击下面链接观看,点我 IP数据包的流量统计(JAVA语言) 要求:编制程序,监控网络,捕获一段时间内网络上的IP数据包,按 ...

  9. Elasticsearch Aggregation 多个字段分组统计 Java API实现

    2019独角兽企业重金招聘Python工程师标准>>> 现有索引数据: index:school type:student ----------------------------- ...

最新文章

  1. JAVA基础知识之Set集合
  2. hdu2063 二分图(基础题)
  3. SpringBoot踩坑记录 Invalid bound statement (not found)引发的一些列问题
  4. 深拷贝浅拷贝的一些理解
  5. Mobile Web调试工具Weinre
  6. u盾 签名pdf显示 “签名于修订版中删除”_Adobe Acrobat Reader DC for mac(免费pdf阅读)...
  7. Arduino 操作BT008蓝牙串口模块
  8. “onkeyup”=按键抬起触发,“onafterpaste”=粘贴之后触发
  9. 科研笔记(六) 基于环境感知的室内路径规划方法
  10. 迷宫_随机实验_边做边学深度强化学习:PyTorch程序设计实践(1)
  11. 点在多边形内算法,JS判断一个点是否在一个复杂多边形的内部
  12. 干货 | 使用京东云搭建视频直播网站
  13. Scrapy 2.6 Items 数据项定义、加载、传输使用指南
  14. python中对象的定义_python中对象的定义和使用
  15. 企业公司网站模板 免费下载
  16. KILE5生成bin文件
  17. WEB安全师傅们的博客友情链接
  18. Allegro 16 板内挖孔
  19. 产品经理(PM)素质与技能初总结
  20. 中级数据开发工程师养成计

热门文章

  1. 金蝶登录显示服务器离线,金蝶云服务器已离线 登陆不了
  2. 易语言基础——模块的编写和使用
  3. 企业服务器采用虚拟化技术的利弊
  4. 离线版百度地图瓦片下载html版
  5. 红米note+开机+android,红米note清除数据进入方法 红米note三清教程
  6. Ubuntu20.04或18.04下PX4(pixhawk)源码编译环境配置教程,及构建代码各种错误解决办法
  7. IE浏览器插件注册表位置
  8. N33-Week 3-向日葵
  9. python 中 True 和 False、逻辑运算符
  10. 联想笔记本键盘亮屏幕不亮_机械师笔记本电脑屏幕不亮但键盘亮,而且开不开机维修...