Advertisement
JoshJurban

Exercise 7.3

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