程序问答   发布时间:2022-06-01  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了显示最小/最大阵列大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决显示最小/最大阵列?

开发过程中遇到显示最小/最大阵列的问题如何解决?下面主要结合日常开发的经验,给出你关于显示最小/最大阵列的解决方法建议,希望对你解决显示最小/最大阵列有所启发或帮助;

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

在代码中,我对“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 输入流的 2 个扫描器,其次是使用嵌套循环来查找完全不必要的最小值/最大值。由于问题是关于找到最小值/最大值,因此我将只关注该部分,对于扫描仪,我会说移除 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 与当前索引元素进行比较值并相应地更改我们的最小/最大索引。如果当前索引值大于 currentMaxIndex,那么我们将其复制到 currentMaxIndex 并且与 currentMinIndex 相同但相反。所以最后我们将拥有 grades 数组的最高和最低值索引。完整代码在这里https://ideone.com/Qjf48p

大佬总结

以上是大佬教程为你收集整理的显示最小/最大阵列全部内容,希望文章能够帮你解决显示最小/最大阵列所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: