简体   繁体   English

组合两个不同数据类型的数组

[英]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. 我使用了两个不同的数组,一个是字符串,一个是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: 我认为你最好的选择是创建一个包含两个字段(名称和分数)的POJO并创建一个数组:

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. 您可以在一次迭代中简单地遍历两个数组,并填充包含String和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. 请注意,在这种情况下,您不再需要scoreArraynameArray来获得更好的内存利用率。

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; 另外,你应该移动行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;
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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