ultravibez

User Input Homework

Oct 17th, 2017
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.93 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 select = 0;
  10.         int input = 0;
  11.         int count = 0;
  12.         int max = 0;
  13.         int min = 0;
  14.         int index = 0;
  15.         int[] nums = new int[10];
  16.         int[] temp = new int[10];
  17.         boolean check = true;
  18.         while (select != 4) {
  19.             System.out.println("Please choose an option:");
  20.             System.out.println("1. Enter a number:");
  21.             System.out.println("2. Print the greatest number.");
  22.             System.out.println("3. Print the smallest number.");
  23.             System.out.println("4. Exit program.");
  24.             System.out.println("5. Print all the numbers.");
  25.             System.out.println("6. Clear all the numbers.");
  26.             select = scanner.nextInt();
  27.             if (input > max)
  28.                 max = input;
  29.             if (input < min)
  30.                 min = input;
  31.  
  32.             switch (select) {
  33.                 case 1:
  34.                     if (count > nums.length - 1)
  35.                         System.out.println("You can't print any more numbers.");
  36.                     else {
  37.                         System.out.print("Enter an integer: ");
  38.                         input = scanner.nextInt();
  39.                         nums[count] = input;
  40.                         count++;
  41.                     }
  42.                     if (count > nums.length)
  43.                         System.out.println("You can't print any more numbers.");
  44.                     if (check) {
  45.                         min = input;
  46.                         max = input;
  47.                         check = false;
  48.                     }
  49.                     if (max < input)
  50.                         index++;
  51.                     break;
  52.                 case 2:
  53.                     if(count == 0)
  54.                         check = true;
  55.                     if (check) {
  56.                         System.out.println("You must enter a number first!");
  57.                     } else {
  58.                         System.out.println(max);
  59.                         for (int i = index; i < count; i++) {
  60.                             nums[i] = nums[i + 1];
  61.                         }
  62.                         nums[count] = 0;
  63.                         count--;
  64.                         max = 0;
  65.                         for (int i = 0; i < count; i++) {
  66.                             if (nums[i] > max) {
  67.                                 max = nums[i];
  68.                                 index = i;
  69.                             }
  70.                         }
  71.                     }
  72.                     break;
  73.                 case 3:
  74.                     if (check) {
  75.                         System.out.println("You must enter a number first!");
  76.                     } else
  77.                         System.out.println(min);
  78.                     break;
  79.                 case 4:
  80.                     System.out.println("The program will now exit, bye!");
  81.                     break;
  82.                 case 5:
  83.                     if (count == 0) {
  84.                         System.out.println("No numbers entered.");
  85.                         min = 0;
  86.                         max = 0;
  87.                         check = true;
  88.                     }
  89.                     for (int i = 0; i < count; i++) {
  90.                         System.out.print(i == count - 1 ? nums[i] : nums[i] + ",");
  91.                     }
  92.                     System.out.println("");
  93.                     break;
  94.                 case 6:
  95.                     for (int i = 0; i < nums.length; i++) {
  96.                         nums[i] = 0;
  97.                     }
  98.                     count = 0;
  99.                     System.out.println("All the numbers deleted.");
  100.                     break;
  101.                 default:
  102.                     System.out.println("Invalid number!");
  103.                     break;
  104.             }
  105.         }
  106.     }
  107. }
Add Comment
Please, Sign In to add comment