// Leser inn studentnavn og score på en innlevert oppgave // Skriver ut liste og statistikk public class Score2 { public static void main(String[] args) { final int MAX_ANTALL_STUD = 1000; final int ANTALL_OPPG = 4; int antall_stud = 0; // antall innleste studenter String[] navn = new String[MAX_ANTALL_STUD]; int[][] score = new int[MAX_ANTALL_STUD][ANTALL_OPPG]; String nytt_navn; int ny_score; int stud_indeks, oppg_indeks; System.out.println("Dette programmet leser inn studentnavn og oppgavescore"); System.out.println("Avslutt innlesing med en blank linje."); System.out.println(); // Les inn navn og score do { System.out.print("Navn på student " + (antall_stud+1) + ": "); nytt_navn = Terminal.lesString(); if (nytt_navn.equals("")) break; navn[antall_stud] = new String(nytt_navn); // Les inn score for alle oppgaver for (oppg_indeks = 0; oppg_indeks < ANTALL_OPPG; oppg_indeks++) { System.out.print("Score for " + nytt_navn + " på oppgave " + (oppg_indeks+1) + ": "); ny_score = Terminal.lesInt(); score[antall_stud][oppg_indeks] = ny_score; } antall_stud++; } while (antall_stud < MAX_ANTALL_STUD); // Skriv ut liste med navn og score System.out.println(); System.out.println("Resultat:"); System.out.println(); for (stud_indeks = 0; stud_indeks < antall_stud; stud_indeks++) { System.out.print(navn[stud_indeks]); for (oppg_indeks = 0; oppg_indeks < ANTALL_OPPG; oppg_indeks++) System.out.print("\t" + score[stud_indeks][oppg_indeks]); System.out.println(); // avslutt linjen } // Statistikk: beste, dårligste, gjennomsnitt for hver oppgave int[] sum = new int[ANTALL_OPPG]; int[] maxscore = new int[ANTALL_OPPG]; int[] minscore = new int[ANTALL_OPPG]; for (oppg_indeks = 0; oppg_indeks < ANTALL_OPPG; oppg_indeks++) { sum[oppg_indeks] = score[0][oppg_indeks]; maxscore[oppg_indeks] = score[0][oppg_indeks]; minscore[oppg_indeks] = score[0][oppg_indeks]; // iterer fra 2. element for (stud_indeks = 1; stud_indeks < antall_stud; stud_indeks++) { sum[oppg_indeks] += score[stud_indeks][oppg_indeks]; if (score[stud_indeks][oppg_indeks] > maxscore[oppg_indeks]) maxscore[oppg_indeks] = score[stud_indeks][oppg_indeks]; if (score[stud_indeks][oppg_indeks] < minscore[oppg_indeks]) minscore[oppg_indeks] = score[stud_indeks][oppg_indeks]; } } System.out.println(); System.out.print("Beste score:"); for (oppg_indeks = 0; oppg_indeks < ANTALL_OPPG; oppg_indeks++) System.out.print("\t" + maxscore[oppg_indeks]); System.out.println(); System.out.print("Dårligste:"); for (oppg_indeks = 0; oppg_indeks < ANTALL_OPPG; oppg_indeks++) System.out.print("\t" + minscore[oppg_indeks]); System.out.println(); System.out.print("Gjennomsnitt:"); for (oppg_indeks = 0; oppg_indeks < ANTALL_OPPG; oppg_indeks++) System.out.print("\t" + (double)sum[oppg_indeks]/antall_stud); System.out.println(); } }