如何比较Java中的一些值以在列表中找到最大值?

时间:2018-11-13 18:01:42

标签: java

因此,我需要检查列表的所有值并返回最大的列表(listsize是我之前声明的变量,它是列表的大小)。事实证明,这比看起来要难。这总是为我提供列表中特定位置的值...出了什么问题?

  package act9;
import java.util.Scanner;
public class Act9 {
public static void main(String[] args) throws InterruptedException{
    Scanner input = new Scanner(System.in);
            String[] listnames = null;
            int[] listgrades = null;
            int size = 0;   
            System.out.println("How many students in your class? ");
            try {
            size = input.nextInt();
                if(size<=30) {
                    listnames = new String[size+1];
                    listgrades = new int[size+1];
                }
                else if(size>30) {
                    System.out.println("The maximum class size is 30, didn't you know?");
                }
            }catch(Exception e) {
            System.out.println("This needs to be a number, please. Jesus, how dense.");
            System.exit(0);
            }
            try {
            for(int c=0; c<size; c++) {
                System.out.println("Give the [first] name of a student:");
                listnames[c] = input.next();
                System.out.println("What was their final grade?");
                listgrades[c] = input.nextInt();
            }
            }catch (Exception f) {

                System.out.println("Did you correctly type the name and grade? you're so dumb I think I'll stop the program right here.");
                System.exit(0);
            }
        System.out.println("Out of your group of " + size + "students, this were their scores;");
        System.out.println("");
        System.out.println("name    |     grade");
        System.out.println("____________________________________");
        for(int c2=0; c2<size; c2++) {
            System.out.println(listnames[c2] + "    |     " + listgrades[c2]);
            Thread.sleep(100);

        }
        int temp = 1;
        int d = 0;
        for (d=0 ; d<size; d++) {
        }
         if(Math.max(listgrades[d],listgrades[temp])==listgrades[d]) {
             temp = d;
         }
         else {
             //temp=temp. That's what nothingness means

         }
         System.out.println("Your student with the highest grade is " + listnames[temp] + " with a grade of " + listgrades[temp] + " points. Well Done.");
        }

    }

这是完整的代码,但是有些东西令人费解(想法是有教室等等等等。成绩,等等。这是一种我无法理解的作业)

3 个答案:

答案 0 :(得分:0)

您可以使用Arrays.sort对列表进行排序

如果您的列表是一个整数数组,则可以执行以下操作:

Arrays.sort(listarray);
int highest = listarray[listarray.length - 1];

System.out.println("Your highest number is " + highest);

答案 1 :(得分:0)

int array[] = [3,6,2], largestNumber = 0;
for(int i = 0; i < array.length(); i++) {
    if(i == 0) {
        largestNumber = array[i];
    }
    int newNumber = array[i];
    if(largestNumber < newNumber) {
        largestNumber = newNumber;
    }
}
System.out.println(largestNumber);

享受。

答案 2 :(得分:0)

假设listGrades是一个数组,用于存储成绩列表。 您可以尝试在列表中找到最大值

    int[] listGrades = new int[]{1,3,5,10,6,2};
    int largest = listGrades[0]; //assume the first element is the largest
    for(int index=1; index < listGrades.length; index++)
    {
        if(listGrades[index]>largest) //if there is any element in the list larger than the current one
              largest = listGrades[index]; //replace it
         else
                  ;//do nothing
     }

     System.out.println("Largest value in the list is : " + largest);

希望这会澄清您在列表中找到最大值的问题。