Display lottery winners - java

I created a lottery program to randomly generate 3 numbers between 0-9 and then randomly generate the 3 winning numbers. I need help on how to make the program display the winners (if there are some) and display the number of how many won the lottery.
So something like:
Winners:
Person1
person5
Number of winners: 2
Here is my program
import java.util.Random;
public class TwoDArray
{
public static void main(String[] args)
{
int[][] table = new int[50][3];
int[][] win = new int[1][3];
Random rand = new Random();
int i = 1;
// Load the table with values
for (int row=0; row < table.length; row++)
for (int col=0; col < table[row].length; col++)
table[row][col] = rand.nextInt(7-0 +1)+0 + col;
// Load the winning Values
for (int row=0; row < win.length; row++)
for(int col=0; col < win[row].length; col++)
win[row][col] = rand.nextInt(7-0 +1)+0 + col;
// Print the table of People
for (int row=0; row < table.length; row++)
{
System.out.print("Person" + i++ +":\t");
for (int col=0; col < table[row].length; col++)
System.out.print(table[row][col] + "\t");
System.out.println();
}
//Print the Winning Numbers
for (int row=0; row < win.length; row++)
{
System.out.print("\nThe winning numbers are:\t");
for(int col=0; col < win[row].length; col++)
System.out.print(win[row][col] + "\t");
System.out.println();
}
}
}

You want another for loop. Something like:
int counter = 0;
for (int i =0; i < table.length; i++){
if (table[i][0] == win[0][0] && table[i][1] == win[0][1] && table[i][2] == win[0][2])
{
counter++;
System.out.println("Person " + i);
}
}
System.out.println("There were " + counter + " winners.");

Related

Trying to input values into a 2d array and print it, code not running

Code not running??
import java.util.Scanner;
public class Array2dNightPractice
{
int[][] studentmarks;
studentmarks = new int[3][3];
Scanner kb = new Scanner(System.in);
System.out.println("Enter 9 integers");
for(int row = 0;row<3;row++){
for(int col=0;col<3;col++){
studentmarks[row][col] = kb.nextInt();
}
}
for(int row = 0; row < 3; row++) {
for(int col = 0; col < 4; col++) {
System.out.print(studentmarks[row][col] + " ");
}
System.out.println();
}
}
you're getting IndexOutOfBoundsException because of this --> col < 4.
for(int col = 0; col < 4; col++)
change to this:
for(int col = 0; col < studentmarks[row].length; col++)
side note - make use of the length property to prevent errors such as the one you've just encountered.
full solution:
for(int row = 0; row < studentmarks.length; row++) {
for(int col = 0; col < studentmarks[row].length; col++) {
System.out.print(studentmarks[row][col] + " ");
}
System.out.println();
}
the error comes from exceeding the bound of the array studentMarks in the second for loops at the number 4 it should be 3
A good habit to develop is to create constant variables as required with the size of each of the rows and columns, so you will not fall in such mistake.
Something like this:
import java.util.Scanner;
public class Array2dNightPractice{
public static void main(String[] agrs){
final int ROW =3, COL=3; // constant variable to determine the size of the 2d array
int[][] studentMarks = new int[ROW][COL];
Scanner in = new Scanner(System.in);
System.out.println("Enter 9 integers");
for(int row = 0;row<ROW;row++){ //note how I don't need to memorize and rewrite the numbers every time
for(int col=0;col<COL;col++){
studentMarks[row][col] = in.nextInt();
}
}
// print the marks as matrix
for(int row =0; row <ROW; row++) {
for(int col =0; col <COL; col++) {
System.out.print(studentMarks[row][col] + " ");
}
System.out.println();
}
}
}

Can not figure out how to increment a two dimensional array

