B.学生成绩查询系统(命令行版本),要求实现以下功能:
*1.显示所以学生所有课程的成绩表。
*2.统计某个学生的课程成绩平均分。
*3.统计某门课程的成绩平均分。
*4.查询某个学生某门课程的成绩。
*5.查询某门课程的学生成绩,并能实现从高到低和从低到高的排序。
*提示:学生和课程可以用一个String类型的数组表示,分数可以用int类型的数组表示,分数值可以随机生成。

这个题目除了第五个排序没写好,排序都可以,就是分数对应的姓名也要打印出来就有点繁琐了。后来也懒得写了,主要是没有思路了。都在写扎金花的那个。

先放上我的,说好了,我的是未完全版。 :arrow:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import java.util.*;
public class HomeWork4_4
{
	public static void main(String[] args)
	{
		Scanner sc = new Scanner(System.in);
		Random rd = new Random ();
		int n = 10, m = 3;
		String[][] arr = new String[n][m+1];//*0为姓名,*1、*2....为课程名
		int[][]  score= new int[n][m+1]; //*0为个人总成绩,*1、*2...为每门课的成绩
		int[] s = new int[m+1];//单门课的总分数
		String[] name={"TOM","Lucy","Lily","Bruce","Frank","Bloon","Mary","LiLei","HanMei","Laycher"};
		for(int i=0;i<n ;i++)
		{
			arr[i][0]=name[i];
			arr[i][1]="Chinese";
		    arr[i][2]="Math";
		    arr[i][3]="English";
			for (int j=1;j<m+1 ;j++ )
			{
			   score[i][j]=rd.nextInt(101);
			   score[i][0]+=score[i][j];
			}
		}
		for(int j=1;j<m+1;j++)
		{
			for (int i=0;i<n;i++ )
			{
				s[j]+=score[i][j];
			}
		}
		while(true)
		{
		System.out.println("Please input your choice:");
		System.out.println("1.Display all students's each course's score.");
		System.out.println("2.Display a student's average score of all the course.");
		System.out.println("3.Display a course's average socre.");
		System.out.println("4.Search one student's score of one course.");
		System.out.println("5.Search the socre of one course.");
		System.out.println("6.Exit.");
		int ch = sc.nextInt();
		if(ch==1)
		{
			System.out.println("\t\t"+arr[0][1]+"\t\t"+arr[0][2]+"\t\t"+arr[0][3]);
            for (int i=0;i<n ;i++ )
            {
				System.out.print(arr[i][0]+"\t");
				for (int j=1;j< m+1;j++ )
				{
					System.out.print("\t"+score[i][j]+"\t");
				}
			    System.out.println();
            }
			System.out.println();
		}
		else if (ch==2)
		{
			System.out.println("Please input the sutdent's name:(For Example:Laycher)");
            String w = sc.next();
			for (int i=0; ;i++ )
			{
				if (i>=n)
				{
					System.out.println("No this person! Please check.\n");
					break;
				}
				if(w.equals(arr[i][0]))
				{
					System.out.println(w+"'s average score is "+score[i][0]/m+"\n");
					break;
				}
			}
		}
		else if (ch==3)
		{
			System.out.println("Please input the course's name:(For Example:Chinese)");
            String w = sc.next();
			for(int j=1;;j++)
			{
				if (j>m)
				{
					System.out.println("No this course! Please check.\n");
					break;
				}
				if(w.equals(arr[0][j]))
				{
					System.out.println("The course"+w+"'s average score is "+s[j]/n+"\n");
					break;
				}
			}
		}
		else if (ch==4)
		{
			System.out.println("Please input the student's name and the course's name:\n(For Example:Laycher Math)");
			String sn = sc.next();
			String cn = sc.next();
	    loop:for(int i=0;;i++)
			{
				if(i>=n)
				{
					System.out.println("No this person! Please check.\n");
					break;
				}
				if(sn.equals(arr[i][0]))
				{
					for(int j=1;;j++)
					{
					  if (j>m)
				      {
					     System.out.println("No this course! Please check.\n");
					     break loop;
				      }
					  if(cn.equals(arr[i][j]))
				      {
					     System.out.println(sn+"'s "+cn+" score is "+score[i][j]+".\n");
					     break loop;
				      }
					}
				}
			}
		}
		else if (ch==5)
		{
			System.out.println("Please input the course's name:(For Example:Chinese)");
            String w = sc.next();
		    for(int j=1;;j++)
			{
				if (j>m)
				{
					System.out.println("No this course! Please check.\n");
					break;
				}
				if(w.equals(arr[0][j]))
				{
					int[] num = new int[n];
					int k=0,i=0;
					for (i=0;i</n><n ;i++ )
						System.out.print(arr[i][0]+"\t");
					for (i=0;i<n ;i++ )
					{
						System.out.print(score[i][j]+"\t");
						num[i] = score[i][j];
					}
					System.out.println();
					Arrays.sort(num);
					System.out.println("Max to Min:");
					i=0;
						//System.out.print(num[i]);
					while(true)
					{
						 if(num[i]==score[k][j])
						 {
						      System.out.print(arr[k][0]+"\t");
							  i++;
							  k=0;
							  if(i>=n) break;
						  }
						  else
						  {
							 k++;
						  }
					}
					for(i=0;i</n><n ;i++)
					{
						System.out.print(num[i]+"\t");
					}
					break;
				}
			}
		}
		else if (ch==6)
		{
			return;
		}
		else
		{
			System.out.println("Input Error! Please input again!");
		}
		}
	}
}

