Advertisement
Guest User

Untitled

a guest
Nov 8th, 2020
237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.86 KB | None | 0 0
  1. import java.util.Arrays;
  2. import java.util.Scanner;
  3.  
  4. public class Lottery {
  5.  
  6.     public static void main(String[] args) {
  7.         Scanner keyboard = new Scanner(System.in); //keyboard input
  8.         int[] WINNUMS = {8, 13, 27, 53, 54};
  9.         int[] lottery = new int[5]; //array of lottery numbers
  10.         int number = 0;
  11.  
  12.         System.out.println("Check your lottery numbers here!");
  13.  
  14.         //loop for numbers 1-5 with input from user choosing numbers
  15.         while (number < lottery.length) {
  16.             System.out.println("Enter number " + (number + 1) + ":");
  17.             lottery[number] = keyboard.nextInt();
  18.  
  19.             if (lottery[number] > 99 || lottery[number] < 1) {
  20.                 System.out.println("Must be between 1 and 99");
  21.                 continue;
  22.             }
  23.             number++;
  24.         }
  25.  
  26.         System.out.println("All set. The winning numbers were: 8 13 27 53 54");
  27.         selectionSort(WINNUMS);
  28.         selectionSort(lottery);
  29.  
  30.         if (Arrays.equals(WINNUMS, lottery)) {
  31.             System.out.println("WOW! Grand prize winner!");
  32.         } else {
  33.             System.out.println("Well, you didn't win. You got " + linearSearch(WINNUMS, lottery) + " matching number(s)");
  34.         }
  35.  
  36.  
  37.     }
  38.  
  39.     public static void selectionSort(int[] array) {
  40.  
  41.         //visit all elements except last
  42.         for (int startScan = 0; startScan < array.length - 1; startScan++) {
  43.  
  44.             //assume the first element in the subsequence is the smallest
  45.             int minIndex = startScan;
  46.             int minValue = array[minIndex];
  47.  
  48.             //visit all the remaining elements
  49.             //represents one pass
  50.             for (int index = startScan + 1; index < array.length; index++) {
  51.  
  52.                 //if current element is smaller than what we know so far...
  53.                 if (array[index] < (minValue)) {
  54.  
  55.                     //keep track of this new smaller element
  56.                     minValue = array[index];
  57.                     minIndex = index;
  58.                 }
  59.             }
  60.  
  61.             //swap that small element into place
  62.             array[minIndex] = array[startScan];
  63.             array[startScan] = minValue;
  64.         }
  65.     }
  66.  
  67.     public static int linearSearch(int[] haystack, int[] needle) {
  68.         int subscript = 0;
  69.         int subscript2 = 0;
  70.  
  71.         //FLAG keeps track of when something happens
  72.         boolean found = false;
  73.  
  74.         //while there are more elements to look at AND have Not found the element
  75.         while (subscript < haystack.length && !found) {
  76.  
  77.             //if the current element matches what we're looking for
  78.             if (haystack[subscript] == needle[subscript2]) {
  79.  
  80.  
  81.                 //remember that we have found
  82.                 found = true;
  83.             }
  84.             //move to the next element
  85.             subscript++;
  86.         }
  87.         return;
  88.     }
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement