Пытаясь найти вероятность того, что в розыгрыше лотереи не будет никаких последовательных номеров, можно определить, как проверить, не совпадают ли цифры друг с другом, например 8,9 или 15,14. РЕДАКТИРОВАТЬ. Я изменил код, поэтому теперь он помещает значения в массив int для сортировки и сравнения чисел, но теперь он просто выводит ноль каждый раз.
import java.util.Random;
public class Lotto {
Random rangen = new Random();
private int result;
public Lotto()
{
result = spin();
}
public int getSpin()
{
return spin();
}
private int spin()
{
return rangen.nextInt(45)+1;
}
} // this is my spin class with constructors
import java.util.Arrays;
public class con2
{
public static void main(String args[])
{
int num = 100;
int count = 0;
Lotto[] myarray = new Lotto[5]; //an array to hold the lotto spins
int[] numarray = new int[5];
for(int i=0; i<myarray.length; i++)//loop to go trough array
{
myarray[i] = new Lotto();//fill array with balls or spins
numarray[i] = myarray[i].getSpin();//fill int array with value of lotto spin
//Arrays.sort(myarray);//sort the array
}
Arrays.sort(numarray);//sort the number array
for(int i =0; i<num; i++)//monte carlo loop
{
for(int z=0;z<numarray.length*2;z++) //loop to go trough the array
{
if(numarray[z] == numarray[z+1])
{
count++;//here i want to add 1 to count if number is one larger than position at z
}
else if(numarray[z] == numarray[z-1])
{
count++;//here i want to add 1 to count if number is one less than position at z
}
else
{
break;//if numbers are the same break out of loop
}
}
}
System.out.println(count/num);
}
}
Метод спина должен избегать множественного появления. Сначала нужно изменить его. Вы можете использовать специально созданные коллекции.