3 Replies - 6971 Views - Last Post: 11 December 2009 - 04:15 PM Rate Topic: -----

#1 polska03  Icon User is offline

  • D.I.C Regular

Reputation: 5
  • View blog
  • Posts: 302
  • Joined: 28-November 09

lottery program help

Post icon  Posted 11 December 2009 - 03:34 PM

It compiles, but for some reason everytime I enter 5 numbers I always recieve that I got 0 match, why is this PLEASE HELP
/**
 * Lottery Application- Write a Lottery class that simulates a lottery. The
 * class should use the Random class (from the Java API) to generate a random
 * number in the range of 0 through 9 for each element in the array.
 * 
 * The class should also have a method that accepts an array of 5 integers that
 * represent a person's lottery picks. The method is to compare the
 * corresponding elements in the two arrays and return the number of digits that
 * match. For example, the following shows that lotteryNumbers array and the
 * user's array with sample numbers stored in each.
 * 
 * There are two matching digits (elements 2 and 4) lotteryNumbers array: (7, 4,
 * 9, 1, 3) user's array: (4, 2, 9, 7, 3)
 * 
 * In addition, the class should have a method that returns a copy of the
 * lotteryNumbers array.
 * 
 */

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

class Lottery {

	/**
	 * The lottery numbers.
	 */
	private int lotteryNumbers[];

	/**
	 * Default Constructor.
	 * 
	 * The class should use the Random class (from the Java API) to generate a
	 * random number in the range of 0 through 9 for each element in the array.
	 */
	public Lottery() {
		Random rand = new Random(System.currentTimeMillis());
		lotteryNumbers = new int[5];
		for (int i = 0; i < lotteryNumbers.length; i++) {
			lotteryNumbers[i] = Math.abs(rand.nextInt()) % 10;
		}
	}

	/**
	 * The class should also have a method that accepts an array of 5 integers
	 * that represent a person's lottery picks. The method is to compare the
	 * corresponding elements in the two arrays and return the number of digits
	 * that match.
	 */
	public int compareNumbers(int[] usersNumbers) {
		int match = 0;
		if (usersNumbers.length == lotteryNumbers.length) {
			for (int i = 0; i < lotteryNumbers.length; i++) {
				if (usersNumbers[i] == lotteryNumbers[i]) {
					match++;
				}
			}
		}
		return match;
	}

	/**
	 * In addition, the class should have a method that returns a copy of the
	 * lotteryNumbers array.
	 */
	public int[] getLotteryNumbers() {
		return lotteryNumbers;
	}
}

/**
 * Demonstrate the class in a program that asks the user to enter five numbers.
 * The program should display the number of digits that match the randomly
 * generate lottery numbers. If all of the digits match, display a message
 * proclaiming the user a grand prize winner.
 */
public class LotteryApplication {
	public static void main(String[] args) {
		Lottery lottery = new Lottery();
		int lotteryNumbersCount = lottery.getLotteryNumbers().length;

		System.out.println("Lottery Application\n");
		System.out.println("There are " + lotteryNumbersCount
				+ " secret numbers in range of 0 through 9. "
				+ "Try to guess them!!!\n");

		// Asks the user to enter five numbers.
		Scanner kb = new Scanner(System.in);
		int numbers[] = new int[lotteryNumbersCount];

		for (int i = 0; i < numbers.length; i++) {
			System.out.print(String.format("Enter Number %d: ", i + 1));
			numbers[i] = kb.nextInt();
		}

		// Display the number of digits that match the randomly generate
		// lottery numbers.

		int match = lottery.compareNumbers(numbers);

		if (match == lotteryNumbersCount) {

			// If all of the digits match, display a message proclaiming the
			// user a grand prize winner.
			System.out.println("\nWOHOO! ALL CORRECT! YOU WON THE BIG PRIZE!");

		} else {

			System.out.println("\nUh-oh! You hit " + match + " number(s).");

		}

	}
}



Is This A Good Question/Topic? 0
  • +

Replies To: lottery program help

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8346
  • View blog
  • Posts: 31,904
  • Joined: 06-March 08

Re: lottery program help

Posted 11 December 2009 - 04:03 PM

First instead of

lotteryNumbers[i] = Math.abs(rand.nextInt()) % 10;

do that much clearer and simpler

lotteryNumbers[i] = rand.nextInt(10);

Second in your match you just compare
number[0] with user[0]
number[1] with user[1]

you have to change your loop for something like this

   public int compareNumbers(int[] usersNumbers) {
		int match = 0;
		if (usersNumbers.length == lotteryNumbers.length) {
			for (int i = 0; i < lotteryNumbers.length; i++) {
				for(int j = 0; j < lotteryNumbers.length; i++) {
					if (usersNumbers[i] == lotteryNumbers[j]) {
						match++;
						break;
					}
				}
			}
		}
		return match;
	}


Was This Post Helpful? 0
  • +
  • -

#3 Paul-  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 61
  • View blog
  • Posts: 260
  • Joined: 11-December 09

Re: lottery program help

Posted 11 December 2009 - 04:10 PM

Seems to be working just fine. You can first print out the "lottery" variable to make sure you pick some matching numbers.
Was This Post Helpful? 0
  • +
  • -

#4 polska03  Icon User is offline

  • D.I.C Regular

Reputation: 5
  • View blog
  • Posts: 302
  • Joined: 28-November 09

Re: lottery program help

Posted 11 December 2009 - 04:15 PM

Thank you very much for the help
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1