So, basically what I want to do with my code is make it so that a user can enter in a value, and then the program will add whatever value that would be to ALL the values in the array
//my code
{
//creates array called table
int[][] table = new int [10][10];
//load the table with values
for (int row=0; row < table.length; row++)
for (int col=0; col < table[row].length; col++)
table[row][col] = row * 10 + col;
//Print the table
for (int row=0; row < table.length; row++)
{
for (int col=0; col < table[row].length; col++)
System.out.print (table[row][col] + "\t");
System.out.println();
int incr;
Scanner scan = new Scanner(System.in);
System.out.println("");
System.out.println("What do you want to increment by?");
incr = scan.nextInt();
for (int row=0; row < table.length; row++)
{
for (int col=0; col < table[row].length; col++)
System.out.print (table[row][col] + "\t");
System.out.println();
}
}
That is my code so far, and I'm not sure where to go from here. If someone could help me figure this out, that would be great.
inside the loop add this line of code:
table[row][col] += incr;

java performing array calculation [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
The code below will print two square matrices and I need them to perform multiplication between the two matrices but i cant seem to get that part working. I put a comment right before that block of code where the problem is. But for now all it prints is zeros. Iv been looking online at a lot of sites but cant seem to get mine to work.
import java.util.*;
import java.math.*;
public class Main {
public static void main(String[] args) {
//create the grid
final int rowWidth = 9;
final int colHeight = 9;
Random rand = new Random();
int [][] board = new int [rowWidth][colHeight];
//fill the grid
for (int row = 0; row < board.length; row++) {
for (int col = 0; col < board[row].length; col++) {
board[row][col] = rand.nextInt(10);
}
}
//display output
for(int i = 0; i < board.length; i++) {
for(int j = 0; j < board[i].length; j++) {
System.out.print(board[i][j] + " ");
//System.out.println();
}
System.out.println();
}
System.out.println();
int [][] board2 = new int [rowWidth][colHeight];
//fill the grid
for (int row2 = 0; row2 < board2.length; row2++) {
for (int col2 = 0; col2 < board[row2].length; col2++) {
board[row2][col2] = rand.nextInt(10);
}
}
//display output
for(int m = 0; m < board2.length; m++) {
for(int n = 0; n < board[m].length; n++) {
System.out.print(board[m][n] + " ");
}
System.out.println();
}
//error is somewhere here
int[][] calculationMultiplication = new int[rowWidth][colHeight];
for (int l = 0; l < rowWidth; l++) {
for (int t = 0; t < colHeight; t++) {
for (int z = 0; z < rowWidth; z++) {
calculationMultiplication[l][t] = calculationMultiplication[l][t] + board[l][z] * board2[z][t];
}
}
}
//display output
System.out.println("\nProduct of the 2 matrices is ");
for (int i = 0; i < calculationMultiplication.length; i++) {
for (int j = 0; j < calculationMultiplication[0].length; j++) {
System.out.print(calculationMultiplication[i][j] + " ");
}
System.out.println();
}
} //end of main
} //end of class Main
The problem is that you've not filled the board2 array, so all its elements will be 0. In the for loop, you should also assign random values to board2. You are doing it twice for the board array.
//fill the grid
for (int row2 = 0; row2 < board2.length; row2++) {
for (int col2 = 0; col2 < board2[row2].length; col2++) { // notice 'board2[row].length'
board2[row2][col2] = rand.nextInt(10);
}
}
You should do something similar in the for loops where you display the array:
//display output
for (int m = 0; m < board2.length; m++) {
for (int n = 0; n < board2[m].length; n++) { // notice 'board2[row].length'
System.out.print(board2[m][n] + " ");
}
System.out.println();
}
board2 is always zero.
You fill the wrong array here:
//fill the grid
for (int row2 = 0; row2 < board2.length; row2++) {
for (int col2 = 0; col2 < board[row2].length; col2++) {
board[row2][col2] = rand.nextInt(10);
}
}
Basically you just did simple copy paste mistakes that can be avoidable if you used methods. I'll tell you how but first here's how you can discover your mistake:
System.out.println(String.format("%d * %d = %d",board[l][z], board2[z][t], board[l][z] * board2[z][t]));
Add this just before doing the calculation for calculationMultiplication[l][t]. Try if you want...
Anyway getting back to your mistakes, you have populated your first matrix only, the second one contain zeros (verify your code in the for loop for inserting random numbers into the second matrix), therefore any number multiplied by zero is equal to zero.
I haven't looked at all your code, because it contain a lot of copy paste which confused you and could confuse any one as well, so here's a better way to avoid mistakes in printing the matrix and inserting random numbers:
Printing:
void printMatrix(int[][] matrix){
for(int row =0; row < numOfRows; row++){
for(int col =0; col < numOfCols; col++){
System.out.print(matrix[row][col]+" ");
}
System.out.println(); // new line
}
}
Inserting:
void insertMatrix(int[][] matrix){
for(int row =0; row < numOfRows; row++){
for(int col =0; col < numOfCols; col++){
matrix[row][col] = rand.nextInt(10); // rand must be declared outside any method
}
}
}
Putting all together:
import java.util.*;
public class Main {
// create the grid
final static int rowWidth = 9;
final static int colHeight = 9;
static Random rand;
public static void main(String[] args) {
rand = new Random();
int[][] board = new int[rowWidth][colHeight];
int[][] board2 = new int[rowWidth][colHeight];
int[][] calculationMultiplication = new int[rowWidth][colHeight];
// fill
insertMatrxi(board);
// display output
printMatrix(board);
System.out.println();
// fill
insertMatrxi(board2);
// display output
printMatrix(board2);
for (int l = 0; l < rowWidth; l++) {
for (int t = 0; t < colHeight; t++) {
for (int z = 0; z < rowWidth; z++) {
calculationMultiplication[l][t] += board[l][z] * board2[z][t];
}
}
}
// display output
System.out.println("\nProduct of the 2 matrices is ");
printMatrix(calculationMultiplication);
} // end of main
public static void printMatrix(int[][] matrix) {
for (int row = 0; row < rowWidth; row++) {
for (int col = 0; col < colHeight; col++) {
System.out.print(matrix[row][col] + " ");
}
System.out.println(); // new line
}
}
public static void insertMatrxi(int[][] matrix) {
for (int row = 0; row < rowWidth; row++) {
for (int col = 0; col < colHeight; col++) {
matrix[row][col] = rand.nextInt(10);
}
}
}
} // end of class Main

How do you print rows and columns in a 2-d array?

I'm trying to get this array to display 4 values arranged in a 2x2 array. With what I'm doing so far, I'm getting an array out of bounds error. How can I do this display properly?
import java.util.Scanner;
import java.util.Random;
public class GridPractice
{
public static void main(String[] args)
{
//declarations
Scanner in = new Scanner(System.in);
Random generator = new Random();
int [][] grid; //un-instantiated grid
int size = 0; //number of rows and columns
//get size of grid - no validation & instantiate
System.out.print("Enter size of grid: ");
size = in.nextInt();
grid = new int[size][size];
//fill grid with random number from 1..99
System.out.println();
for (int row=0; row<size; row++)
{
for (int col=0; col<size; col++)
{
grid[row][col] = generator.nextInt(100); //random numbers 0.99 - not 100
}
}
System.out.printf("%2d\n", grid[size][size]);
for (int row = 0; row < size; row++) {
for (int col = 0; col < size; col++) {
System.out.printf("%2d ", grid[row][col]);
}
System.out.println();
}
Try this:
for (int row = 0; row < size; row++) {
for (int col = 0; col < size; col++) {
System.out.print(grid[row][col] + " ");
}
System.out.println("");
}
plus remove this line, this is the culprit of the exception:
System.out.printf("%2d\n", grid[size][size]);

Arrays and 'null' can I remove it and will it affect a random number generator

I'm just wondering is there a way to make the 'null' that is outputted when using a for loop output white space?
For example, I have a 2d array of an type object and when displaying the array I want to display the first three elements and then skip say 10 empty spaces then print another three elements.
Also if I was to use a random generator to pick array elements at random would the empty elements affect this or not?
Main:
public class CinemaSystem {
public static void main(String[] args) {
Seat cinemaactual = new Seat("Cinema");
Seat[][] cinema = new Seat[12][23];
Ticket ticket = new Ticket();
Scanner scan = new Scanner(System.in);
String answer, contiune;
int number, check = 0, category, id;
do {
System.out.print("Welcome to the Theatre Booking System. (QUIT to exit)"
+ "\nWould you like to purchase tickets or list available seats?"
+ "(/Purchase/List/Help)");
answer = scan.nextLine();
if (answer.equalsIgnoreCase("purchase")) {
do {
System.out.println("Please choose the cateogry of ticket "
+ "you would like, followed by who the ticket is for"
+ "and the amount required. (separated by a space)\n"
+ "1. Gold\n2. Silver\n3. Bronze\n\n1. Adult\n2."
+ " Child\n3. Concession");
category = scan.nextInt();
number = scan.nextInt();
id = scan.nextInt();
if (category == 1 || category == 2 || category == 3 && id == 1 || id == 2 || id == 3) {
ticket.SetType(category);
if (category == 1) {
ticket.SetName("Gold");
} else if (category == 2) {
ticket.SetName("Siler");
} else {
ticket.SetName("Bronze");
}
ticket.SetNumber(number);
ticket.SetID(id);
if (id == 1) {
ticket.SetCategory("Adult");
} else if (id == 2) {
ticket.SetCategory("Child");
} else {
ticket.SetCategory("Bronze");
}
System.out.print("You have selected"
+ ticket.GetNumber() + " " + ticket.GetName()
+ " ticket(s) at the" + ticket.GetCategory() + " price .");
ticket.BuyTicket(category, id, number);
} else {
System.out.print("Sorry, incorrect input, please enter an apropriate value.");
check = scan.nextInt();
}
} while (check == 0 || check > 3);
do {
System.out.print("Would you like to perchase more tickets? (Yes/No)");
contiune = scan.nextLine();
} while (contiune.equalsIgnoreCase("Yes"));
} else if (answer.equalsIgnoreCase("list")) {
cinemaactual.CreateTheatre(cinema);
cinemaactual.DisplayTheatre(cinema);
} else if (answer.equalsIgnoreCase("help")) {
// Code for help
} else if (answer.equalsIgnoreCase("quit")) {
System.exit(-1);
}
System.out.print("Sorry, incorrect input please enter"
+ " a valid input (Purchase/List/Help or QUIT to exit");
answer = scan.nextLine();
} while (!answer.equalsIgnoreCase("purchase")
|| !answer.equalsIgnoreCase("List")
|| !answer.equalsIgnoreCase("help")
|| !answer.equalsIgnoreCase("quit"));
}
}
Method in the class that makes the array:
public Seat[][] CreateTheatre(Seat[][] x) {
for (int row = 0; row < 8; row++) {
for (int col = 0; col < 4; col++) {
x[row][col] = new Seat("B");
}
}
for (int row = 8; row < 12; row++) {
for (int col = 0; col < 4; col++) {
x[row][col] = new Seat("S");
}
}
for (int row = 0; row < 8; row++) {
for (int col = 19; col < 23; col++) {
x[row][col] = new Seat("B");
}
}
for (int row = 8; row <12; row++){
for (int col = 19; col < 23; col++) {
x[row][col] = new Seat("S");
}
}
for (int row = 3; row < 5; row++) {
for (int col = 4; col < 9; col++) {
x[row][col] = new Seat("B");
}
}
for (int row = 3; row < 5;row++){
for (int col = 14; col < 19; col++) {
x[row][col] = new Seat("S");
}
}
for (int row = 9; row < 12; row++) {
for (int col = 7; col < 4; col++) {
x[row][col] = new Seat("S");
}
}
for (int row = 3; row < 5; row++) {
for (int col = 14; col < 20; col++) {
x[row][col] = new Seat("B");
}
}
for (int row = 5; row < 9; row++) {
for (int col = 4; col < 9; col++){
x[row][col] = new Seat("S");
}
}
for (int row = 5; row < 9; row++) {
for (int col = 14; col < 20; col++) {
x[row][col] = new Seat("S");
}
}
for (int row = 6; row < 9; row++) {
for (int col = 9; col < 14; col++) {
x[row][col] = new Seat("G");
}
}
for (int row = 9; row < 12; row++) {
for (int col = 7; col < 16; col++) {
x[row][col] = new Seat("G");
}
}
return x;
}
For the display of the array:
public void DisplayTheatre(Seat[][] x) {
for (int row = 0; row < x.length; row++) {
for (int col = 0; col < x[row].length; col++) {
System.out.print(x[row][col] + " ");
}
System.out.println();
}
}
Just wondering if I can remove the null since I want some objects display then blank space in the middle and then some more, on the same line.
Also do null elements interfere with randomly selecting elements from an array, like will a null element ever be randomly selected?
if i understand your requirement. below is something you are looking for .
String[] arr = new String[5];
arr[0]="2";
arr[1]="3";
arr[4]="5";
for(int i=0; i<arr.length; i++){
if(arr[i]==null){
System.out.println(' ');
}
else {
System.out.println(arr[i]);
}
}
Also if I was to use a random generator to pick array elements at random would the empty elements affect this or not?
If some of the elements were null, then picking an element at random would inevitably give you a null sometimes.
You could attempt to deal with this by trying again with a new random number ... until you get a non-null element. But that could be expensive if the array is sparse. And it could give you an infinite loop if all of the elements are null.
(But don't just search from the initial random position for a non-null element ... because that will give a biased selection.)
However a better solution would be to have a separate list containing just the unoccupied seats (or whatever).
And an ideal cinema booking system would not allocate seats at random. Rather, it would try to fill methodically ... so that Jim and his girlfriend have a good chance of sitting together even if they buy the last 2 tickets.

Categories

Resources