RGAlex

Min and Max Challenge

Apr 17th, 2021 (edited)
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.71 KB | None | 0 0
  1. package com.company;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class Main {
  6.  
  7.     public static void main(String[] args) {
  8.         Scanner scanner = new Scanner(System.in);
  9.         int min = Integer.MAX_VALUE;
  10.         int max = Integer.MIN_VALUE;
  11.         while (true) {
  12.             System.out.println("Enter number: ");
  13.             boolean isAnInt = scanner.hasNextInt();
  14.             if (isAnInt) {
  15.                 int number = scanner.nextInt();
  16.                 if (number > max) max = number;
  17.                 if (number < min) min = number;
  18.             } else break;
  19.             scanner.nextLine();
  20.         }
  21.         System.out.println("Min = " + min + ", Max = " + max);
  22.         scanner.close();
  23.     }
  24. }
  25.  
Add Comment
Please, Sign In to add comment