
Andy C. answered 09/18/17
Tutor
4.9
(27)
Math/Physics Tutor
class Lottery
{
public static final int MAX_NUM=50;
public static final int MIN_NUM=1;
public static final int NUM_PICKS = 6;
private boolean [] numsPicked; // flags which numbers have been picked; true means the number has been picked
Lottery()
{
numsPicked = new boolean[MAX_NUM];
for (int iLoop=0; iLoop<MAX_NUM; iLoop++)
{
numsPicked[iLoop]=false;
}
}
// generates NUM_PICKS random numbers between MIN and MAX and stores them in the array
public void GO()
{
for (int iLoop=0; iLoop<NUM_PICKS; iLoop++) // generates NUM_PICKS random numbers
{
int iNum=0;
do // generates a random number between MIN and MAX until it is not in the array
{
iNum = (int) (Math.floor( ( Math.random() * ( MAX_NUM - MIN_NUM ) + MIN_NUM) ) ) ; // random number generated here
} while ( numsPicked[iNum] == true); // same number cannot be generated twice;
numsPicked[iNum]=true; // marks the number as picked
}
}
// displays the numbers marked in the array and returns an array of those integers
public int[] GetNums()
{
int [] nums = new int[NUM_PICKS];
int numIndex=0;
for (int iLoop=0; iLoop<MAX_NUM; iLoop++)
{
if (numsPicked[iLoop])
{
//System.out.print( (iLoop+1) + " ");
nums[numIndex++]= iLoop+1;
}
}
return(nums);
}
public static void main(String args[])
{
Lottery myLottery = new Lottery();
myLottery.GO();
int [] nums = myLottery.GetNums();
System.out.print(" The Winning Numbers are : ");
for (int iLoop=0; iLoop<myLottery.NUM_PICKS; iLoop++)
{
System.out.print(nums[iLoop] + " ");
}
System.out.println("");
}
}
{
public static final int MAX_NUM=50;
public static final int MIN_NUM=1;
public static final int NUM_PICKS = 6;
private boolean [] numsPicked; // flags which numbers have been picked; true means the number has been picked
Lottery()
{
numsPicked = new boolean[MAX_NUM];
for (int iLoop=0; iLoop<MAX_NUM; iLoop++)
{
numsPicked[iLoop]=false;
}
}
// generates NUM_PICKS random numbers between MIN and MAX and stores them in the array
public void GO()
{
for (int iLoop=0; iLoop<NUM_PICKS; iLoop++) // generates NUM_PICKS random numbers
{
int iNum=0;
do // generates a random number between MIN and MAX until it is not in the array
{
iNum = (int) (Math.floor( ( Math.random() * ( MAX_NUM - MIN_NUM ) + MIN_NUM) ) ) ; // random number generated here
} while ( numsPicked[iNum] == true); // same number cannot be generated twice;
numsPicked[iNum]=true; // marks the number as picked
}
}
// displays the numbers marked in the array and returns an array of those integers
public int[] GetNums()
{
int [] nums = new int[NUM_PICKS];
int numIndex=0;
for (int iLoop=0; iLoop<MAX_NUM; iLoop++)
{
if (numsPicked[iLoop])
{
//System.out.print( (iLoop+1) + " ");
nums[numIndex++]= iLoop+1;
}
}
return(nums);
}
public static void main(String args[])
{
Lottery myLottery = new Lottery();
myLottery.GO();
int [] nums = myLottery.GetNums();
System.out.print(" The Winning Numbers are : ");
for (int iLoop=0; iLoop<myLottery.NUM_PICKS; iLoop++)
{
System.out.print(nums[iLoop] + " ");
}
System.out.println("");
}
}