有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

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

我收到的输出

最高年级学生为:年级: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
}

打印报告功能

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");
}

共 (3) 个答案

  1. # 1 楼答案

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

    所以在highestGrade循环之前

    topID = d[0].getId(); 
    

    不知道你为什么要这么做

  2. # 2 楼答案

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

  3. # 3 楼答案

    • 如果最高分数在第一个条目中,你将不会得到任何输出bcz,你已将“max”设置为第一个学生的分数,但未设置“topID”
    • 如果多个学生将获得最高分数,那么它将只返回第一个学生。“highestGrade”的返回类型是字符串,所以即使多个学生获得最高分数,在o/p中也只能获得一个学生的分数