Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class FinalExamMark {
- public static void main(String[] args) {
- Scanner input = new Scanner(System.in);
- while (true) {
- //get average the user wants
- System.out.print("Enter the desired average: ");
- int d = input.nextInt();
- //if input average is negative, exit the program
- if (d < 0) {
- System.out.println("Goodbye.");
- System.exit(1);
- }
- //get number of tests including the final
- System.out.print("Enter the number of tests (including the final): ");
- int k = input.nextInt();
- //use nextLine to avoid skipping weights entry
- input.nextLine();
- //get the test weights
- System.out.print("Enter the test weights: ");
- String weightsString = input.nextLine();
- int[] weights = stringToArray(weightsString, k);
- //get the test grades before the exam (k - 1)
- System.out.print("Enter the previous test grades (out of 100): ");
- String gradesString = input.nextLine();
- int[] gradesInt = stringToArray(gradesString, (k - 1));
- double[] grades = new double[gradesInt.length];
- //convert grades to a double array
- for (int i = 0; i < (k - 1); i++) {
- grades[i] = gradesInt[i];
- }
- //debug lines
- //System.out.println(Arrays.toString(weights));
- //System.out.println(Arrays.toString(grades));
- //divide the marks for each test by 100
- for (int i = 0; i < grades.length; i++) {
- grades[i] = grades[i] / 100;
- }
- //add the marks for each test up
- double totalMarks = 0;
- for (int i = 0; i < grades.length; i++) {
- totalMarks += grades[i];
- }
- //divide totalMarks by the number of tests
- totalMarks = totalMarks / (k - 1);
- //multiply that value by 100 to get the mark thus far
- totalMarks = totalMarks * 100;
- double finalWeight = weights[weights.length - 1] / 100;
- double finalMark = (d - ((1 - finalWeight) * totalMarks)) / finalWeight;
- System.out.println(finalMark);
- }
- }
- public static int[] stringToArray(String string, int arrayLength) {
- //split all the numbers up into an array
- String[] stringArray = string.split("\\s+");
- int[] array = new int[arrayLength];
- //convert the string array to an int array
- for (int i = 0; i < arrayLength; i++) {
- array[i] = Integer.parseInt(stringArray[i]);
- }
- return array;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement