Advertisement
marking2112

Number sequence

Nov 27th, 2018
387
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.27 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Demo {
  4.     public static void main(String[] args) {
  5.         Scanner scanner = new Scanner(System.in);
  6.  
  7.         String input = scanner.nextLine();
  8.  
  9.         int maxNum = Integer.MIN_VALUE; // Set the minimum value for Integer
  10.         int minNum = Integer.MAX_VALUE; // Set the maximum value for Integer
  11.  
  12.         while (!"END".equals(input)) { // Util the input is "END" keep rolling...
  13.             int currentNumber = Integer.parseInt(input); // parsing the input to a number.
  14.  
  15.             if (maxNum < currentNumber) {
  16.                 maxNum = currentNumber;
  17.                 /* If the maxNum value is smaller than the currentNumber,
  18.                     set currentNumber value to maxNum
  19.                 */
  20.             }
  21.             if (minNum > currentNumber) {
  22.                 minNum = currentNumber;
  23.                 /* If the minNum value is bigger than the currentNumber,
  24.                     set currentNumber value to minNum
  25.                 */
  26.             }
  27.  
  28.  
  29.             input = scanner.nextLine();
  30.             // read the next line, avoiding infinity loop.
  31.         }
  32.         System.out.printf("Max number: %d%n", maxNum);
  33.         System.out.printf("Min number: %d", minNum);
  34.         // Printing both numbers
  35.     }
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement