Editted answer as I read the question wrong first off, apologies!
Okay, let's break this up. So first off, how do we check if two numbers are consecutive? If they come after one another. This is correct, but how do we express this via programming?
It's actually pretty simple, two numbers are consecutive if the difference between them is equal to 1. So therefore to check if two numbers are consecutive, just subtract them and see if the result is 1 (or -1 depending on the order).
Now, the full solution to this problem is a little bit more complicated. We need to find all numbers in your generated lotto numbers that are consecutive. So, in order to break this up nicely I suggest putting this part into a separate method, called something like getConsecutiveNumbers(int [] lottoNumbers)
. This way, you have the basic setup done already like so:
// Now returns int array instead of String
public static int [] getLottoTicket(){
Random lottoNumbers = new Random(); //random object
int [] ticket = new int[6]; //6 lotto numbers
int counterr = 0;
int a;
for( int counter = 0; counter < ticket.length; ++counter ){
ticket[counter] = (1 + lottoNumbers.nextInt(45) ); //numbers between 1-45
}
Arrays.sort( ticket ); //array class .. sorts array
return ticket;
}
public static void main( String [] args ){
int a, d, n=1, t, b;
int totalConsecutives = 0;
for(int i=1; i<1000000; i++){
int counter = 0;
int [] lottoTicket = getLottoTicket();
// Keep a count of consecutives
totalConsecutives += getConsecutiveNumbers( lottoTicket );
}
System.out.println("Number of consecutives: " + totalConsecutives );
}
Now for the main part. What goes into this magic getConsecutiveNumbers()
method? Let's find out.
First off let's write the signature/makeup of the method which has to be put outside of the main method, as I said it needs to take in an integer array, but also it needs to return the number of consecutive numbers, as the method name suggests, if the program asks "how many numbers are consecutive?" you should respond accordingly! The method will start off like this:
public static int getConsecutiveNumbers(int [] lottoNumbers) {
// How many
}
Okay. Because of the way your program is setup so far we can assume that the lotto numbers given to us here are sorted already. This is good, it means we just have to go through the array and check one-by-one if the numbers are consecutive. How will we do this you say? Let's break this up also.
We need to go through the array to check each element, so that will definitely require some sort of loop. I see you've already used a for loop so let's go for that.
for(int i = 0; i < lottoNumbers.length; i++) {
// Going through each number here
}
Now here's the intelligent part. For each iteration of this loop we need to check if element lottoNumbers[i]
is consecutive to lottoNumbers[i-1]
. You see? In english this could mean the 2nd element is consecutive to the 1st element and then the 3rd element is consecutive to the 2nd element and so on until i
reaches the end of the array.
BUT there's one problem. At the start of this loop i
is zero (which is what we want), however in this case, lottoNumbers[i-1]
will give an IndexOutOfBoundsException
, an array has no element at index -1! So to avoid this problem we need to start the search at one instead of zero.
public static int getConsecutiveNumbers(int [] lottoNumbers) {
// i starts at 1 not 0
for(int i = 1; i < lottoNumbers.length; i++) {
// Going through each number here
}
}
So, how do we finish this off? Let's think of it this way. While we're going through the array, every time a number is consecutive, just add it to a counter and at the end, return this counter.
Here's the final solution:
public static int getConsecutiveNumbers(int [] lottoNumbers) {
int consecutives = 0;
// i starts at 1 not 0
for(int i = 1; i < lottoNumbers.length; i++) {
// Going through each number here
if(lottoNumbers[i] - lottoNumbers[i-1] == 1)
consecutives++; // Numbers were consecutive
}
// Return the number of consecutives
return consecutives;
}
I hope this helps you understand the problem and benefits you more than just a straight up answer! Any questions please ask away :)