显示最小/最大阵列



最终输出将显示谁的分数最高,谁的分数最低。我不知道如何在最终输出中调用最低的名称/等级。

在代码中,我对我使用";currentMinIndex";,";currentMaxIndex";工作正常,并将显示给最终输出。我试着把它照出来,但它并不像我预期的那样。不确定是否有";(int k=1;k>=m;k++)";不正确。

import java.util.*;
public class MyArrayEX {
// Sort grades lowest on top
public static int[] reverseInt(int[] array) {
int[] input = new int[array.length];
for (int i = 0, j = input.length - 1; i < array.length; i++, j--) {
input[j] = array[i];
}
return input;
}
public static void main(String[] args) {
// Scanners
Scanner input = new Scanner(System.in);
Scanner keyboard = new Scanner(System.in);
// Input amount
System.out.print("nEnter number of students: ");
int numOfStu = input.nextInt(); // Number of Students 
int[] grades = new int[numOfStu];
String[] names = new String[numOfStu];
// Start loop, amount is based off of "numOfStu"
for (int i = 0; i < numOfStu; i++) {
System.out.print("rEnter student first name: ");
String name = keyboard.next();
System.out.print("Enter the students grade: ");
int grade = input.nextInt();
// Assigning i
names[i] = name;
grades[i] = grade;

//System.out.println("");
}
// This is the area that sorts it from least to greatest    
// i is the indexed value of the last number in array   
for (int i = grades.length - 1; i > 0; i--) {
// Resets both to 0 to start at the beginning of the array  
int currentMax = grades[0];
int currentMaxIndex = 0;
// i is back-limit that gets chopped off by one each time   
for (int k = 1; k <= i; k++) {
if (currentMax < grades[k]) {
currentMax = grades[k];
currentMaxIndex = k;
}
}
// This is where im lost on how to call the min value
// Trying to mirror the one above but using it
// to show the minimum grade along with the name            
for (int m = grades.length - 1; i > 0; i--) {
int currentMin = grades[0];
int currentMinIndex = 0;
// Min grades
for (int k = 1; k >= m; k++) {
if (currentMin < grades[m]) {
currentMin = grades[m];
currentMinIndex = m;
}
}
// After largest number is found, assign that number to i 
// Im trying to have the final output show the min/max grade with who has it
// Would the MinIndex be assigned to a different variable? 
grades[currentMaxIndex] = grades[i];
grades[currentMinIndex] = grades[m];
grades[i] = currentMax;
grades[m] = currentMin;
String highName = names[currentMaxIndex];
String lowName = names[currentMinIndex];
names[currentMaxIndex] = names[i];
names[currentMinIndex] = names[m];
names[i] = highName;
names[m] = lowName;
// This shows the name and grade for the highest number
System.out.print("rThe highest grade is " + highName + " with a " + currentMax);
// Unsure how to call this.
System.out.println("r and the Lowest grade is " + lowName + " with a " + currentMin); 
}
}
input.close();
keyboard.close();
}
}

您的代码有多个问题。第一个是使用两个扫描仪,用于相同的System.in输入流,第二个是使用嵌套循环来查找完全不必要的最小值/最大值。由于问题是关于找到最小值/最大值,所以我将只关注该部分,对于扫描仪,我会说去掉keyboard扫描仪,只使用input扫描仪。无论如何,使用以下代码块来查找具有名称的最大和最小等级:

int currentMaxIndex = 0;
int currentMinIndex =  0;
// Get min max
for (int i = 1; i<grades.length; i++) { 
if (grades[currentMaxIndex]<grades[i]) { 
currentMaxIndex=i;
}
if (grades[currentMinIndex]>grades[i]) { 
currentMinIndex=i;
}
}

String highName = names[currentMaxIndex];
String lowName = names[currentMinIndex];
int currentMax = grades[currentMaxIndex];
int currentMin = grades[currentMinIndex];

System.out.print("rThe highest grade is " + highName + " with a " + currentMax);
System.out.println("r and the Lowest grade is " + lowName + " with a " + currentMin);

方法非常简单。我们首先假设grades数组中的第一个元素是min和max,然后我们循环到从1grades.length的其余元素,并将索引min/max与当前索引元素值进行比较,并相应地更改我们的min/max索引。如果当前索引值大于currentMaxIndex,则我们将其复制到currentMaxIndex,并且与currentMinIndex相同但相反。因此,最终我们将得到grades阵列的最高和最低值索引。完整的代码在这里https://ideone.com/Qjf48p

最新更新