简体   繁体   中英

Java Lottery Array Program

I am designing a program for my class that is supposed to simulate a lottery game. I am supposed to design a method that generates random lottery numbers, a method that asks and stores the user for their number choices, a method that compares the arrays to find how many numbers are the same, and then I am supposed to call them all back up to the main method, and create my output statement that contains some if statements that determine which prize is awarded for the particular amount of matches.

Here is what I have thus far

import java.util.*;


public class LotteryGame {
/**
  The main method is the program's starting point 
*/
public static void main(String[] args){

  int NUM_DIGITS = 5;

  int[] userDigits = new int[5];
  int[] lotteryNumbers = new int[5];
  int sameNum;

  generateNumbers(lotteryNumbers);
  getUserData(userDigits);
  compareArrays();


  System.out.println("Lottery numbers: " + lotteryNumbers[0] + " " +
  lotteryNumbers[1] + " " + lotteryNumbers[2] + " " + lotteryNumbers[3] +
  " " + lotteryNumbers[4] + " ");

  System.out.println("Player numbers:  " + userDigits[0] + " " + userDigits[1] + " " + userDigits[2] + " " + userDigits[3] + " " + userDigits[4] + " ");
  System.out.println("Number of matching digits: " + sameNum);

  if (sameNum == 5){
     System.out.println("GRAND PRIZE WINNER - $5 MILLION!!");
     }

  if (sameNum == 4){
     System.out.println("SUPER PRIZE WINNER - $500,000!!");
     }

  if (sameNum == 3){
     System.out.println("GOOD PRIZE WINNER - $5,000!!");
     }

  if (sameNum == 2){
     System.out.println("NICE PRIZE WINNER - $500!!");
     } 

  if (sameNum == 1){
     System.out.println("WINNER - $5!!");
  }
  if (sameNum ==0){
     System.out.println("No matching numbers - better luck next time");
     }


} 
public static int generateNumbers(int [] lotteryNumbers){


  Random randNum = new Random();

  lotteryNumbers[0] = randNum.nextInt(10);
  lotteryNumbers[1] = randNum.nextInt(10);
  lotteryNumbers[2] = randNum.nextInt(10);  
  lotteryNumbers[3] = randNum.nextInt(10);
  lotteryNumbers[4] = randNum.nextInt(10);

  return lotteryNumbers[4];
}

public static int getUserData (int [] userDigits){
  Scanner keyboard = new Scanner(System.in);

  System.out.print("Enter digit 1: ");
  userDigits[0] = keyboard.nextInt();
  System.out.print("Enter digit 2: ");
  userDigits[1] = keyboard.nextInt();
  System.out.print("Enter digit 3: ");
  userDigits[2] = keyboard.nextInt();
  System.out.print("Enter digit 4: ");
  userDigits[3] = keyboard.nextInt();
  System.out.print("Enter digit 5: ");
  userDigits[4] = keyboard.nextInt();

  return userDigits[4];

}

public static int compareArrays (int [] userDigits, 
                                    int [] lotteryNumbers){

 int sameNum = 0;

 for (int i = 0; i < 5; i++){

     for (int x = 0; x < 5; x++){

         if (lotteryNumbers[i] == userDigits[x]){
           sameNum++;
           }
              return sameNum;
              }                           

              return sameNum;                         
  }
              return sameNum;

 }

}

I am very new to arrays (and Java at that) so my problems are in my return/call statements. Please excuse my spacey coding style and any blatant mistakes that I have made. Any tips,advice, solutions, or if you notice anything wrong with what I have please let me know. Thanks!

I've updated your code with the changes you'll need.

  • Added NUM_DIGITS to your initializer
  • Closed your Scanner
  • Removed the early returns in your comparison method
  • Assigned the return value of the comparison method to sameNum
  • Set the return value of the generation and get methods to void

The other suggestions might be something you want to incorporate (such as the switch/case).

import java.util.Random;
import java.util.Scanner;

public class App {

    public static void main(String[] args) {

        int NUM_DIGITS = 5;

        int[] userDigits = new int[NUM_DIGITS];
        int[] lotteryNumbers = new int[NUM_DIGITS];
        int sameNum;

        generateNumbers(lotteryNumbers);
        getUserData(userDigits);
        sameNum = compareArrays(lotteryNumbers, userDigits);

        System.out.println("Lottery numbers: " + lotteryNumbers[0] + " "
                + lotteryNumbers[1] + " " + lotteryNumbers[2] + " "
                + lotteryNumbers[3] + " " + lotteryNumbers[4] + " ");

        System.out.println("Player numbers:  " + userDigits[0] + " "
                + userDigits[1] + " " + userDigits[2] + " " + userDigits[3]
                + " " + userDigits[4] + " ");
        System.out.println("Number of matching digits: " + sameNum);

        if (sameNum == 5) {
            System.out.println("GRAND PRIZE WINNER - $5 MILLION!!");
        }

        if (sameNum == 4) {
            System.out.println("SUPER PRIZE WINNER - $500,000!!");
        }

        if (sameNum == 3) {
            System.out.println("GOOD PRIZE WINNER - $5,000!!");
        }

        if (sameNum == 2) {
            System.out.println("NICE PRIZE WINNER - $500!!");
        }

        if (sameNum == 1) {
            System.out.println("WINNER - $5!!");
        }
        if (sameNum == 0) {
            System.out.println("No matching numbers - better luck next time");
        }

    }

    public static void generateNumbers(int[] lotteryNumbers) {

        Random randNum = new Random();

        lotteryNumbers[0] = randNum.nextInt(10);
        lotteryNumbers[1] = randNum.nextInt(10);
        lotteryNumbers[2] = randNum.nextInt(10);
        lotteryNumbers[3] = randNum.nextInt(10);
        lotteryNumbers[4] = randNum.nextInt(10);

        return lotteryNumbers[4];
    }

    public static void getUserData(int[] userDigits) {
        Scanner keyboard = new Scanner(System.in);

        System.out.print("Enter digit 1: ");
        userDigits[0] = keyboard.nextInt();
        System.out.print("Enter digit 2: ");
        userDigits[1] = keyboard.nextInt();
        System.out.print("Enter digit 3: ");
        userDigits[2] = keyboard.nextInt();
        System.out.print("Enter digit 4: ");
        userDigits[3] = keyboard.nextInt();
        System.out.print("Enter digit 5: ");
        userDigits[4] = keyboard.nextInt();

        keyboard.close();

        return userDigits[4];
    }

    public static int compareArrays(int[] userDigits, int[] lotteryNumbers) {
        int sameNum = 0;

        for (int i = 0; i < 5; i++) {
            for (int x = 0; x < 5; x++) {
                if (lotteryNumbers[i] == userDigits[x]) {
                    sameNum++;
                }
            }
        }
        return sameNum;
    }

}

Keep in mind that randNum.nextInt(10) will give you lottery numbers that range from 0 to 9. You can use a for loop to assign the random numbers to the lotteryNumbers array easier. Also, you should make sure that the random lottery numbers don't repeat.

In your compareArrays function, just put one return sameNum call after the outermost for loop, otherwise it won't update with the correct number of matching numbers. You need to give compareArrays() the correct parameters (userDigits and lotteryNumbers) and set sameNum equal to this result.

Several points you might find useful:

  1. You might want to use NUM_DIGITS for the initiation of the arrays since that's the whole point of having named constant:

     int[] userDigits = new int[NUM_DIGITS]; int[] lotteryNumbers = new int[NUM_DIGITS]; 
  2. You can use Arrays.toString() to output arrays, eg:

     System.out.println(Arrays.toString(lotteryNumbers)); 
  3. Instead of multiple if s use switch , plus do not repeat the whole print statement, only assign the part that differs:

     String prize = ""; switch (sameNum) { case 5: prize = "GRAND PRIZE WINNER - $5 MILLION!!"; break; case 4: prize = "SUPER PRIZE WINNER - $500,000!!"; break; case 3: prize = "GOOD PRIZE WINNER - $5,000!!"; break; case 2: prize = "NICE PRIZE WINNER - $500!!"; break; case 1: prize = "WINNER - $5!!"; break; case 0: prize = "No matching numbers - better luck next time"; break; default: prize = "Something weird happened"; } System.out.println(prize); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM