一、问题描述及基本要求
【问题描述】
参加计算机设计大赛的n个学校编号为1~n,赛事分成m个项目,项目的编号为1~m.比赛获奖按照得分降序,取前三名,写一个统计程序产生各种成绩单和得分报表。
【基本要求】
1)每个比赛项目至少有10支参赛队;每个学校最多有6支队伍参赛;
2)能统计各学校的总分;
3)可以按照学校编号或名称查询,学校的总分、各项目的总分排序输出;
4)可以按学校编号查询学校某个项目的获奖情况;可以按项目编号查询取得前三名的学校;
5)数据存入文件并能随时查询
【设计要求】
1)输入数据形式和范围:可以输入学校的名称,赛事项目的名称。
2)输出形式:有中文提示,各学校分数为整数
3)界面要求:交互设计要合理,每个功能可以设立菜单,根据提示,完成相关功能的要求。
4)存储结构:学生自己根据系统功能要求自己设计,但是赛事相关数据要存储在文件中。
【测试数据】
要求使用全部合法数据,整体非法数据,局部非法数据。进行程序测试,以保证程序的稳定。
【实现提示】
假设3<赛事项目数量<=10,学校名称长度不超过20个字符。每个赛事结束时,将其编号、名称输入,并依次输入参赛学校编号、学校名称和成绩。
二、问题分析和任务定义
题目中没有给我们相应数据来处理,所以要自己生成。可以自己手动输入,也可以通过随机生成。在生成时要注意以下两点:
- 总的比赛个数(a)和总的队伍数(b)要满足一定的条件:
b/a>=10(每个比赛要至少10个队伍,每个学校最多6支队伍,所以学校数与比赛项目数有制约);
2、用随机数时要注意使用,不然会生成一样的数据。
需要统计的数据:
1、每个学校的总分 ;
2、每种比赛的分数排名;
3、每个学校的获奖情况 ;
4、每种比赛的获奖情况。
需要完成的功能:
1、照学校编号或名称查询,学校的总分输出;
2、各项目的总分排序输出;
3、按学校编号查询学校某个项目的获奖情况;
4、可以按项目编号查询取得前三名的学校(注意并列的情况);
三、项目代码
package E1;
import java.util.ArrayList;
public class Competition {
public String name;
public int num;
public ArrayList<School>teamscore;
public Competition(String name, int num) {
this.name = name;
this.num = num;
teamscore=new ArrayList<School>();
}
public Competition(String name, int num, ArrayList<School> teamscore) {
this.name = name;
this.num = num;
this.teamscore = teamscore;
}
}
--------------------------------------------------------------
package E1;
import java.io.*;
import java.util.*;
import static java.lang.System.exit;
public class renwu1 {
public static ArrayList<Competition> coms;
public static void main(String[] args) throws IOException {
coms = new ArrayList<Competition>();
totalinput();
try{
loadfile();
}
catch (FileNotFoundException e)
{
BufferedWriter fw = new BufferedWriter(new FileWriter("test.txt"));
fw.close();
}
while (true) {
System.out.println("中国计算机设计大赛赛事统计");
System.out.println("请输入您的选择:");
System.out.println("1.添加竞赛成绩");
System.out.println("2.查找竞赛成绩");
System.out.println("3.查看学校成绩");
System.out.println("4.储存数据文件");
System.out.println("5.清空数据文件");
System.out.println("0.退出统计程序");
Scanner sc = new Scanner(System.in);
int choice = sc.nextInt();
switch (choice) {
case 0:
exit(0);
cls();
break;
case 1:
addscore();
cls();
break;
case 2:
findscore();
cls();
break;
case 3:
schoolscoref();
cls();
break;
case 4:
savefile();
cls();
break;
case 5:
clearfile();
cls();
break;
}
}
}
private static void clearfile() throws IOException {
BufferedWriter fw = new BufferedWriter(new FileWriter("test.txt"));
fw.close();
System.out.println("文件已经清空");
}
private static void savefile() throws IOException {
BufferedWriter fw = new BufferedWriter(new FileWriter("test.txt"));
for (int i = 0; i < coms.size(); i++) {
Competition com = coms.get(i);
ArrayList<School> ts = com.teamscore;
fw.write(com.name);
fw.newLine();
fw.write(String.valueOf(com.num));
fw.newLine();
fw.write(String.valueOf(ts.size()));
fw.newLine();
for (int i1 = 0; i1 < ts.size(); i1++) {
School s = ts.get(i1);
fw.write(s.name);
fw.newLine();
fw.write(String.valueOf(s.num));
fw.newLine();
fw.write(String.valueOf(s.score));
fw.newLine();
}
}
fw.close();
}
private static void schoolscoref() {
Scanner sc = new Scanner(System.in);
System.out.println("若用学校名称查找请输入1,若用学校编号查找请输入2,若返回请输入3。");
int choic;
try {
choic = sc.nextInt();
} catch (InputMismatchException e) {
System.out.println("输入格式错误,已返回菜单");
return;
}
switch (choic) {
case 1:
System.out.println("请输入你要查找学校的名称:");
String name = sc.next();
boolean flag = false;
int allsum = 0;
for (int i = 0; i < coms.size(); i++) {
Competition com = coms.get(i);
boolean f1 = false;
ArrayList<School> ar = new ArrayList<School>();
ar.sort(new Comparator<School>() {
@Override
public int compare(School o1, School o2) {
return o1.score - o2.score;
}
});
for (int i1 = 0; i1 < com.teamscore.size(); i1++) {
School s = com.teamscore.get(i1);
if (s.name.equals(name)) {
ar.add(s);
flag = true;
f1 = true;
}
}
if (f1) {
int sum = 0;
System.out.println(com.name + "比赛的成绩为:");
for (int i1 = 0; i1 < ar.size(); i1++) {
School school = ar.get(i1);
sum += school.score;
System.out.print(school.score + "\t");
}
System.out.println();
System.out.println("该项目总分为:" + sum);
allsum += sum;
}
}
if (!flag) {
System.out.println("未找到该学校成绩,已返回主页面");
return;
} else System.out.println("学校总分为:" + allsum);
break;
case 2:
System.out.println("请输入你要查找学校的编号:");
int id;
try{
id = sc.nextInt();
}catch (InputMismatchException e)
{
System.out.println("输入格式错误,已返回菜单");
return;
}
boolean flag2 = false;//该学校存在成绩
int allsum2 = 0;
for (int i = 0; i < coms.size(); i++) {
Competition com = coms.get(i);
boolean f1 = false;//该比赛有项目
ArrayList<School> ar = new ArrayList<School>();
ar.sort(new Comparator<School>() {
@Override
public int compare(School o1, School o2) {
return o1.score - o2.score;
}
});
for (int i1 = 0; i1 < com.teamscore.size(); i1++) {
School s = com.teamscore.get(i1);
if (s.num == id) {
ar.add(s);
flag2 = true;
f1 = true;
}
}
if (f1) {
int sum = 0;
System.out.println(com.name + "比赛的成绩为:");
for (int i1 = 0; i1 < ar.size(); i1++) {
School school = ar.get(i1);
sum += school.score;
System.out.print(school.score + "\t");
}
System.out.println();
System.out.println("该项目总分为:" + sum);
allsum2 += sum;
}
}
if (!flag2) {
System.out.println("未找到该学校成绩,已返回主页面");
return;
} else System.out.println("学校总分为:" + allsum2);
break;
case 3:
System.out.println("已返回主页面");
return;
default:
System.out.println("输入错误,已返回主页面");
return;
}
//打印学校成绩
}
private static void cls() {
System.out.println();
System.out.println("____________________________________________________________________________________________");
System.out.println("____________________________________________________________________________________________");
System.out.println("____________________________________________________________________________________________");
System.out.println();
}
private static void findscore() {
Scanner sc = new Scanner(System.in);
System.out.println("若用比赛名称查找请输入1,若用比赛编号查找请输入2,若返回请输入3。");
int choi;
try {
choi = sc.nextInt();
} catch (InputMismatchException e) {
System.out.println("输入格式错误,已返回菜单");
return;
}
Competition c = null;
switch (choi) {
case 1:
System.out.println("请输入你要查找竞赛的名称:");
String name = sc.next();
boolean flag = false;
for (int i = 0; i < coms.size(); i++) {
Competition com = coms.get(i);
if (com.name.equals(name)) {
c = com;
flag = true;
break;
}
}
if (!flag) {
System.out.println("未找到该比赛,已返回主页面");
return;
}
break;
case 2:
System.out.println("请输入你要查找竞赛的编号:");
int id;
try {
id = sc.nextInt();
} catch (InputMismatchException e) {
System.out.println("输入格式错误,已返回菜单");
return;
}
boolean flag2 = false;
for (int i = 0; i < coms.size(); i++) {
Competition com = coms.get(i);
if (com.num == id) {
c = com;
flag2 = true;
break;
}
}
if (!flag2) {
System.out.println("未找到该比赛,已返回主页面");
return;
}
break;
case 3:
System.out.println("已返回主页面");
return;
default:
System.out.println("输入错误,已返回主页面");
return;
}
printcomscore(c);
}
private static void printcomscore(Competition c) {
System.out.println(c.name + "的成绩如下:");
System.out.println("学校\t\t学校编号\t\t成绩");
ArrayList<School> ar = new ArrayList<School>();
for (int i = 0; i < c.teamscore.size(); i++) {
School school = c.teamscore.get(i);
System.out.println(school.name + "\t\t" + school.num + "\t\t" + school.score);
boolean flag = false;
//是否ar有含有学校
for (int i1 = 0; i1 < ar.size(); i1++) {
School ss = ar.get(i1);
if (ss.num == school.num) {
flag = true;
ar.set(i1, new School(school.name, school.num, school.score + ss.score));
break;
}
}
if (!flag) {
ar.add(new School(school.name, school.num, school.score));
}
}
ar.sort(new Comparator<School>() {
@Override
public int compare(School o1, School o2) {
return o2.score - o1.score;
}
});
System.out.println(c.name + "比赛学校总分分数前三为:");
for (int i = 0; i < Math.min(ar.size(), 3); i++) {
School ss = ar.get(i);
System.out.println(ss.name + "\t\t" + ss.num + "\t\t" + ss.score);
}
}
private static void loadfile() throws IOException {
BufferedReader br = new BufferedReader(new FileReader("test.txt"));
String line;
while ((line = br.readLine()) != null) {
String s1 = line;
String s2 = br.readLine();
String s3 = br.readLine();
ArrayList<School> ar = new ArrayList<School>();
for (int i = 0; i < Integer.parseInt(s3); i++) {
ar.add(new School(br.readLine(), Integer.parseInt(br.readLine()), Integer.parseInt(br.readLine())));
}
coms.add(new Competition(s1, Integer.parseInt(s2), ar));
}
br.close();
}
private static void totalinput() {
}
private static void addscore() {
Scanner sc = new Scanner(System.in);
try {
System.out.println("请输入你要添加竞赛的编号:");
int matchnum = sc.nextInt();
System.out.println("请输入你要添加竞赛的名称:");
String matchname = sc.next();
Competition com = null;
boolean flag = false;//是否存在竞赛
if (coms.size() != 0) {
for (int i = 0; i < coms.size(); i++) {
Competition c = coms.get(i);
if (c.num == matchnum) {
flag = true;
com = c;
System.out.println("已选中已存在竞赛");
break;
//存在该竞赛
}
}
}
//不存在该竞赛
if (!flag) {
com = new Competition(matchname, matchnum);
}
Map<Integer, Integer> maxteam = new HashMap<Integer, Integer>();
while (true) {
System.out.println("请输入你要添加学校的编号:");
int schoolnum = sc.nextInt();
System.out.println("请输入你要添加学校的名称:");
String schoolname = sc.next();
System.out.println("请输入你要添加学校队伍的成绩:");
int score = sc.nextInt();
School sch = new School(schoolname, schoolnum, score);
if (!maxteam.containsKey(schoolnum)) {
maxteam.put(schoolnum, 1);
} else {
maxteam.put(schoolnum, maxteam.get(schoolnum) + 1);
}
if (maxteam.get(schoolnum) == 7) {
System.out.println("每个学校最多六支队伍,已经超越限制无法添加该队伍");
} else {
com.teamscore.add(sch);
}
System.out.println("当前已经创建" + com.teamscore.size() + "只队伍(至少10只队伍),是否继续该竞赛队伍的成绩?");
System.out.println("1.继续\t0.结束");
int cho = sc.nextInt();
if (cho == 0) {
if (com.teamscore.size() < 10) {
System.out.println("比赛队伍少于10支,创建失败");
break;
} else {
coms.add(com);
System.out.println("竞赛成绩创建成功");
break;
}
}
}
} catch (InputMismatchException e) {
System.out.println("输入错误已返回菜单");
return;
}
}
}
----------------------------------------------------------------
package E1;
public class School {
public String name;
public int num;
public int score;
public School(String name, int num,int score) {
this.name = name;
this.num = num;
this.score=score;
}
}