[英]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]));
}
請注意,在這種情況下,您不再需要scoreArray
和nameArray
來獲得更好的內存利用率。
您可以使用(在第一次循環后添加):
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.