kingasen

maxArray

Oct 28th, 2021
76
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class FindMaxWithForeach {
  2.  
  3. public static void main(String[] args) {
  4. Scanner scn = new Scanner(System.in);
  5. System.out.print("Enter count of numbers: ");
  6. int count = Integer.parseInt(scn.nextLine());
  7.  
  8. // Read Array
  9. int[] numbers = new int[count];
  10. for (int i = 0; i < count; i++) {
  11. // For readability start numbers from 1
  12. // Still, the array's index ALWAYS starts from 0
  13. System.out.printf("Enter number %d: ", i + 1);
  14. int nextNumber = Integer.parseInt(scn.nextLine());
  15. numbers[i] = nextNumber;
  16. }
  17.  
  18. // Find Max
  19. int max = Integer.MIN_VALUE;
  20. for (int number : numbers) {
  21. if (number > max) {
  22. max = number;
  23. }
  24. }
  25.  
  26. System.out.printf("Max: %d", max);
  27. }
  28. }
RAW Paste Data