Created
September 15, 2015 17:04
-
-
Save SergKolo/345d19f713d9e7838f27 to your computer and use it in GitHub Desktop.
Lottery. Generate 6 random numbers untill they match your guess. Do this until you hit jackpot. Once you hit jackpot , repeat that 25 times.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.lang.Math; | |
import java.util.Random; | |
import java.util.Arrays; | |
public class scratch | |
{ | |
public static void main ( String [] args ) | |
{ | |
int lottoNums[] = new int [6]; | |
int myNums[] = {1,13,15,22,41,36}; | |
int countMatched = 0; | |
/* for (int i = 0; i < 6; i++) | |
{ | |
lottoNums[i] = getRandomInt(1,41); | |
} | |
System.out.println(lottoNums.length); | |
for (int i = 0; i < 6; i++) | |
{ | |
System.out.print(lottoNums[i] + "#"); | |
} | |
*/ | |
for (int x = 1; x<= 25; x++) | |
{ | |
System.out.println("Game #" + x); | |
while (true) | |
{ | |
//System.out.println("generating numbers"); | |
for (int i = 0; i < 6; i++) | |
{ | |
lottoNums[i] = getRandomInt(1,41); | |
} | |
if (hasDuplicates(lottoNums)) | |
{ | |
//System.out.println("Duplicates found"); | |
continue; | |
} | |
/* | |
Arrays.sort(lottoNums); | |
for(int i = 1; i < numbers.length; i++) { | |
if(numbers[i] == numbers[i - 1]) { | |
continue; | |
} | |
} | |
*/ | |
/////////////////////////////////////////// | |
/* for (int i = 0; i < 6; i++) | |
{ | |
System.out.print(lottoNums[i] + "#"); | |
} | |
System.out.printf ("\n"); | |
*/ | |
//compare myNums to lottoNums | |
// count matched numbers | |
//System.out.println("Comparing"); | |
for (int i = 0; i < 6; i++) | |
{ | |
for (int j = 0 ; j < 6; j++) | |
{ | |
if (lottoNums[j] == myNums[i]) | |
countMatched++; | |
} | |
} | |
// if we matched 6 numbers, we've got a jackpot | |
if ( countMatched == 6 ) | |
{ | |
System.out.println("Jackpot"); | |
break; | |
} | |
/*else if (countMatched == 3) | |
{ System.out.println("Matched 3");} */ | |
//we didn't match 6, so reset the counter | |
countMatched = 0; | |
} | |
} | |
} | |
public static int getRandomInt (int low, int high) | |
{ | |
int out; | |
//out = low + (int)(Math.random()*((high-low) + 1 )); | |
Random rand = new Random(); | |
out = rand.nextInt((high - low) + 1) + low; | |
// if (out == 0 || out == 42) | |
// System.out.print("ERROR" + out); | |
return out; | |
//System.out.print(out + "\n"); | |
} | |
public static boolean hasDuplicates (int[] a) | |
{ | |
boolean result = false; | |
Arrays.sort(a); | |
for(int i = 1; i < a.length; i++) | |
{ | |
if(a[i] == a[i - 1]) | |
{ | |
result = true; | |
} | |
} | |
return result; | |
} | |
} |
- Class names starts with an uppercase character
Scratch
instead ofscratch
- import java.lang.Math; is never used
- Change the visibility (
private
) forgetRandomInt
andhasDuplicates
. No need forpublic
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Optimizations: