Reputation: 17
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!
Upvotes: 2
Views: 5231
Reputation: 11
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.
Upvotes: 0
Reputation: 3283
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);
Upvotes: 5