简体   繁体   中英

How do I calculate the average of specific indexes within multiple arrays input by a user in Java?

I am not sure if I am doing this correctly or not, but here is what I did. Ultimately I want to take user input of test results to get their average from each test and display the grade with average score and name. This is for a class assignment.

Data:

test1 = {67, 87, 71, 79, 100} \\I want the user to be able to input this data as test results may vary
test2 = {70, 90, 74, 82, 100}
test3 = {73, 93, 77, 85, 100}

Code:

import java.lang.reflect.Array;
import java.util.Scanner;

public class GradesArray {

    static Scanner reader = new Scanner(System.in);

    public static void main(String[] args) {

        String[] nameArray = {"Jon", "Rajesh", "Elizabeth", "Julio", "Chang"};
        System.out.println("Enter the five scores from Test 1, type 0 when done");
        String[] gradeArray = new String[5];
            double[] test1Array = {reader.nextInt()};
            int keyboard = reader.nextInt();
            while (keyboard != 0){
                keyboard = reader.nextInt();
            }
        System.out.println("Enter the five scores from Test 2, type 0 when done");
            double[] test2Array = {reader.nextInt()};
            int keyboard2 = reader.nextInt();
            while (keyboard2 != 0){
                keyboard2 = reader.nextInt();
            }
        System.out.println("Enter the five scores from Test 3, type 0 when done");
            double[] test3Array = {reader.nextInt()};
            int keyboard3 = reader.nextInt();
            while (keyboard3 != 0){
            keyboard3 = reader.nextInt();
            }
            double[] averageArray = new double[5];
            for(int i=0; i<5; i++){ 
                averageArray[i] = (test1Array[i] + test2Array[i] + test3Array[i])/3;
                    if (averageArray[i] >=90){
                        gradeArray[i] = "A";
                    } else if (averageArray[i] >= 80){
                        gradeArray[i] = "B";
                    } else if (averageArray[i] >= 70){
                        gradeArray[i] = "C";
                    } else if (averageArray[i] >= 60){
                        gradeArray[i] = "D";
                    } else{
                        gradeArray[i] = "F";
                    }
            }
            System.out.println("Name is: " + nameArray[i] + " Grade is: " + gradeArray[i] + " Average is: " + averageArray[i]);
        }
}

Snapshot of error

错误快照

Here is the original code that had intentional errors that we had to correct, but then the instructor asked us to try and rewrite the code using different classes and methods.

import java.util.Scanner;

public class GradesArray {
public static void main (String[]args) {
    System.out.println("Welcome to your Grade Average Calculator");
    String[] nameArray = {"Jon", "Rajesh", "Elizabeth", "Julio", "Chang"};
    String[] gradeArray = new String[5];

    double[] score1Array = {67, 87, 71, 79, 100};
    double[] score2Array = {70, 90, 74, 82, 100};
    double[] score3Array = {73, 93, 77, 85, 100};
    double[] averageArray = new double[5];
    for(int i=0; i<5; i++){ 
         averageArray[i] = (score1Array[i] + score2Array[i] + score3Array[i])/3;
    if (averageArray[i] >=90){
        gradeArray[i] = "A";
    } else if (averageArray[i] >= 80){
        gradeArray[i] = "B";
    } else if (averageArray[i] >= 70){
        gradeArray[i] = "C";
    } else if (averageArray[i] >= 60){
        gradeArray[i] = "D";
    } else{
        gradeArray[i] = "F";
    }
    System.out.println("Name is: " + nameArray[i] + " Grade is: " + gradeArray[i] + " Average is: " + averageArray[i]);
    }
  }
}

System.out.println(" is out of loop, i has different vale

for (int i = 0; i < 5; i++) {
            averageArray[i] = (test1Array[i] + test2Array[i] + test3Array[i]) / 3;
            // rest of the code
            System.out.println("Name is: " + nameArray[i] + " Grade is: " + gradeArray[i] + " Average is: " + averageArray[i]);
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM