Skip to content

Instantly share code, notes, and snippets.

@daniel-stoneuk
Last active January 19, 2017 12:44
Show Gist options
  • Save daniel-stoneuk/77f42ddce81408388a5cf6a651a954e9 to your computer and use it in GitHub Desktop.
Save daniel-stoneuk/77f42ddce81408388a5cf6a651a954e9 to your computer and use it in GitHub Desktop.
Pre Release Task - Test Scores
package main.com.danielstone.prereleasetestscores;
import java.util.Arrays;
import java.util.Collections;
import java.util.InputMismatchException;
import java.util.Scanner;
public class Task {
public static void main(String[] args) {
// TODO Auto-generated method stub
Task task = new Task();
task.tasks();
}
public void tasks() {
String[] names = new String[5];
int[] test1scores = new int[30]; Arrays.fill(test1scores, -1);
int[] test2scores = new int[30]; Arrays.fill(test2scores, -1);
int[] test3scores = new int[30]; Arrays.fill(test3scores, -1);
int[] totalscores = new int[30]; Arrays.fill(totalscores, -1);
for (int i = 0; i < names.length; i++) {
Scanner in = new Scanner(System.in);
System.out.println("Name " + (i + 1) + "/" + names.length + ": ");
names[i] = in.next();
do {
System.out.println("Test one score: ");
try {
test1scores[i] = in.nextInt();
} catch (InputMismatchException e) {
in.next();
}
} while (!(test1scores[i] >= 0 && test1scores[i]<= 20));
do {
System.out.println("Test two score: ");
try {
test2scores[i] = in.nextInt();
} catch (InputMismatchException e) {
in.next();
}
} while (!(test2scores[i] >= 0 && test2scores[i]<= 25));
do {
System.out.println("Test three score: ");
try {
test3scores[i] = in.nextInt();
} catch (InputMismatchException e) {
in.next();
}
} while (!(test3scores[i] >= 0 && test3scores[i]<= 35));
}
// Start of task 2
int classtotalavg = 0;
for (int i = 0; i < names.length; i++) {
totalscores[i] = 0;
totalscores[i] += test1scores[i];
totalscores[i] += test2scores[i];
totalscores[i] += test3scores[i];
classtotalavg += totalscores[i];
System.out.println("Student " + names[i]+ " scored " + totalscores[i] );
}
classtotalavg = classtotalavg / names.length;
System.out.println("Average total score was " + classtotalavg);
// Start of task 3
int highestscore = 0;
String highestname = "";
for (int i = 0; i < names.length; i++) {
if (totalscores[i]> highestscore ) {
highestscore = totalscores[i];
highestname = names[i];
}
}
System.out.println("Highest total score was " + highestname + " with " + highestscore);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment