繁体   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);
    }
}

我必须制作一个应用程序,让我可以说有多少人参加了测试,然后输入他们的名字和分数。 我使用了两个不同的数组,一个是字符串,一个是double。 我的输出是为了读取哪些名称低于平均值并显示名称。 我不知道如何组合这两个数组,以便它识别此分数与此名称相关,因此显示该名称。

我认为你最好的选择是创建一个包含两个字段(名称和分数)的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;
    }
}

您可以在一次迭代中简单地遍历两个数组,并填充包含String和double的自定义类型的数组。 (即学生班)

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]));
}

请注意,在这种情况下,您不再需要scoreArraynameArray来获得更好的内存利用率。

您可以使用(在第一次循环后添加):

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

或者如果你想要一行:

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])
        }
    }

另外,你应该移动行average = sum / classSize; 在循环之外,每次重新计算平均值是没有意义的。


要找出最高值,请为名称保留一个临时变量,为最高值保留另一个变量,并循环学生:

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.")

如果有关系,可以使用它来打印多个学生:

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])
        }
    }

您还可以结合循环内容,打印低于平均成绩的学生,以及找到最高成绩的学生,以提高您的课程效率:

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