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!
Aucun commentaire:
Enregistrer un commentaire