Advertisement
micher43

E7.3

Nov 14th, 2014
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.43 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3.  
  4. public class LargestInArray {
  5.  
  6.     public static void main(String[] args) {
  7.         final int LENGTH = 100;
  8.         int[] values = new int[LENGTH];
  9.         int currentSize = 0;
  10.  
  11.         System.out.println("enter valuess");
  12.         Scanner in = new Scanner(System.in);
  13.         while (in.hasNextDouble() && currentSize < values.length) {
  14.             values[currentSize] = in.nextInt();
  15.             currentSize++;
  16.         }
  17.         in.close();
  18.  
  19.         // finding the largest and smallest values
  20.         int largest = values[0];
  21.         int smallest = values[0];
  22.         for (int i = 1; i < currentSize; i++) {
  23.             if (values[i] > largest) {
  24.                 largest = values[i];
  25.             }
  26.             if (values[i] < smallest) {
  27.                 smallest = values[i];
  28.             }
  29.         }
  30.  
  31.         //printing stuff
  32.         for (int i = 0; i < currentSize; i++) {
  33.             System.out.print(values[i]);
  34.            
  35.             if (values[i] == largest && smallest != largest) {
  36.                 System.out.print(" is the largest value");
  37.                 }
  38.             else if (values[i] == smallest && smallest != largest)
  39.             {
  40.                 System.out.print(" is the smallest value");
  41.             }
  42.             else
  43.             {
  44.                 System.out.print(" is the smallest and largest value");
  45.             }
  46.             System.out.println();
  47.         }
  48.     }
  49.  
  50.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement