0

Min and max problem >>>Java<<<

Hello, I'm doing the Minimum and Maximum problem from the Sorting Lists chapter and i don't know what is wrong and why SoloLearn is not certifying the problem as correct. (Test case 4 is still wrong). "The program you are given declares ArrayList of integers. Complete the program to take numbers as input and add them to ArrayList until its size isn't equal to 5. Then output its maximum and minimum values. Sample Input 4 12 3 88 96 Sample Output 96 3" My code is: import java.util.Scanner; import java.util.ArrayList; import java.util.Collections; public class Main { public static void main(String[ ] args) { ArrayList<Integer> nums = new ArrayList<Integer>(); Scanner scanner = new Scanner(System.in); while(nums.size()<5){ int num = scanner.nextInt(); //your code goes here nums.add(num); } Collections.sort(nums,Collections.reverseOrder()); //your code goes here for (int s:nums){ if (s==Collections.max(nums) || s==Collections.min(nums)) System.out.println(s); } } }

15th Jun 2021, 10:20 AM
Remus Tanasa
7 odpowiedzi
+ 2
Remove the for loop and write this: System.out.println(Collections.max(num)); System.out.print(Collections.min(num)); ...and by the way, there is no need to sort the ArrayList either.
15th Jun 2021, 11:29 AM
Jan
Jan - avatar
+ 13
import java.util.Scanner; import java.util.ArrayList; import java.util.Collections; //Please Subscribe to My Youtube Channel //Channel Name: Fazal Tuts4U public class Main { public static void main(String[ ] args) { ArrayList<Integer> nums = new ArrayList<Integer>(); Scanner scanner = new Scanner(System.in); while(nums.size()<5){ int num = scanner.nextInt(); nums.add(num); } int i = Collections.max(nums); System.out.println(i); i = Collections.min(nums); System.out.println(i); } }
5th Sep 2021, 7:57 AM
Fazal Haroon
Fazal Haroon - avatar
+ 2
import java.util.Scanner; import java.util.ArrayList; import java.util.Collections; public class Main { public static void main(String[ ] args) { ArrayList<Integer> nums = new ArrayList<Integer>(); Scanner scanner = new Scanner(System.in); while(nums.size()<5){ int num = scanner.nextInt(); //your code goes here nums.add(num); } //your code goes here System.out.println(Collections.max(nums)); System.out.println(Collections.min(nums)); } }
20th Dec 2021, 6:39 AM
Ika Purnamasari
0
I think if it has duplicates of max or min then it may need only one need to print. Like 3 3 5 97 97 output is 97 3 so just try with printing first and last number. Then no need to loop..
15th Jun 2021, 11:29 AM
Jayakrishna 🇮🇳
0
I cannot access this challenge because I don't have pro. But what if the list contains multiple maximum and minimum? Something like {30, 30, 15, 10, 10} Your code will output 30 30 10 10, while I think it should be output once only. Besides, you can just do nums[0], nums[4] to get max and min as you sort the array.
15th Jun 2021, 11:31 AM
你知道規則,我也是
你知道規則,我也是 - avatar
0
Many thanks to my community!!
15th Jun 2021, 11:42 AM
Remus Tanasa
0
import java.util.Scanner; import java.util.ArrayList; import java.util.Collections; public class Main { public static void main(String[ ] args) { ArrayList<Integer> nums = new ArrayList<Integer>(); Scanner scanner = new Scanner(System.in); while(nums.size()<5){ int num = scanner.nextInt(); nums.add(num); } int maximum = Collections.max(nums); int minimum = Collections.min(nums); System.out.println(maximum); System.out.println(minimum); } }
3rd Aug 2022, 11:47 PM
Ariaie M
Ariaie M - avatar