JAVA 程序设计及应用开发 P55-56 实验题2 1)Avg+all :计算所有课程,所有学生的平均分。并以如下表格的形式打印输出。
2)2)max+课程名:查询该课程的最高分。
Max+学生名:查询该学生的最高分。
代码如下:
import java.util.*;
DecimalFormat;
public class searchsystem {
public static void main(String[]args) {
Scanner scn=new Scanner(System.in);
String[] students=new String[] {"zhang","wang","li","zhao","liu","song"};
String[] courses=new String[] {"C","Java","mySQL","Linux","HTML","平均分:"};printf输出格式java
int[][]score=new int[][] {{9,98,94,61,14},{87,42,62,24,55},{78,95,3,54,67},{14,59,26 ,74,30},{26,86,47,58,81},{92,31,72,61,19}};
while(true) {
System.out.print("请输入命令:");
String ();
if(command.equalsIgnoreCase("avg")) {
String ();
avg(students,courses,score,parameter);}
if(command.equalsIgnoreCase("max")) {
String ();
getMax(students,courses,score,parameter);
}
if(command.equalsIgnoreCase("exit")) {System.out.println("退出查询系统!byebye!");it(0);}
}
}
private static void getMax(String[] students, String[] courses, int[][] score,String parameter) {//求最高分
int c1=-1,c2=0;
for(int i=0;i<students.length;i++) {//查询学生最高分if(students[i].equalsIgnoreCase(parameter)) {
int max = 0;
c1=1;
for (int k = 0; k< score.length; k++) {
for (int j = 0; j <5; j++) {
if (max < score[i][j]) {
max = score[i][j];//算出最大值
}
}
} System.out.println(max);
}
}
for(int i=0;i<courses.length;i++) {//查询课程最高分if(courses[i].equalsIgnoreCase(parameter)) {
int max = 0;
c2=1;
for (int k = 0; k< score.length; k++) {
for (int j = 0; j < score[j].length - 1; j++) {
if (max < score[k][i]) {
max = score[k][i];//算出最大值
}}}
System.out.println(max);}}
if(c1==-1&c2==0) {System.out.println("你输入的既不是课程名,也不是学生名");}
}
private static void avg(String[] students, String[] courses, int[][] score, String parameter) {//计算所有学生,课程成绩平均分
int i,k=0;
int sum1=0,sum2=0;
DecimalFormat df = new DecimalFormat( "0.00 ");
System.out.print("\t");//7
for( i=0;i<courses.length;i++) {
System.out.print(courses[i]+"\t");}
System.out.println();//换行
for(i=0;i<students.length;i++) {
System.out.print(students[i]+"\t");
for(k=0;k<5;k++) {
System.out.print(score[i][k]+"\t");
sum1=sum1+score[i][k];
}double avg1=sum1/5.0;
System.out.printf(df.format(avg1));
sum1=0;
System.out.println();
}System.out.print("平均分:\t");
for(i=0;i<5;i++) {for(int j=0;j<6;j++) {sum2=sum2+score[j][i];}double avg2=sum2/6.0;
System.out.printf(df.format(avg2)+"\t");
sum2=0;
}}}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。