【问题标题】:Display Min/Max array显示最小/最大数组
【发布时间】:2021-02-28 05:06:35
【问题描述】:

最终输出将显示谁的成绩最高,谁的成绩最低。 我不知道如何将最低名称/等级称为最终输出。

在代码中,我有一些 cmets 在我遇到“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();
    }
}

【问题讨论】:

  • 您对最低成绩的测试与最高成绩的测试完全一样。 currentMin&lt;grades[m]。这是错误的方法;你想要grades]m] &lt; currentMincurrentMin &gt;= grades[m]
  • 当你只想要数组中的最高和最低时,你为什么要对它们进行排序
  • @BeshambherChaukhwan 不要相信你在 cmets (;->) 中读到的所有内容;代码讲述了真实的故事。他实际上是在“搜索”,而不是真正的“分类”。
  • 是的,代码真的很混乱,比如为什么循环中的循环以及为什么有 2 个扫描仪用于相同的输入流。当他只想要一个输出时,他不会为最高最低数据打印多行吗?
  • 这是一个非常令人困惑的代码。您可以在单个循环中找到最大和最小元素。为什么使用two nested loops

标签: java arrays


【解决方案1】:

您的代码存在多个问题。首先是使用 2 个扫描仪用于相同的 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 /最大指数。如果当前索引值大于currentMaxIndex,那么我们将其复制到currentMaxIndexcurrentMinIndex 相同但相反。所以最后我们将得到grades数组的最高和最低值索引。完整代码在这里https://ideone.com/Qjf48p

【讨论】:

  • 非常感谢您的解释!说到这个,我还是很耳目一新,我正在通过 YouTube 学习一切,并在互联网上进行研究。看到其他回复也对我有帮助。非常感谢。
  • @Ali 从基础开始。在这里查看这个网站它非常有帮助geeksforgeeks.org
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-07-21
  • 1970-01-01
  • 2012-09-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多