2

I am writing a program for class. It is a lottery game. This is the question given:

Write a java program. The program should have a method named lotteryNumber() that should accept two integers, maximum and minimum numbers and should randomly generate and return a number between these two numbers (both inclusive).

Also, provide a method called checkWinner() that should accept two arrays (one for lottery numbers and one for user’s numbers) and check if they are the same (same numbers and sequence). If they are same, it should return true, otherwise it should return false. Please use your own logic to check equality using equality operator. Use of prebuilt functions like Arrays.equals() is not allowed!

In main method, randomly generate 4 numbers, using lotteryNumber() method, between 0 – 10 (both inclusive). Then ask the user to input from keyboard 4 numbers between 0 and 10. Then using checkWinner() method check and display if the user is a winner or not.

My problem is with the lotteryNumber method. How am I supposed to return only one number and not use arrays and still generate 4 numbers? Keep in mind that I am new to arrays and java in general and that I need for the lottery number to be within an array in the main method so that I can compare them to the user input. Or at least that is what I got out of the question

This is what I have for now. Sorry that it is such a mess, I've been trying different things in hopes to figure it out

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

public class Assignment04 {
public static void main (String [] args){

    Scanner input = new Scanner (System.in); 

    Random r = new Random (); 

    int [] user= new int [4]; 
    //int [] rand= new int [4]; 

    int rand1= r.nextInt(10); 
    int rand2= r.nextInt(10); 
    int rand3 = lotteryNumber(rand1,rand2);

    /*for (int i=0; i<rand.length; i++){
        rand [i] = r.nextInt(11); 
    }

    for (int j=0; j<rand.length; j++){
        System.out.print(rand[j]+",");
    }*/

    System.out.println("");
    System.out.print("Enter number 1 between (1-10)= ");
    user[0] = input.nextInt();
    System.out.print("Enter number 2 between (1-10)= ");
    user[1] = input.nextInt(); 
    System.out.print("Enter number 3 between (1-10)= ");
    user[2]= input.nextInt();
    System.out.print("Enter number 4 between (1-10)= ");
    user[3] = input.nextInt();



    System.out.printf("Your numbers- %d, %d, %d, %d \n",user[0],user[1],user[2],user[3]);

    if (checkNumber(user,rand)){
        System.out.println("Winner!");
    }

    else System.out.println("Better luck next time!");

}


public static int lotteryNumber(int max, int min){

    Random r = new Random (); 
    max = 0; 
    min= 100; 
    int n= 1+r.nextInt(10) ; 

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

        n = 1+r.nextInt(10);
        if (n > max) max= n;
        else if (n < min) min = n ; 
        else if (n<max && n>min) n = r.nextInt(10);

    }

    return n; 
}

public static boolean checkNumber(int [] lottery, int[] input){

    boolean a ; 
    int b=0; 

    for (int i=0; i<lottery.length; i++){
        if (lottery[i]==input[i]) b++; 
    }

    if (b==lottery.length) a = true;
    else a = false; 

    return a; 
}

}

Any help would be really appreciated!

Mary
  • 17
  • 1
  • 4
  • Have you tried looping over the lotteryNumber method? – nietsnegttiw Nov 03 '15 at 20:17
  • No I haven't. We didn't talk about that in class so I don't know if my professor would be okay with me using it. – Mary Nov 03 '15 at 20:20
  • A couple of pointers for you: (1) to get a random number in a range in Java, use `n = r.nextInt(maxValue-minValue+1)+minValue`. (2) In your `checkNumber` method, you simply need a single match to fail, meaning you can break out of the loop early (with return false inside the `for(;;)`) when the number at index `i` doesn't match. Try it and see if that helps. – kha Nov 03 '15 at 20:21
  • Oh and the best pointer and your best friend as a future programmer: `google` :). Just google `how to get a random number in a range in java` and select any of the millions of results :). Same with learning how to loop over arrays and how to populate them correctly. – kha Nov 03 '15 at 20:23
  • Alright. Sounds good. Thank you!! – Mary Nov 03 '15 at 20:29

2 Answers2

5

Since this is a homework assignment I'm going to avoid giving you the code, but I'll help with the concept and you can work on making it a reality in Java.

You would simply create an array in the main method, and then make the lotteryNumber() method return a single number. Then fill the array in the main method with random numbers you get from the lotteryNumber() method.

Something like this:

Main(){
    array[] numbers;
    loop 4 times{
        numbers[index] = lotteryNumber(0, 10);
    }
}
public int lotteryNumber(int min, int max){
    return (random number between min and max);
}

EDIT

Since you mentioned in a comment you haven't yet gone over looping, you can still just manually put in the random numbers:

numbers[0] = lotteryNumber(0, 10);
numbers[1] = lotteryNumber(0, 10);
numbers[2] = lotteryNumber(0, 10);
numbers[3] = lotteryNumber(0, 10);
leigero
  • 3,233
  • 12
  • 42
  • 63
  • 1
    Although you actually have a for loop in your code already, so I'm not sure why you can't just use one here too -.- – leigero Nov 03 '15 at 20:23
  • I tried what you said and it worked perfectly! Thank you so much. Also, I meant looping over methods specifically. We did study looping in general. – Mary Nov 03 '15 at 20:26
  • @Ariana Well you can do anything you want inside a loop, a loop can loop over methods just the same as it can loop over single lines of code. Loop away, they're everywhere. – leigero Nov 03 '15 at 20:28
  • Haha, alright then. Will do in the future. Thanks again! – Mary Nov 03 '15 at 20:30
0

You can convert a set of numbers to a string, then the string into a single number.

Java - Convert integer to string

How to convert a String to an int in Java?

you make it sound like your having a user enter a 4-8 digit number, 1-2 digits at a time. Then randomly generating a 4-8 digit number, 1-2 digits at a time, looking at them in order of generation/inputed, and seeing if they are the same.

however you will have an issue with this method if you try to combine then compare since they are not all the same number of digits. Consider for example: Random number of 10,1,0,0 and a generated set of numbers of 1,0,10,0 combining them they both would result in 10100, which might be a problem depending on how you define equality.

You probably want to be looking at a sequence of 4, 1-2 digit numbers by your problem definition. Your equality method should be as simple as checking if the numbers in the same position in both arrays are all equal. You just need to return if someone is a winner, a Boolean, not a 4-8 digit number.

Community
  • 1
  • 1
casper
  • 11
  • 5