Java 显示最小/最大数组

Java 显示最小/最大数组,java,arrays,Java,Arrays,最终输出将显示谁的分数最高,谁的分数最低。 我不知道如何在最终输出中调用最低的名称/等级 在代码中,我有一些关于“currentMinIndex”的注释,“currentMaxIndex”工作得很好,并将显示给最终输出。我试着模仿它,但它不像我想象的那样。不确定带有“(int k=1;k>=m;k++)”的内容是否不正确 import java.util.*; 公共类MyArrayEX{ //把等级排在最上面 公共静态int[]反向输入(int[]数组){ int[]输入=新的int[array

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

在代码中,我有一些关于“currentMinIndex”的注释,“currentMaxIndex”工作得很好,并将显示给最终输出。我试着模仿它,但它不像我想象的那样。不确定带有“(int k=1;k>=m;k++)”的内容是否不正确

import java.util.*;
公共类MyArrayEX{
//把等级排在最上面
公共静态int[]反向输入(int[]数组){
int[]输入=新的int[array.length];
对于(int i=0,j=input.length-1;i0;i--){
//将两者都重置为0,以从数组的开头开始
int currentMax=等级[0];
int currentMaxIndex=0;
//我是一个每次都会被砍掉一个的限制
对于(int k=1;k 0;i--){
int currentMin=等级[0];
int currentMinIndex=0;
//最低等级
对于(int k=1;k>=m;k++){
如果(电流最小值<等级[m]){
currentMin=等级[m];
currentMinIndex=m;
}
}
//找到最大数字后,将该数字分配给i
//我试图让最终输出显示谁拥有它的最小/最大等级
//MinIndex是否会分配给其他变量?
等级[currentMaxIndex]=等级[i];
等级[currentMinIndex]=等级[m];
等级[i]=当前最大值;
等级[m]=当前最小值;
字符串highName=名称[currentMaxIndex];
字符串lowName=名称[currentMinIndex];
名称[currentMaxIndex]=名称[i];
名称[currentMinIndex]=名称[m];
名称[i]=高名称;
名称[m]=低名称;
//这将显示最高编号的名称和等级
系统输出打印(“\r最高等级为“+highName+”,带“+currentMax”);
//不知道怎么称呼这个。
System.out.println(“\r,最低等级为“+lowName+”,带“+currentMin”);
}
}
input.close();
键盘关闭();
}
}

您的代码有多个问题。首先是使用两个扫描仪,用于相同的
系统。在
输入流中,然后使用嵌套循环查找完全不必要的最小/最大值。因为问题是关于找到最小值/最大值,所以我将只关注这一部分,对于扫描仪,我会说移除
键盘
扫描仪,只使用
输入
扫描仪。无论如何,使用以下代码块查找具有名称的最大和最小等级:

  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);
int currentMaxIndex=0;
int currentMinIndex=0;
//获取最小最大值

对于(int i=1;i您的代码有多个问题。首先是您在同一
系统中使用的两个扫描仪。在
输入流中,第二个是使用嵌套循环来查找完全不必要的最小/最大值。因为问题是关于查找最小/最大值,所以我将只关注该部分,对于扫描仪,我将y卸下
键盘
扫描仪,仅使用
输入
扫描仪。无论如何,使用以下代码块查找具有名称的最大和最小等级:

  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);
int currentMaxIndex=0;
int currentMinIndex=0;
//获取最小最大值

对于(int i=1;i)您对最低等级的测试与对最高等级的测试完全相同。
currentmin当您只希望测试中的最高和最低等级时,为什么要对它们进行排序array@BeshambherChaukhwan不要相信你在评论(;->)中读到的一切;代码讲述了真实的故事。他实际上是在“搜索”,不是真的“分类"是的,代码真的很混乱,比如为什么循环中有一个循环,为什么同一个输入流有两个扫描仪。当他只需要一个输出时,他不会打印多行最高最低数据吗?这是一个非常混乱的代码。你可以在一个循环中找到最大和最小元素。为什么要使用两个嵌套循环d最低等级的测试与最高等级的测试太完全相同。
currentmin当你只想要最高等级和最低等级时,为什么要对它们进行排序array@BeshambherChaukhwan不要相信你在评论中读到的一切(;->);代码讲述了真实的故事。他实际上是在“搜索”,而不是真正的“排序”是的,代码真的是一团糟,比如为什么循环中有一个循环,为什么同一个输入流有两个扫描仪。难道他不想打印多行最高最低的数据吗