Enter a name, enter grade, and enter 7 test scores.
A program that calculates average test scores.
I'd like to get the average, except for the highest and lowest scores. How should I do it?
A program that calculates average test scores.
I'd like to get the average, except for the highest and lowest scores. How should I do it?
Code:
import java.util.Scanner;
public class StudentDemo {
Scanner s = new Scanner(System.in);
Student[] stList = new Student[10];
int cnt = 0;
public static void main(String[] args){
StudentDemo demo = new StudentDemo();
demo.doit();
}
void doit() {
Student st = null;
while (true) {
st = new Student();
st.read(this.s);
if(st.name.equals("end"))
break;
stList[cnt++] = st;
}
for (int i = 0; i < cnt; i++){
stList[i].print();
}
}
}
class Student {
String name;
int year;
int[] score = new int[7];
float avg;
void read(Scanner s){
name = s.next();
if(name.equals("end"))
return;
year = s.nextInt();
for(int i = 0; i < 7; i++)
score[i] = s.nextInt();
}
void print() {
int sum = 0;
System.out.printf("%-5s (%d) ",name, year);
for(int i = 0; i < 7; i++){
System.out.printf("%3d", score[i]);
sum += score[i];
}
System.out.printf(" => %5.1f%n", sum/3.0);
}
}