Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 返回数组中最大值的函数仅在数组有一个项时起作用_Java_Arrays_Max - Fatal编程技术网

Java 返回数组中最大值的函数仅在数组有一个项时起作用

Java 返回数组中最大值的函数仅在数组有一个项时起作用,java,arrays,max,Java,Arrays,Max,我创建了一个函数来标识数组中的最大值。仅当数组有一个项时,函数调用才起作用。你能检查一下下面的代码并告诉我你的想法吗 我收到的输出 最高年级学生为:年级:0.0 需要输出(示例) 最高年级学生为:976年级:90.0 最高级别功能: public static String highestGrade(Student[] d) { // the function checks the highest grade and returns the corresponding id

我创建了一个函数来标识数组中的最大值。仅当数组有一个项时,函数调用才起作用。你能检查一下下面的代码并告诉我你的想法吗

我收到的输出

最高年级学生为:年级:0.0

需要输出(示例)

最高年级学生为:976年级:90.0

最高级别功能:

public static String highestGrade(Student[] d)   
{    // the function checks the highest grade and returns the corresponding id
    // d is an array of Student data type                
    double max = d[0].getScore();  // assigning max value for reference
    int gradeCounter = 1; // counter
    String topID = "";  // emplty string

    // looping through array
    for(  ; gradeCounter< d.length; gradeCounter++ )
    {
       if( d[gradeCounter].getID() != "") 
       // Checking if there is an ID assigned
       {   // comparing the score at index with the max value
           if(d[gradeCounter].getScore() > max)
           {   // if the score is higher than the max value, the max is updated
              max = d[gradeCounter].getScore();
              topID=d[gradeCounter].getID();
            }
        }
     }

   return topID; // returning the id that corresponds to the highest grade
}
公共静态字符串最高分(学生[]d)
{//函数检查最高等级并返回相应的id
//d是学生数据类型的数组
double max=d[0]。getScore();//为引用分配最大值
int gradeCounter=1;//计数器
字符串topID=”“;//雇员字符串
//循环通过数组
对于(;gradeCountermax)
{//如果分数高于最大值,则会更新最大值
max=d[gradeCounter].getScore();
topID=d[gradeCounter].getID();
}
}
}
return topID;//返回与最高等级对应的id
}
打印报告功能

public static void printReport(Student[] c)
{
    System.out.print("\n ***Class Report*** \n\n");
    // Looping through the array and printing both Id and Grade
    for(int idCounter = 0; idCounter<c.length; idCounter++ )
    {
        if( c[idCounter].getID() != "")
        {
            System.out.print("ID: "); 
            System.out.print(c[idCounter].getID());  
            System.out.print(" Grade: ");
            System.out.println(c[idCounter].getGrade());
        }
    }

//*******This is the part that has the issue*************

    // providing the user with the id having the highest grade        
    System.out.print("\nThe Highest Grade Student is: ");  

    // assigning a variable to the function call of highestgrade id    
    String studentHighestGrade=highestGrade(c);

    // printing the variable  to provide the id
    System.out.print(studentHighestGrade);

    // providing the user with  grade that corresponds to the id
    System.out.print(" Grade: ");  

    // declaring and initializing a variable
    double valueOfHighestGrade = 0.0;

    // Looping through the array to get the highest grade that 
   // corresponds to the ID
    for(int idCounter = 0; idCounter<c.length; idCounter++ )
    {
     // if the id at index =idCounter equals the highest id then
     // we get the grade (score) at that index and assign it to 
     // valueOfHighestGrade
      if(c[idCounter].getID().equals(studentHighestGrade))
      {
          valueOfHighestGrade=c[idCounter].getScore();
      }

    }
    // printing the highest grade (score)
    System.out.print(valueOfHighestGrade);

    countGrades( c);

    System.out.print("\n ***End of Report*** \n");
}
公共静态无效打印报告(学生[]c)
{
系统输出打印(“\n***类报告***\n\n”);
//在数组中循环并打印Id和等级

对于(int idCounter=0;idCounter建议使用equals方法比较字符串实例。

建议使用equals方法比较字符串实例。

如果数组中只有一个
学生,
当您执行
int gradeCounter=1;//counter
时,您将无法获得学生id的值

因此,在您的循环进入最高等级之前,请执行以下操作

topID = d[0].getId(); 

不确定为什么要执行
if(c[idCounter].getID()!=“”)
,但如果数组中只有一个
学生
, 当您执行
int gradeCounter=1;//counter
时,您将无法获得学生id的值

因此,在您的循环进入最高等级之前,请执行以下操作

topID = d[0].getId(); 
不确定您为什么要执行if(c[idCounter].getID()!=“”)
尽管

  • 如果最高分数在第一个条目中,您将不会获得任何输出bcz,您已将“max”设置为第一个学生的分数,但未设置“topID”
  • 如果多个学生将获得最大分数,则它将仅返回其条目位于第一位的学生。返回类型“highestGrade”为字符串,因此即使多个学生获得最高分数,您也只能在o/p中获得1个学生的分数
      • 如果最高分数在第一个条目中,您将不会获得任何输出bcz,您已将“max”设置为第一个学生的分数,但未设置“topID”
      • 如果多个学生将获得最大分数,则它将仅返回其条目位于第一位的学生。返回类型“highestGrade”为字符串,因此即使多个学生获得最高分数,您也只能在o/p中获得1个学生的分数

      问题是什么?请提供一个问题是什么?请提供一个工作正常的函数。我有另一个计算平均值的函数,我想计算只有id的项目的平均值。因此我跳过了id为“”的项目因为他们的分数=0.0,这会影响平均值。我想我只是遵循了最高级函数中的模式:)工作得很好。我有另一个函数计算平均值,我想计算只有id的项目的平均值。因此我跳过了id为“”的项目因为他们的分数=0.0,这会影响平均值。我想我只是在最高级函数中遵循了这个模式:)