项目要求:
①学生成绩查询系统中保存学生姓名、课程名称以及学生的课程成绩。
②查询系统从控制台输入统计命令以及参数,实现成绩的查询。主要命令如下:
avg Java:统计课程Java的平均分。
avg song:统计学生song的平均分。
get song Java:获取学生song的Java课程的成绩。
sort Java:列出Java 课程的成绩排序结果。
exit:退出程序。
数据储存:
如果查询的学生或课程不存在时,给出相应的提示。
学生的名字和课程的名称分别存在一维数组中:
static String[ ] students={"zhang","wang","li","zhao","liu"、"song"};
static String[ ]courses ={"C","Java","mySQL","Linux","HTML"};
学生成绩存在二维数组中:
static int[ ][ ] score =new int[6][5];
初始化数组,输出数组即显示成绩,执行查询系统的控制台命令调度。
import java.util.*;
public class StudentScore
{
static final int STUDENT_NUM=6;
static final int COURSE_NUM=5;
static String[] students={"zhang","wang","li","zhao","liu","song"};
static String[] courses={"C","Java","mySQL","Linux","HTML"};
public static void main(String[] args){
double[][] score={{26,69,46,25,5},{27,10,24,66,58},{44,58,0,82,75},{6,68,92,9,84},{75,1,51,41,74},{23,38,65,1,55}}; //定义二维数组记录学生成绩;
double ave_c,ave_java,ave_mysql,ave_linux,ave_html; //定义课程平均成绩
ave_c=(score[0][0]+score[1][0]+score[2][0]+score[3][0]+score[4][0]+score[5][0])/6;
ave_java=(score[0][1]+score[1][1]+score[2][1]+score[3][1]+score[4][1]+score[5][1])/6;
ave_mysql=(score[0][2]+score[1][2]+score[2][2]+score[3][2]+score[4][2]+score[5][2])/6;
ave_linux=(score[0][3]+score[1][3]+score[2][3]+score[3][3]+score[4][3]+score[5][3])/6;
ave_html=(score[0][4]+score[1][4]+score[2][4]+score[3][4]+score[4][4]+score[5][4])/6;
double ave_zhang,ave_wang,ave_li,ave_zhao,ave_liu,ave_song; //定义学生平均成绩
ave_zhang=(score[0][0]+score[0][1]+score[0][2]+score[0][3]+score[0][4])/5;
ave_wang=(score[1][0]+score[1][1]+score[1][2]+score[1][3]+score[1][4])/5;
ave_li=(score[2][0]+score[2][1]+score[2][2]+score[2][3]+score[2][4])/5;
ave_zhao=(score[3][0]+score[3][1]+score[3][2]+score[3][3]+score[3][4])/5;
ave_liu=(score[4][0]+score[4][1]+score[4][2]+score[4][3]+score[4][4])/5;
ave_song=(score[5][0]+score[5][1]+score[5][2]+score[5][3]+score[5][4])/5;
System.out.println(" C Java mySQL Linux HTML");
System.out.println("zhang 26 69 46 25 5");
System.out.println("wang 27 10 24 66 58");
System.out.println("li 44 58 0 82 75");
System.out.println("zhao 6 68 92 9 84");
System.out.println("liu 75 1 51 41 74");
System.out.println("song 23 38 65 1 55");
Scanner scanner=new Scanner(System.in);
boolean a = true;
while(a){
System.out.printf("请输入命令:");
String command=scanner.nextLine();//输入指令
if(command.startsWith("avg")){ //按照指令输出课程平均分
if(command.equalsIgnoreCase("avg c")){
System.out.format("C的平均分是:",ave_c);
System.out.println();
}else if(command.equalsIgnoreCase("avg java")){
System.out.format("Java的平均分是:%.2f",ave_java);
System.out.println();
}else if(command.equalsIgnoreCase("avg mysql")){
System.out.format("mySQL的平均分是:%.2f",ave_mysql);
System.out.println();
}else if(command.equalsIgnoreCase("avg linux")){
System.out.format("Linux的平均分是:%.2f",ave_linux);
System.out.println();
}else if(command.equalsIgnoreCase("avg html")){
System.out.format("HTML的平均分是:%.2f",ave_html);
System.out.println();
}else if(command.equalsIgnoreCase("avg zhang")){
System.out.format("zhang的平均分是:%.2f",ave_zhang);//输出学生平均分
System.out.println();
}else if(command.equalsIgnoreCase("avg wang")){
System.out.format("wang的平均分是:%.2f",ave_wang);
System.out.println();
}else if(command.equalsIgnoreCase("avg li")){
System.out.format("li的平均分是:%.2f",ave_li);
System.out.println();
}else if(command.equalsIgnoreCase("avg zhao")){
System.out.format("zhao的平均分是:%.2f",ave_zhao);
System.out.println();
}else if(command.equalsIgnoreCase("avg liu")){
System.out.format("liu的平均分是:%.2f",ave_liu);
System.out.println();
}else if(command.equalsIgnoreCase("avg song")){
System.out.format("song的平均分是:%.2f",ave_song);
System.out.println();
}else
System.out.println("你输入的既不是课程名,也不是学生名");
}
else if(command.startsWith("get")){
String[] words=command.split(" ");//将字符串按空格分割开
String student=words[1]; //分割开的第二个字符串为学生名·
String course=words[2]; //课程名
if(student.equalsIgnoreCase("zhang")){ //将学生zhang的成绩分类
if(course.equalsIgnoreCase("c")){
System.out.format("zhang的c成绩是:%.2f",score[0][0]);
System.out.println();
}else if(course.equalsIgnoreCase("java")){
System.out.format("zhang的java成绩是:%.2f",score[0][1]);
System.out.println();
}else if(course.equalsIgnoreCase("mysql")){
System.out.format("zhang的mysql成绩是:%.2f",score[0][2]);
System.out.println();
}else if(course.equalsIgnoreCase("linux")){
System.out.format("zhang的linux成绩是:%.2f",score[0][3]);
System.out.println();
}else if(course.equalsIgnoreCase("hyml")){
System.out.format("zhang的html成绩是:%.2f",score[0][4]);
System.out.println();
}else{
System.out.println("zhang没有"+course+"这门课程");
}
}
else if(student.equalsIgnoreCase("wang")){//将学生wang成绩分类
if(course.equalsIgnoreCase("c")){
System.out.format("wang的c成绩是:%.2f",score[1][0]);
System.out.println();
}else if(course.equalsIgnoreCase("java")){
System.out.format("wang的java成绩是:%.2f",score[1][1]);
System.out.println();
}else if(course.equalsIgnoreCase("mysql")){
System.out.format("wang的mysql成绩是:%.2f",score[1][2]);
System.out.println();
}else if(course.equalsIgnoreCase("linux")){
System.out.format("wang的linux成绩是:%.2f",score[1][3]);
System.out.println();
}else if(course.equalsIgnoreCase("hyml")){
System.out.format("wang的html成绩是:%.2f",score[1][4]);
System.out.println();
}else{
System.out.println("wang没有"+course+"这门课程");
}
}
else if(student.equalsIgnoreCase("li")){ //将学生li成绩分类
if(course.equalsIgnoreCase("c")){
System.out.format("li的c成绩是:%.2f",score[2][0]);
System.out.println();
}else if(course.equalsIgnoreCase("java")){
System.out.format("li的java成绩是:%.2f",score[2][1]);
System.out.println();
}else if(course.equalsIgnoreCase("mysql")){
System.out.format("li的mysql成绩是:%.2f",score[2][2]);
System.out.println();
}else if(course.equalsIgnoreCase("linux")){
System.out.format("li的linux成绩是:%.2f",score[2][3]);
System.out.println();
}else if(course.equalsIgnoreCase("hyml")){
System.out.format("li的html成绩是:%.2f",score[2][4]);
System.out.println();
}else{
System.out.println("li没有"+course+"这门课程");
}
}
else if(student.equalsIgnoreCase("zhao")){ //将学生zhao成绩分类
if(course.equalsIgnoreCase("c")){
System.out.format("zhao的c成绩是:%.2f",score[3][0]);
System.out.println();
}else if(course.equalsIgnoreCase("java")){
System.out.format("zhao的java成绩是:%.2f",score[3][1]);
System.out.println();
}else if(course.equalsIgnoreCase("mysql")){
System.out.format("zhao的mysql成绩是:%.2f",score[3][2]);
System.out.println();
}else if(course.equalsIgnoreCase("linux")){
System.out.format("zhao的linux成绩是:%.2f",score[3][3]);
System.out.println();
}else if(course.equalsIgnoreCase("hyml")){
System.out.format("zhao的html成绩是:%.2f",score[3][4]);
System.out.println();
}else{
System.out.println("zhao没有"+course+"这门课程");
}
}
else if(student.equalsIgnoreCase("liu")){ //将学生liu成绩分类
if(course.equalsIgnoreCase("c")){
System.out.format("zhao的c成绩是:%.2f",score[4][0]);
System.out.println();
}else if(course.equalsIgnoreCase("java")){
System.out.format("zhao的java成绩是:%.2f",score[4][1]);
System.out.println();
}else if(course.equalsIgnoreCase("mysql")){
System.out.format("zhao的mysql成绩是:%.2f",score[4][2]);
System.out.println();
}else if(course.equalsIgnoreCase("linux")){
System.out.format("zhao的linux成绩是:%.2f",score[4][3]);
System.out.println();
}else if(course.equalsIgnoreCase("hyml")){
System.out.format("zhao的html成绩是:%.2f",score[4][4]);
System.out.println();
}else{
System.out.println("zhao没有"+course+"这门课程");
}
}
else if(student.equalsIgnoreCase("song")){ //将学生song成绩分类
if(course.equalsIgnoreCase("c")){
System.out.format("song的c成绩是:%.2f",score[5][0]);
System.out.println();
}else if(course.equalsIgnoreCase("java")){
System.out.format("song的java成绩是:%.2f",score[5][1]);
System.out.println();
}else if(course.equalsIgnoreCase("mysql")){
System.out.format("song的mysql成绩是:%.2f",score[5][2]);
System.out.println();
}else if(course.equalsIgnoreCase("linux")){
System.out.format("song的linux成绩是:%.2f",score[5][3]);
System.out.println();
}else if(course.equalsIgnoreCase("hyml")){
System.out.format("song的html成绩是:%.2f",score[5][4]);
System.out.println();
}
else{
System.out.println("song没有"+course+"这门课程");
}
}
else{
System.out.println("没有"+student+"这个人");
}
}
else if(command.startsWith("sort")){
double m;
String t;
String[] words=command.split(" ");
String course =words[1];
int[] b=new int[5];
int count=0;
if(course.equalsIgnoreCase("c")){
System.out.println("名次 姓名 C");
for (int i = 0; i < 6; i++){
for (int j = 0; j < 6 - i - 1; j++){
if (score[j][0] > score[j + 1][0]) {
m = score[j][0];
score[j][0] = score[j + 1][0];
score[j + 1][0] = m; // 分数交换
t = students[j];
students[j] = students[j + 1];
students[j + 1] = t; // 名字交换;//这是C的类的成绩排序
}
}
}
for(int i=0;i<6;i++){
System.out.println((i+1)+"\t"+students[i]+"\t"+score[i][0]);
}
}
else if(course.equalsIgnoreCase("java")){
System.out.println("名次 姓名 Java");
for (int i = 0; i < 6; i++){
for (int j = 0; j < 6 - i - 1; j++){
if (score[j][1] > score[j + 1][1]) {
m = score[j][1];
score[j][1] = score[j + 1][1];
score[j + 1][1] = m; // 分数交换
t = students[j];
students[j] = students[j + 1];
students[j + 1] = t; // 名字交换;//这是C的类的成绩排序
}
}
}
for(int i=0;i<6;i++){
System.out.println((i+1)+"\t"+students[i]+"\t"+score[i][1]);
}
}
else if(course.equalsIgnoreCase("mysql")){
System.out.format("名次 姓名 mySQL");
for (int i = 0; i < 6; i++){
for (int j = 0; j < 6 - i - 1; j++){
if (score[j][2] > score[j + 1][2]) {
m = score[j][2];
score[j][2] = score[j + 1][2];
score[j + 1][2] = m;
t = students[j];
students[j] = students[j + 1];
students[j + 1] = t;
}
}
}
for(int i=0;i<6;i++){
System.out.println((i+1)+"\t"+students[i]+"\t"+score[i][2]);
}
}
else if(course.equalsIgnoreCase("linux")){
System.out.println("名次 姓名 Linux");
for (int i = 0; i < 6; i++){
for (int j = 0; j < 6 - i - 1; j++){
if (score[j][3] > score[j + 1][3]) {
m = score[j][3];
score[j][3] = score[j + 1][3];
score[j + 1][3] = m; // 分数交换
t = students[j];
students[j] = students[j + 1];
students[j + 1] = t; // 名字交换;//这是C的类的成绩排序
}
}
}
for(int i=0;i<6;i++){
System.out.println((i+1)+"\t"+students[i]+"\t"+score[i][3]);
}
}
else if(course.equalsIgnoreCase("html")){
System.out.println("名次 姓名 HTML");
for (int i = 0; i < 6; i++){
for (int j = 0; j < 6 - i - 1; j++){
if (score[j][4] > score[j + 1][4]) {
m = score[j][4];
score[j][4] = score[j + 1][4];
score[j + 1][4] = m; // 分数交换
t = students[j];
students[j] = students[j + 1];
students[j + 1] = t; // 名字交换;//这是C的类的成绩排序
}
}
}
for(int i=0;i<6;i++){
System.out.println((i+1)+"\t"+students[i]+"\t"+score[i][4]);
}
}
else{
System.out.println("没有这门课程");
}
}
else{
System.out.println("退出查询系统!byebye!");
break;
}
}
}
}
以下是运行效果的展示
代码经过不断修改终于达到预期输出,其中仍有不足,亲多多指教。