下面这个是老师写的,这个可是正版哦。 :wink:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package day05.student_manager;
import java.util.Scanner;
public class StudentManager {
	static int[][] scores=new int[6][5];
	static String[] students={"zhangsan","lisi","wangwu","zhaoliu","qianqi","liuba"};
	static String[] courses={"corejava","jdbc","servlet","jsp","ejb"};
 
	public static void main(String[] args) {
		for(int i=0;i<scores .length;i++){
			for(int j=0;j<scores[i].length;j++){
				scores[i][j]=(int)(Math.random()*100);
			}
		}
 
		Scanner s=new Scanner(System.in);
		String comm;
		while(true){
			System.out.println("请输入命令:");
			comm=s.next();
			if(comm.equals("AVG")){
				String para=s.next();
				avg(para);
 
			}else if(comm.equals("SORT")){
				String course=s.next();
				sort(course);
 
			}else if(comm.equals("GET")){
				String student=s.next();
				String course=s.next();
				get(student,course);
			}else if(comm.equals("EXIT")){
				break;
			}else{
				System.out.println("命令格式不正确,请重新输入!");
			}
		}
	}//main() end!
 
	public static void avg(String para){
		int sIndex=-1;
		int cIndex=-1;
		for(int i=0;i<students.length;i++){
			if(students[i].equals(para)){
				sIndex=i;
			}
		}
		if(sIndex==-1){
			for(int i=0;i<courses.length;i++){
				if(courses[i].equals(para)){
					cIndex=i;
				}
			}
		}
 
		if(sIndex==-1 && cIndex==-1){
			System.out.println("找不到学生或课程!");
			return;
		}
 
		double avg=0.0;
		if(sIndex!=-1){
			for(int i=0;i<scores[sIndex].length;i++){
				avg+=scores[sIndex][i];
			}
			avg/=scores[sIndex].length;
			System.out.println("学生:"+para+"的平均分是:"+avg);
		}else{
			for(int i=0;i<scores.length;i++){
				avg+=scores[i][cIndex];
			}
			avg/=scores.length;
			System.out.println("课程:"+para+"的平均分是:"+avg);
		}
 
	}
	public static void sort(String course){
		int[] courseScore=new int[scores.length];
		if(course.equals("sum")){//如果求总分的排名
//			//求出每个学生的总分,将成绩存放在courseScore数组中
			for(int i=0;i<scores.length;i++){
				int studentSum=0;
				for(int j=0;j<scores[i].length;j++){
					studentSum+=scores[i][j];
				}
				courseScore[i]=studentSum;
			}
		}else{//如果不是求总分排名
			int cIndex=-1;
			for(int i=0;i<courses.length;i++){//找到这门课程的下标
				if(courses[i].equals(course)){
					cIndex=i;
				}
			}
			if(cIndex!=-1){//如果是一门有效的课程
				//把scores数组中这一列的值放到courseScore数组中!
				for(int i=0;i<scores.length;i++){
					courseScore[i]=scores[i][cIndex];
				}
			}else{//如果不是一门有效的课程
				System.out.println("课程名不正确,请重新输入!");
				return;
			}
		}
 
		String[] studentCopy=new String[students.length];
		System.arraycopy(students, 0, studentCopy, 0, students.length);
 
		for(int i=0;i<courseScore.length-1;i++){
			for(int j=i+1;j<courseScore.length;j++){
				if(courseScore[i]<courseScore[j]){
					int temp=courseScore[i];
					courseScore[i]=courseScore[j];
					courseScore[j]=temp;
 
					String stemp=studentCopy[i];
					studentCopy[i]=studentCopy[j];
					studentCopy[j]=stemp;
				}
			}
		}
 
		int order=1;
		System.out.println("名次\t学生\t成绩");
		for(int i=0;i<courseScore.length;i++){
			if(i!=0 && courseScore[i]==courseScore[i-1]){
				order--;
			}else{
				order=i+1;
			}
			System.out.print(order+"\t");
			System.out.print(studentCopy[i]+"\t");
			System.out.println(courseScore[i]);
			order++;
		}
	}
 
	public static void get(String student,String course){
		int sIndex=-1;
		int cIndex=-1;
 
		for(int i=0;i<students.length;i++){
			if(students[i].equals(student)){
				sIndex=i;
			}
		}
 
		if(sIndex==-1){
			System.out.println("没有这个学生:"+student);
			return;
		}
 
		if(course.equals("sum")){//如果求总分
			int studentSum=0;
			for(int j=0;j<scores[sIndex].length;j++){
				studentSum+=scores[sIndex][j];
			}
			System.out.println(student+"学生的总分为:"+studentSum);
			return;
		}
 
		for(int i=0;i<courses.length;i++){
			if(courses[i].equals(course)){
				cIndex=i;
			}
		}
 
		if(cIndex==-1){
			System.out.println("没有这门课程:"+course);
			return;
		}
 
		System.out.println(student+"学生的"+course+"课程的成绩为:"+scores[sIndex][cIndex]);
 
	}
 
}

>> 若为原创,转载请注明: 转载自Laycher's Blog

>> 本文链接地址: 学生成绩查询系统(命令行版本)

>> 订阅本站: http://feed.feedsky.com/laycher



无觅相关文章插件,快速提升流量