Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

How do I make sure that only the special number can repeat not the first five nu

ID: 3626214 • Letter: H

Question

How do I make sure that only the special number can repeat not the first five numbers?

Here's my code:


import javax.swing.*;
import java.util.Random;
class Lottery {
private int LotteryNumbers[];
private int specialNum=0;

public Lottery() // constructor

{
Random randomNum = new Random(); // randon class

LotteryNumbers = new int[5];
for (int i = 0; i < LotteryNumbers.length; i++)
LotteryNumbers[i] = randomNum.nextInt(9);

specialNum=randomNum.nextInt(8);
}





public int CheckLotteryNum(int[] num) // check
{
int equalNum = 0;
if (num.length == LotteryNumbers.length)
for (int i = 0; i < LotteryNumbers.length; i++)
if (num[i] == LotteryNumbers[i])
equalNum++;
return equalNum;
}





public int[] getLotteryNumbers() // get
{
return LotteryNumbers;
}

public void setLotteryNumbers(int [] ln)
{
LotteryNumbers =ln;
}

public void setSpecialNum(int n)
{
specialNum=n;
}

public int getSpecialNum() // get
{
return specialNum;
}


public static void main(String[] args) { // main method


Lottery l = new Lottery();
int i;
int userSpecailNum=-1;
int lotteryChoice[] = new int[l.getLotteryNumbers().length];
int[] winningNum = l.getLotteryNumbers();
int equalNum = l.CheckLotteryNum(lotteryChoice);


for (int j = 0; j < lotteryChoice.length; j++){

lotteryChoice[j] = Integer.parseInt(JOptionPane.showInputDialog("Enter Number " + (j+1) + ":"));

while(lotteryChoice[j]>56||lotteryChoice[j]<=0)
{
lotteryChoice[j] = Integer.parseInt(JOptionPane.showInputDialog("Enter the Number again: " + (j+1) + ":"));
}
}
while(userSpecailNum<=0||userSpecailNum>46)
{
userSpecailNum=Integer.parseInt(JOptionPane.showInputDialog("Enter Specail Number:"));
}



if ( equalNum == l.getLotteryNumbers().length && userSpecailNum==l.getSpecialNum())
JOptionPane.showMessageDialog(null, "You Have Won!");
else
JOptionPane.showMessageDialog(null, "You've matched " + equalNum + " number(s)." + " However, the specail number ""
+ userSpecailNum + "" didn't match out of all six numbers you've entered!");

for(i = 0; i < l.getLotteryNumbers().length; i++)
System.out.print(lotteryChoice[i] + " ");

System.out.println();

JOptionPane.showMessageDialog(null,"The winning numbers are: ");

for(i = 0; i < l.getLotteryNumbers().length; i++)
JOptionPane.showMessageDialog(null,winningNum[i] + " "); // display the winning numbers
System.out.println(winningNum[i]); // display the winning numbers

if(userSpecailNum==l.getSpecialNum())
{
JOptionPane.showMessageDialog(null, "You got the mathing number: " +userSpecailNum);
}
else
{
JOptionPane.showMessageDialog(null, "The mathing number is not the same: " +l.getSpecialNum());
}
}
}

Explanation / Answer

please rate - thanks

misunderstood, thought the generated numbers couldn't repeat.


import javax.swing.*;
import java.util.Random;
class Lottery {
private int LotteryNumbers[];
private int specialNum=0;

public Lottery() // constructor

{
Random randomNum = new Random(); // randon class

LotteryNumbers = new int[5];
boolean []used=new boolean[10];
for(int i=0;i<5;i++)
     used[i]=false;
int num;
for (int i = 0; i < LotteryNumbers.length; i++)
    { num = randomNum.nextInt(9);
       while(used[num]==true)
            {num = randomNum.nextInt(9);
          }
        LotteryNumbers[i]=num;
        used[num]=true;
  
    }

specialNum=randomNum.nextInt(8);
}





public int CheckLotteryNum(int[] num) // check
{
int equalNum = 0;
if (num.length == LotteryNumbers.length)
for (int i = 0; i < LotteryNumbers.length; i++)
if (num[i] == LotteryNumbers[i])
equalNum++;
return equalNum;
}





public int[] getLotteryNumbers() // get
{
return LotteryNumbers;
}

public void setLotteryNumbers(int [] ln)
{
LotteryNumbers =ln;
}

public void setSpecialNum(int n)
{
specialNum=n;
}

public int getSpecialNum() // get
{
return specialNum;
}


public static void main(String[] args) { // main method


Lottery l = new Lottery();
int i;
int userSpecailNum=-1;
int lotteryChoice[] = new int[l.getLotteryNumbers().length];
int[] winningNum = l.getLotteryNumbers();
int equalNum = l.CheckLotteryNum(lotteryChoice);
boolean []used=new boolean[57];
for(i=0;i<57;i++)
     used[i]=false;


for (int j = 0; j < lotteryChoice.length; j++){

lotteryChoice[j] = Integer.parseInt(JOptionPane.showInputDialog("Enter Number " + (j+1) + ":"));

while(lotteryChoice[j]>56||lotteryChoice[j]<=0||used[lotteryChoice[j]])
{
lotteryChoice[j] = Integer.parseInt(JOptionPane.showInputDialog("Enter the Number again: " + (j+1) + ":"));
}
used[lotteryChoice[j]]=true;
}
while(userSpecailNum<=0||userSpecailNum>46)
{
userSpecailNum=Integer.parseInt(JOptionPane.showInputDialog("Enter Specail Number:"));
}



if ( equalNum == l.getLotteryNumbers().length && userSpecailNum==l.getSpecialNum())
JOptionPane.showMessageDialog(null, "You Have Won!");
else
JOptionPane.showMessageDialog(null, "You've matched " + equalNum + " number(s)." + " However, the specail number ""
+ userSpecailNum + "" didn't match out of all six numbers you've entered!");

for(i = 0; i < l.getLotteryNumbers().length; i++)
System.out.print(lotteryChoice[i] + " ");

System.out.println();

JOptionPane.showMessageDialog(null,"The winning numbers are: ");

for(i = 0; i < l.getLotteryNumbers().length; i++)
JOptionPane.showMessageDialog(null,winningNum[i] + " "); // display the winning numbers
System.out.println(winningNum[i]); // display the winning numbers

if(userSpecailNum==l.getSpecialNum())
{
JOptionPane.showMessageDialog(null, "You got the mathing number: " +userSpecailNum);
}
else
{
JOptionPane.showMessageDialog(null, "The mathing number is not the same: " +l.getSpecialNum());
}
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote