1.11M Members

Need Help with lottery program

 
0
 

Hey everyone, I just need a little help with a lottery program I have to make or class. I have to generate a 7 digit number and then using that number compare it to a guess the user enters. I need to award prizes for the following conditions.

  1. if all numbers match $1000

  2. if all even place numbers match $500

  3. if all odd place numbers match $250

  4. First and last digits match $100

Im using math.Random() to generate the number im just having issues getting the specific place values and comparing them to the answer, for example
if the user enters the number 4875297 and the lottery generates 7845397. Since all the even place numbers match (8, 5, 9) the user would win $500, but how do I select the specific place value to compare

Thanks for any help

 
0
 

please dont ask us to do your homework. do as much of the code as you can then post on here with question about, errors you recieve, specific parts of the code your having trouble with ect.

 
1
 

I wasnt asking you to do my homework thanks, I only asked 1 question regarding a specific aspect of it. I didnt post my homework in full and say figure it out. All I wanted to know was how to select place values of a seven digit number. Infact I tried to be as clear as I could to avoid anyone thinking that I was trying to get someone to do it for me. All I need is a push in the right direction.

 
0
 

You will find it easier if instead of creating a single 7-digit number, you create a 7 element array of single digits. The you can access each element of the array (digit) by a simple array index.

 
0
 

We havent gone over arrays in class as of it. Is it possible to generate 7 different Math.random(); numbers and compare them that way?

 
0
 
Scanner scanner = new Scanner(System.in);
String userGuess;
System.out.println("Please enter a 7 digit number");
userGuess = scanner.next();

int randomNum1 = (int)(Math.random() * 9) + 1;
int randomNum2 = (int)(Math.random() * 9) + 1;
int randomNum3 = (int)(Math.random() * 9) + 1;
int randomNum4 = (int)(Math.random() * 9) + 1;
int randomNum5 = (int)(Math.random() * 9) + 1;
int randomNum6 = (int)(Math.random() * 9) + 1;
int randomNum7 = (int)(Math.random() * 9) + 1;

String winningOne = "" + randomNum1;
String winningTwo = "" + randomNum2;
String winningThree = "" + randomNum3;
String winningFour = "" + randomNum4;
String winningFive = "" + randomNum5;
String winningSix = "" + randomNum6;
String winningSeven = "" + randomNum7;
String winningNumbers = winningOne + winningTwo + winningThree + winningFour + winningFive + winningSix + 
  winningSeven;
if(winningNumbers == userGuess){
  System.out.println("Congrats");
  {
System.out.println(winningNumbers);

This is what I have so far. Just want to double check Im heading in the right direction

edited with my full program. the System.out.println() is there just so I could check if I was getting the right numbers

`

    Scanner scanner = new Scanner(System.in);
    String userGuess;
    System.out.println("Please enter a 7 digit number");
    userGuess = scanner.next();

    int randomNum1 = (int)(Math.random() * 9) + 1;
    int randomNum2 = (int)(Math.random() * 9) + 1;
    int randomNum3 = (int)(Math.random() * 9) + 1;
    int randomNum4 = (int)(Math.random() * 9) + 1;
    int randomNum5 = (int)(Math.random() * 9) + 1;
    int randomNum6 = (int)(Math.random() * 9) + 1;
    int randomNum7 = (int)(Math.random() * 9) + 1;

    String winningOne = "" + randomNum1;
    String winningTwo = "" + randomNum2;
    String winningThree = "" + randomNum3;
    String winningFour = "" + randomNum4;
    String winningFive = "" + randomNum5;
    String winningSix = "" + randomNum6;
    String winningSeven = "" + randomNum7;

    String winningNumbers = winningOne + winningTwo + winningThree + winningFour + winningFive + winningSix + 
      winningSeven;

    int userLength = userGuess.length();

    String evenWin = winningNumbers.substring(1, 2) + winningNumbers.substring(3, 4) + winningNumbers.substring(5, 6);

    String oddWin = winningNumbers.substring(0, 1) + winningNumbers.substring(2, 3) + winningNumbers.substring(4, 5) +
      winningNumbers.substring(6, 7);

    String firstLast = winningNumbers.substring(0, 1) + winningNumbers.substring(6, 7);

    if(userLength > 7){
      System.out.println("Please enter in only 7 numbers.");
    }
    else if(winningNumbers == userGuess){
      System.out.println("Congrats");
    }
    else if(userGuess == evenWin){
      System.out.println("Congrats");
    }
    else if(userGuess == oddWin){
      System.out.println("Congrats");
    }
    else if(userGuess == firstLast){
      System.out.println("Congrats");
    }

    System.out.println(winningNumbers);
    System.out.println(evenWin);
    System.out.println(oddWin);


  }

}

`

 
0
 

Wasnt able to match the odd, even, or first and last place numbers before but I think I figured it out

 public static void main(String[]args){

    Scanner scanner = new Scanner(System.in);
    String userGuess;
    System.out.println("Please enter a 7 digit number");
    userGuess = scanner.next();

    int userLength = userGuess.length();

    int randomNum1 = (int)(Math.random() * 9) + 1;
    int randomNum2 = (int)(Math.random() * 9) + 1;
    int randomNum3 = (int)(Math.random() * 9) + 1;
    int randomNum4 = (int)(Math.random() * 9) + 1;
    int randomNum5 = (int)(Math.random() * 9) + 1;
    int randomNum6 = (int)(Math.random() * 9) + 1;
    int randomNum7 = (int)(Math.random() * 9) + 1;

    String winningOne = "" + randomNum1;
    String winningTwo = "" + randomNum2;
    String winningThree = "" + randomNum3;
    String winningFour = "" + randomNum4;
    String winningFive = "" + randomNum5;
    String winningSix = "" + randomNum6;
    String winningSeven = "" + randomNum7;

    String winningNumbers = winningOne + winningTwo + winningThree + winningFour + winningFive + winningSix + 
      winningSeven;

    String userEven = userGuess.substring(1, 2) + userGuess.substring(3, 4) + userGuess.substring(5, 6);

    String userOdd = userGuess.substring(0, 1) + userGuess.substring(2, 3) + userGuess.substring(4, 5) 
      + userGuess.substring(6, 7);

    String userFirstLast = userGuess.substring(0, 1) + userGuess.substring(6, 7);

    String evenWin = winningNumbers.substring(1, 2) + winningNumbers.substring(3, 4) + winningNumbers.substring(5, 6);

    String oddWin = winningNumbers.substring(0, 1) + winningNumbers.substring(2, 3) + winningNumbers.substring(4, 5) +
      winningNumbers.substring(6, 7);

    String firstLast = winningNumbers.substring(0, 1) + winningNumbers.substring(6, 7);

    if(userLength > 7){
      System.out.println("Please enter in only 7 numbers.");
    }
    else if(userLength < 7){
      System.out.println("Please enter 7 numbers");
    }
    else if(winningNumbers.equals(userGuess)){
      System.out.println("Congrats you matched all the numbers! Your prize is $1000, spend it wisely!");
    }
    else if(evenWin.equals(userEven)){
      System.out.println("Congrats you matched all the even numbers! Your prize is $500");
    }
    else if(oddWin.equals(userOdd)){
      System.out.println("Congrats you matched all the odd numbers! Your prize is $250");
    }
    else if(firstLast.equals(userFirstLast)){
      System.out.println("Congrats you match the first and last number! Your prize is $100");
    }

    System.out.println(winningNumbers);
    System.out.println(evenWin);
    System.out.println(oddWin);
    System.out.println(firstLast);

  }

}
 
0
 

Sorry about that i read most of your post saw what appeared to be an asignment with no code and assumed you were, "asking us to do your homework."

I apoligize for my horrible mistake.

Now that i have read your full post as well as the others:

have you covered modulus, %, in your class?
if so we can make use of it to make a simple method to get any digit of a larger number.

ill give you some examples then see if you can work out a method to do what you want.

if we want to get the 100s place of x = 4536728; we can do (x/(10^2)) % 10 which equals 7.

So shift the hundreds place to the ones place by deviding by 100 and find the remainder of deviding by ten using modulus. This gives us the value of the ones place (the number that was in the hundreds place)

more examples:

get the 100,000s digit (x/(10^5)) % 10 equals 5

get the 10,000s digit (x/(10^4)) % 10 equals 3

hope this helps.

You
This article has been dead for over six months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article