Question
Java lottery

Vrite a java program which gives 3 rows of lottery at random and writes out the result using JOptionPane.showMessageDialog0;
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Note: Could you plz go through this code and let me know if u need any changes in this.Thank You
_________________

// Lottery.java

import java.util.Random;

import javax.swing.JOptionPane;

public class Lottery {

   public static void main(String[] args) {
   int lotteryTic[][]=new int[3][7];

       geneateLotteryNos(lotteryTic);  
   displayLotteryTicket(lotteryTic);

   }

   private static void displayLotteryTicket(int[][] lotteryTic) {
       String str="";
for(int i=0;i<lotteryTic.length;i++)
{
   str+="Row "+(i+1)+": [";
   for(int j=0;j<lotteryTic[0].length;j++)
   {
str+=lotteryTic[i][j];
if(j!=lotteryTic[0].length-1)
{
   str+=",";
}

  
   }
   str+="]\n";
}

// Displaying the output
JOptionPane.showMessageDialog(null,str);
   }

   private static void geneateLotteryNos(int lotteryTic[][]) {
       //Creating a random Class object
       Random r = new Random();
       int num;
       for(int i=0;i<lotteryTic.length;i++)
       {
           for(int j=0;j<lotteryTic[0].length;)
           {
               num=r.nextInt((50 - 1) + 1) + 1;
               boolean b=checkDuplicate(lotteryTic,num,i);
               if(b)
               {
                   lotteryTic[i][j]=num;
                   j++;
               }
           }
       }
      
   }

   private static boolean checkDuplicate(int[][] lt,int num,int row) {
       for(int i=0;i<lt[0].length;i++)
       {
           if(lt[row][i]==num)
               return false;
       }
       return true;
   }

}
_______________________________

Output:

Message Row 1: [25,4,27,22,41,14,46] Row 2: [50,11,12,30,41,29,5] Row 3: [8,46,22,18,30,11,20] OK

_________________________________Thank You

Add a comment
Know the answer?
Add Answer to:
Java lottery Vrite a java program which gives 3 rows of lottery at random and writes...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In Java Please Create A Program For The Following; Please Note: This program should be able...

    In Java Please Create A Program For The Following; Please Note: This program should be able accept changes to the values of constants ROWS and COLS when testing the codes. Switch2DRows Given a 2D array with ROWS rows and COLS columns of random numbers 0-9, re-order the rows such that the row with the highest row sum is switched with the first row. You can assume that 2D arrau represents a rectangular matrix (i.e. it is not ragged). Sample run:...

  • in java / You will: // 1- create a square 2 dimensional array of random size...

    in java / You will: // 1- create a square 2 dimensional array of random size (less than 11) // 2- fill the array with random integers from 1 to the size limit // 3- print the array // 4- prompt to see if the user wants to do another //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ // 1- The square can use the same random value for x and y. Don't allow 0 size arrays. // 2- Maybe a nested set of for loops? to...

  • Using Java. Write a program that creates a “triangular” two-dimensional array A of 10 rows. The...

    Using Java. Write a program that creates a “triangular” two-dimensional array A of 10 rows. The first row has length 1, the second row has length 2, the third row has length 3, and so on. Then initialize the array using nested for loops so that the value of A[i][j] is i+j. Finally, print out the array in a nice triangular form.

  • 3. (20 points) Write a program that writes to output 1000 random 'words', separated by spaces...

    3. (20 points) Write a program that writes to output 1000 random 'words', separated by spaces (word simply means a sequence of alphabetical letters, not necessarily an actual word that can be found in the dictionary). The 'words' you write should be randomly generated sequences of lowercase letters, with random lengths between 3 and 9 characters. Your program must save the word as a string and then print it out using the percent-s format in printf.

  • How to write a Java program that reads the file "input.txt" and writes all even values...

    How to write a Java program that reads the file "input.txt" and writes all even values from this file into a new file called "output.txt." Sample input 10 12 1 3 5 34 2 5 7 9 44

  • Java program for an Airplane Seating Reservation.

    Need help with this, please!Write a java program that can be used to assign seats for a commercial airplane. The airplane has 13 rows, with 6 seats in each row. Rows 1 and 2 are first class, rows3 to 7 are business class, and rows 8 to 13 are economy class. Your program prompts the user to enter the following information:- Ticket type (first class or economy class)- For economy class, the smoking or non-smoking section- Desired seatOutput the seating...

  • Write a java program that simulates the rolling of four dice. The program should use random...

    Write a java program that simulates the rolling of four dice. The program should use random number generator to roll dice. The sum of the four values should then be calculated. Note that each die can show an integer value from 1 to 6, so the sum of the four values will vary from 4 to 24, with 14 being the most frequent sum and 4 and 24 being the least frequent sums. Your program should roll the dice 12,960...

  • Write a java program that will read the values for 3 matrices A, B, and C...

    Write a java program that will read the values for 3 matrices A, B, and C and store the result of their summation in matrix D. You need to use a method to read matrix values. Use another method to add the three 3x3 matrices (each is a 2 dimensional array with 3 rows and 3 columns). Finally, use a third method to print the value of the summation (matrix D). Write a test program that will cal the three...

  • Write a Java program which implements both the while and for loop, as needed, to draw...

    Write a Java program which implements both the while and for loop, as needed, to draw the illustrations given below. Shape 1 123456789 12345678 1234567 123456 12345 1234 123 12 1 Shape 2 ************* ************* ************* ************* ************* Shape 3 ============ * * * * * * * * * * ============ Hint: To draw the shapes above, you can use nested for loops to accomplish the task. The outer for loop iterates on each row, and the inner for...

  • 1.Write a Java program that computes the average of the values of a row in a...

    1.Write a Java program that computes the average of the values of a row in a twodimensional array. You should create a method with the following header: public static double averageRow(double[][] array, int row) Write a test program that reads a matrix from the user and a row index to calculate the average on. Print the result. 2.A square matrix is said to be an upper triangular matrix if the value of the cell is 0 when row > column....

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT