我对这个简单的程序有点困惑。我必须在数组中找到第三大的否。我已经做了一些代码,但在第三大没有问题时只得到了第二大没有问题,所以请建议我这个解决方案有什么问题:
class ArrayExample {
public static void main(String[] args) {
int secondlargest = Integer.MIN_VALUE;
int thirdlargest = Integer.MIN_VALUE;
int largest = Integer.MIN_VALUE;
Scanner input = new Scanner(System.in);
System.out.println("Enter array values: ");
int arr[] = new int[5];
for (int i = 0; i < arr.length; i++) {
arr[i] = input.nextInt();
if (largest < arr[i]) {
secondlargest = largest;
largest = arr[i];
}
if (secondlargest < arr[i] && largest != arr[i]) {
thirdlargest = secondlargest;
secondlargest = arr[i];
if (thirdlargest < arr[i] && secondlargest != arr[i])
thirdlargest = arr[i];
}
}
System.out.println("Second Largest number is: " + secondlargest
+ "\nThird largest number is=====" + thirdlargest);
}
}