Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: McGeeLotto?????

  1. #1
    Junior Member
    Join Date
    Nov 2009
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Question McGeeLotto?????

    how do you do this program? I have this much and now i cant decide what to do next.

    Lotto.java
    For one dollar, you get to play the Lotto. You will enter 6 numbers. The six numbers must be between 1 and 60. The computer will generate 6 random numbers between 1 and 60. For every number you get right, you get a bigger jackpot. Print out the resulting jackpot.
    0 number right = an empty feeling in your stomach
    1 number right = a quarter!
    2 numbers right = 35 cents off your next purchase (when you spend $5)
    3 numbers right = one dollar
    4 numbers right = $100 dollars
    5 numbers right = $5000 dollars
    6 numbers right = $1,000,000 dollars
    Warning: Make sure you generate 6 different random numbers.

    Is this correct? I ran it but I want to make sure it's correct from the point of view of experienced programmers.


    import java.util.Scanner;
    import java.util.Random;
    public class Lotto
    {
    	public static void main (String[] args) 
    	{
    	Lotto example= new Lotto();
    	example.getRandomNumber();
    	}
     
    	public void getRandomNumber();
    	{
    	int min=1;
    	int max=60;
    	Random randGenerator= new Random();
    	int randomNum=randGenerator.nextInt(max-min+1) + min;
    	int randomNum2=randGenerator.nextInt(max-min+1) + min;
    	int randomNum3=randGenerator.nextInt(max-min+1) + min;
    	int randomNum4=randGenerator.nextInt(max-min+1) + min;
    	int randomNum5=randGenerator.nextInt(max-min+1) + min;
    	int randomNum6=randGenerator.nextInt(max-min+1) + min;
    	int randomNum7=randGenerator.nextInt(max-min+1) + min;
    	System.out.println ("Random number generated is:" +  randomNum);
    	}
    											//ask user for guess//
    	Scanner input=new Scanner(System.in);
    	System.out.println ("Please enter a number between one and sixty");
    	int num1=input.nextInt();
    	System.out.println ("Please enter a number between one and sixty");
    	int num2=input.nextInt();
    	System.out.println ("Please enter a number between one and sixty");
    	int num3=input.nextInt();
    	System.out.println ("Please enter a number between one and sixty");
    	int num4=input.nextInt();
    	System.out.println ("Please enter a number between one and sixty");
    	int num5=input.nextInt();
    	System.out.println ("Please enter a number between one and sixty");
    	int num6=input.nextInt();
    	System.out.println ("Please enter a number between one and sixty");
    	int num7=input.nextInt();
     
     
    														// the string arrays//
     
     
     
    		String[]jackpot={{"empty stomach","quarter", "35 cent coupon when you spend $5", "one dollar", "hundred dollars", "five thousand dollars", "one million dollars"}};
    		if(randomNum=num1);
    		{
    			System.out.println ("There is an empty feeling in your stomach.");
    		}
    		else if(randomNum2=num2)
    		{
    			System.out.println ("You get a quarter!");
    		}
    				else if(randomNum3=num3)
    				{
    				System.out.println ("You receive a coupon with thirty-five cents off your next purchase when you spend five dollars.");
    				}
    					else if(randomNum4=num4)
    					{
    						System.out.println ("You get a dollar.");
    					}
    						else if(randomNum5=num5)
    						{
    							System.out.println ("You get hundred dollars!");
    						}
    							else if(randomNum6=num6)
    							{
    								System.out.println ("You get five thousand dollars!");
    							}
    							else if(randomNum7=num7)
    							{
    								System.out.println ("You've just won a million dollars!!!!!!!!!!");
    							}
     
    			}		
     
    		}
    Thank you.


    Sincerely,
    Ian
    Last edited by helloworld922; November 5th, 2009 at 08:17 PM. Reason: please, please put [code] tags around your code!

  2. #2
    Member
    Join Date
    Jul 2009
    Posts
    31
    Thanks
    3
    Thanked 6 Times in 5 Posts

    Default Re: McGeeLotto?????

    that code will not work, unless the user actually picks the numbers in the EXACT order that it is randomized.

    and also, most of the code is not even inside of a method, its just ...there.

    your method getRandomNumber is good, except it could be written cleaner.

    i will post correction


    Compiles, not tested.

    import java.util.Scanner;
    import java.util.Random;
    public class Lotto
    {
    int[] randomNum;
    int[] userGuess;
     
    public static void main (String[] args)
    {
    getRandomNumbers();
    getUserNumbers();
    announceReward();
    }
     
    public void getRandomNumbers()
    {
    int min=1;
    int max=60;
    Random randGenerator= new Random();
    randomNum = new int[7];
     
    //we create 7 random numbers and store them inside an array
    for(int i=0;i<randomNum.length;++i)
    {
        randomNum[i] = randGenerator.nextInt(max-min+1) + min;
    }
     
    }
     
    //ask user for guess//
    //we can loop this around 7 times since it is identical code...AGAIN storing numbers in an array.
    //Another correction, it is placed inside its own method. (getUserNumbers)
    public void getUserNumbers()
    {
    int[] userGuess = new int[7];
    Scanner input=new Scanner(System.in);
    for(int j=0;j<7;++j)
    {
        System.out.println(j+") Please enter a number between one and sixty:");
        //preview:  1) Please enter a number between one and sixty:
        userGuess[j]=input.nextInt();
    }
     
    }
    // the string arrays//
     
     
     
     
    //Code previously written did not work because it only checked
    // the First guess with the First random number, and the 2nd guess with the 2nd random...it did not check every possible case... for example the 1st guess with 4th random.
    private boolean checkSingleNumberForWinner(int guess)
    {
    //This method checks a single number to see if it is one of the guesses, if it is..the method returns TRUE.
    //if not, it returns false
    for(int i=0;i<randomNum.length;++i)
    {
        if(guess == randomNum[i])
        return true;
    }
    return false;
    }
     
     
    public void announceReward()
    {
     
    //correction from original code, array only needs 1 set of {}.
    String[] jackpot={"There is an empty feeling in your stomach.","You get a quarter!", "You receive a coupon with thirty-five cents off your next purchase when you spend five dollars.",
     "You get a dollar.", "You get hundred dollars!", "You get five thousand dollars!", "You\'ve just won a million dollars!!!!!!!!!!"};
    if(userGuess.length > jackpot.length)
    return;
    //the above line is to make sure there is less guesses than there is prizes.
     
     
    //this for loop will check all our guesses for winners, and add them up.
    int wins=0;
    for(int i=0;i<userGuess.length;++i)
    {
    if(checkSingleNumberForWinner(userGuess[i]))
    ++wins;
    }
    System.out.println(jackpot[wins]);
    }
     
    }
    Last edited by rsala004; November 5th, 2009 at 07:30 PM.

  3. The Following User Says Thank You to rsala004 For This Useful Post:

    iank (November 5th, 2009)