Java game that generates and compares random numbers, looking for matches - Code Review Stack Exchange


i have written java code school assignment. game, , works follows:

the game makes 4 random numbers, different, users ticket. generates 4 other random numbers, again different, compare to. if match, win. if don't match, lose.

a = random 0-9
b = random 0-9
c = random 0-9
d = random 0-9

a cannot equal b / c / d
b cannot equal / c / d
c cannot equal / b / d
d cannot equal / b / c

here code:

import java.util.random; import java.util.scanner;  public class lottery {     public static void main(string[] args) {        scanner scan = new scanner(system.in);        system.out.println("lottery game");          random rand = new random();       // creating 4 individual balls       int ball1 = 0;       int ball2 = 0;       int ball3 = 0;        int ball4 = 0;       int guess1 = 0;       int guess2 = 0;       int guess3 = 0;       int guess4 = 0;        // users ticket number        system.out.println("generating ticket");          guess1 = rand.nextint(10);       guess2 = rand.nextint(10);       guess3 = rand.nextint(10);       guess4 = rand.nextint(10);        {                guess1 = rand.nextint(10);           guess2 = rand.nextint(10);           guess3 = rand.nextint(10);           guess4 = rand.nextint(10);         if ( guess1 == guess2 || guess1 == guess3 || guess1 == guess4 ) {           guess1 = rand.nextint(10);       }       if ( guess2 == guess3 || guess2 == guess4 ) {           guess2 = rand.nextint(10);       }       if ( guess3 == guess4 ) {           guess3 = rand.nextint(10);       }        } while (guess1 == guess2 || guess2 == guess3 || guess3 == guess4 || guess4 == ball1);        system.out.println(guess1 + " " + guess2 + " " + guess3 + " " + guess4);        system.out.println("are happy selected numbers?");        int confirmation = 0;        system.out.println("please type '1' 'yes' or '2' 'no");        confirmation = scan.nextint();        system.out.println(confirmation);         if (confirmation == 1) {            ball1 = rand.nextint(10);           ball2 = rand.nextint(10);           ball3 = rand.nextint(10);           ball4 = rand.nextint(10);            {                    ball1 = rand.nextint(10);               ball2 = rand.nextint(10);               ball3 = rand.nextint(10);               ball4 = rand.nextint(10);             if ( ball1 == ball2 || ball1 == ball3 || ball1 == ball4 ) {               ball1 = rand.nextint(10);           }           if ( ball2 == ball3 || ball2 == ball4 ) {               ball2 = rand.nextint(10);           }           if ( ball3 == ball4 ) {               ball3 = rand.nextint(10);           }            } while (ball1 == ball2 || ball2 == ball3 || ball3 == ball4 || ball4 == ball1);            system.out.println("the winning numbers follows...");           system.out.println(ball1 + " " + ball2 + " " + ball3 + " " + ball4);            if ( ball1 == guess1 &&  ball2 == guess2 &&  ball3 == guess3 &&  ball4 == guess4) {               system.out.println("congratulations! you've won £1,000,000!");               system.exit(0);           }           else {               system.out.println("unfortunately, did not win. better luck next time!");               system.exit(0);           }         }       else if (confirmation == 2) {           system.out.println("exiting game, playing!");       }       else {           system.out.println("error");       }        }     } 

specifically, improve algorithm i've used here. don't think elegant, though work.

i'm interested in other ways code can improved. appreciate suggestions.

besides minor improvements consistent formatting , proper indentation target random number generation:

int guess1 = 0; int guess2 = 0; int guess3 = 0; int guess4 = 0;  guess1 = rand.nextint(10); guess2 = rand.nextint(10); guess3 = rand.nextint(10); guess4 = rand.nextint(10);  {          guess1 = rand.nextint(10);     guess2 = rand.nextint(10);     guess3 = rand.nextint(10);     guess4 = rand.nextint(10);   if ( guess1 == guess2 || guess1 == guess3 || guess1 == guess4 ) {     guess1 = rand.nextint(10); } if ( guess2 == guess3 || guess2 == guess4 ) {     guess2 = rand.nextint(10); } if ( guess3 == guess4 ) {     guess3 = rand.nextint(10); }  } while (guess1 == guess2 || guess2 == guess3 || guess3 == guess4 || guess4 == ball1); 

while not incorrect, looks bit clumsy.

since want 4 unique random numbers, better choice create list of numbers 0-9, shuffle it , take first 4 elements. , while you're @ it, convert 4 individual variables guess1 guess4 single int[] array of guesses.

this allows replace above code simply:

list<integer> numbers = intstream.range(0, 9).boxed()         .collect(collectors.tolist()); collections.shuffle(numbers); int[] guesses = new int[4]; (int = 0; < 4; i++) {     guesses[i] = numbers.get(i); } 

(refactor separate method , can re-use balls generation well.)

when guesses , balls both arrays, comparison simplified to:

arrays.equals(guesses, balls); 

arrays.equals:

returns true if 2 specified arrays of ints equal 1 another. 2 arrays considered equal if both arrays contain same number of elements, , corresponding pairs of elements in 2 arrays equal. in other words, two arrays equal if contain same elements in same order.

(emphasis me)


Comments

Popular posts from this blog

ios - MKAnnotationView layer is not of expected type: MKLayer -

ZeroMQ on Windows, with Qt Creator -

unity3d - Unity SceneManager.LoadScene quits application -