简体   繁体   中英

Combine two arrays of different data type

import java.util.Scanner;

public class scores
{
    static Scanner input = new Scanner(System.in);
    public static void main(String[] args)
    {
        System.out.print("\f");
        int classSize, counterScore, counterName;
        String name;
        double score,average, sum;

        System.out.print("Enter size of class: ");
        classSize = input.nextInt();

        int[] scoreArray = new int[classSize];
        String[] nameArray = new String[classSize];

        counterScore=1;
        counterName = 1;
        average = 0;
        sum = 0;

        for (int x = 0; x < classSize; x++)
        {
            input.nextLine();
            System.out.print("Student " + counterName++ + " Name: ");
            nameArray[x] = input.nextLine();
            System.out.print("Student " + counterScore++ + " Score: ");
            scoreArray[x] = input.nextInt();

            sum = sum + scoreArray[x];
            average = sum / classSize;
        }

        System.out.println(average);
    }
}

I have to make an app that allows me to say how many people took a test and then enter their names and scores. I have used two different arrays as one is a string and one a double. My output is meant to read which names got under the average and display the name. I do not know how to combine the two arrays so that it recognizes that this score is related to this name so display that name.

I think your best option is to create a POJO with two fields (name and score) and create an array of it:

public class Student {
    private String name;
    private int score;

    public Student(String name, int score) {    
        this.name = name;
        this.score = score;
    }

    public String getName() {
        return name;
    }

    public int getScore() {
        return score;
    }
}

You can simply iterate through the two arrays together in a single iteration and populate an array of the custom type containing a String and double. (ie a Student class)

public class Student {
    public String name;
    public double score;
    public Student(String name, double score) {
        this.name = name;
        this.score = score;
    }
}

List<Student> students = new ArrayList<Student>();

for (int x = 0; x < classSize; x++)
{
    input.nextLine();
    System.out.print("Student " + counterName++ + " Name: ");
    nameArray[x] = input.nextLine();
    System.out.print("Student " + counterScore++ + " Score: ");
    scoreArray[x] = input.nextInt();

    sum = sum + scoreArray[x];
    average = sum / classSize;

    // populate array of student
    students.add(new Student(nameArray[x], scoreArray[x]));
}

Note that in this case, you don't need to have the scoreArray and nameArray anymore for better memory utilization.

You can use (add this after your first loop):

for (int i = 0; i < classSize; i++)
    {
        if(scoreArray[i] < average) {
            System.out.println(nameArray[i])
        }
    }

Or if you want it all on one line:

System.out.println("The following students are below average: ")
boolean first = true;
for (int i = 0; i < classSize; i++)
    {
        if(scoreArray[i] < average) {
            if(!first) {
                System.out.println(", ");
                first = false;
            }
            System.out.print(nameArray[i])
        }
    }

Also, you should move the line average = sum / classSize; outside of your loop, there's no point in re-calculating the average each time.


To find out the highest value, keep a temporary variable for the name and another for the highest value, and loop through the students:

String highestName = "";
double highestValue = 0;
for (int i = 0; i < classSize; i++) {
    if(scoreArray[i] > highestValue) {
        highestName = nameArray[i];
        highestValue = scoreArray[i];
    }
}
System.out.println(highestName + " has the highest grade.")

Or use this to print more than one student if there's a tie:

String[] highestNames = new String[classSize];
int numHighest = 0;
double highestValue = 0;
for (int i = 0; i < classSize; i++) {
    if(scoreArray[i] > highestValue) {
        highestNames[0] = nameArray[i];
        numHighest = 1;
        highestValue = scoreArray[i];
    } else if(scoreArray[i] > highestValue) {
        highestNames[numHighest] = nameArray[i];
        numHighest = numHighest + 1;
    }
}

System.out.println("The following student(s) has/have the highest grade: ")
boolean first2 = true;
for (int i = 0; i < numHighest; i++)
    {
        if(!first2) {
            System.out.println(", ");
            first2 = false;
        }
        System.out.print(highestNames[i])
        }
    }

You can also combine the content of the loop for printing students with grades below average with the one for finding the highest grades to make your program more efficient:

String[] highestNames = new String[classSize];
int numHighest = 0;
double highestValue = 0;
System.out.println("The following students are below average: ")
boolean first = true;
for (int i = 0; i < classSize; i++)
    {
        if(scoreArray[i] < average) {
            if(!first) {
                System.out.println(", ");
                first = false;
            }
            System.out.print(nameArray[i])
        }
        if(scoreArray[i] > highestValue) {
            highestNames[0] = nameArray[i];
            numHighest = 1;
            highestValue = scoreArray[i];
        } else if(scoreArray[i] > highestValue) {
            highestNames[numHighest] = nameArray[i];
            numHighest = numHighest + 1;
        }
    }

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