Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Arrays;
- import java.util.Scanner;
- public class Lottery {
- public static void main(String[] args) {
- Scanner keyboard = new Scanner(System.in); //keyboard input
- int[] WINNUMS = {8, 13, 27, 53, 54};
- int[] lottery = new int[5]; //array of lottery numbers
- int number = 0;
- System.out.println("Check your lottery numbers here!");
- //loop for numbers 1-5 with input from user choosing numbers
- while (number < lottery.length) {
- System.out.println("Enter number " + (number + 1) + ":");
- lottery[number] = keyboard.nextInt();
- if (lottery[number] > 99 || lottery[number] < 1) {
- System.out.println("Must be between 1 and 99");
- continue;
- }
- number++;
- }
- System.out.println("All set. The winning numbers were: 8 13 27 53 54");
- selectionSort(WINNUMS);
- selectionSort(lottery);
- if (Arrays.equals(WINNUMS, lottery)) {
- System.out.println("WOW! Grand prize winner!");
- } else {
- System.out.println("Well, you didn't win. You got " + linearSearch(WINNUMS, lottery) + " matching number(s)");
- }
- }
- public static void selectionSort(int[] array) {
- //visit all elements except last
- for (int startScan = 0; startScan < array.length - 1; startScan++) {
- //assume the first element in the subsequence is the smallest
- int minIndex = startScan;
- int minValue = array[minIndex];
- //visit all the remaining elements
- //represents one pass
- for (int index = startScan + 1; index < array.length; index++) {
- //if current element is smaller than what we know so far...
- if (array[index] < (minValue)) {
- //keep track of this new smaller element
- minValue = array[index];
- minIndex = index;
- }
- }
- //swap that small element into place
- array[minIndex] = array[startScan];
- array[startScan] = minValue;
- }
- }
- public static int linearSearch(int[] haystack, int[] needle) {
- int subscript = 0;
- int subscript2 = 0;
- //FLAG keeps track of when something happens
- boolean found = false;
- //while there are more elements to look at AND have Not found the element
- while (subscript < haystack.length && !found) {
- //if the current element matches what we're looking for
- if (haystack[subscript] == needle[subscript2]) {
- //remember that we have found
- found = true;
- }
- //move to the next element
- subscript++;
- }
- return;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement