Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- import java.util.Random;
- import java.text.DecimalFormat;
- public class Powerball2
- {
- public static void main (String[] args)
- {
- //---------------------------------------------------------------------------------------------------------
- // Header.
- //---------------------------------------------------------------------------------------------------------
- System.out.print("\n****************************************************************************************\n");
- System.out.print(" Welcome to Powerball! You have to play to win!\n");
- System.out.print(" Change NUMBER_OF_COLUMNS to fit your console size.\n");
- System.out.print("****************************************************************************************\n\n");
- //---------------------------------------------------------------------------------------------------------
- // Variables and object initialization.
- //---------------------------------------------------------------------------------------------------------
- Scanner input = new Scanner (System.in);
- DecimalFormat format1 = new DecimalFormat ("00");
- final int PLAYERS_PER_ROUND = 20;
- final int NUMBERS_TO_PLAY = 3;
- final int NUMBER_OF_COLUMNS = 2; // Columns in output, Change to fit your console.
- int[][] powerballArray = new int[NUMBERS_TO_PLAY][PLAYERS_PER_ROUND];
- int[] winningNumbers = new int[NUMBERS_TO_PLAY];
- String endLoop = "no";
- int i = 0;
- int j = 0;
- int k = 0;
- int checkWin = 0;
- int totalWinners = 0;
- int noWinRounds = 0;
- Random rand = new Random();
- //---------------------------------------------------------------------------------------------------------
- // Creating and displaying the winning numbers.
- //---------------------------------------------------------------------------------------------------------
- do
- {
- for (i = 0; i < NUMBERS_TO_PLAY; ++i)
- {
- winningNumbers[i] = rand.nextInt(10);
- }
- System.out.print("\nThe winning numbers for this round are : ");
- for (i = 0; i < NUMBERS_TO_PLAY; ++i)
- {
- System.out.print(winningNumbers[i] + " ");
- }
- //---------------------------------------------------------------------------------------------------------
- // Generating player numbers, checking for a win and displaying that
- //---------------------------------------------------------------------------------------------------------
- System.out.print("\n\nHere are the players numbers for this drawing.\n\n");
- for (j = 0; j < NUMBERS_TO_PLAY; ++j)
- {
- for (i = 0; i < PLAYERS_PER_ROUND; ++i)
- {
- powerballArray[j][i] = rand.nextInt(10);
- }
- }
- for (i = 0; i < PLAYERS_PER_ROUND; ++i)
- {
- System.out.print(" Player " + format1.format(i+1) + ": ");
- for (j = 0; j < NUMBERS_TO_PLAY; ++j)
- {
- System.out.print(powerballArray[j][i] + " ");
- }
- for (j = 0; j < NUMBERS_TO_PLAY; ++j)
- {
- if (powerballArray[j][i] == winningNumbers[j])
- {
- checkWin++;
- }
- }
- if (checkWin == NUMBERS_TO_PLAY)
- {
- System.out.print("*** We have a winner ***");
- totalWinners++;
- noWinRounds = 0;
- }
- else
- {
- System.out.print(" ");
- }
- checkWin = 0;
- k++;
- if (k % NUMBER_OF_COLUMNS == 0 && NUMBER_OF_COLUMNS != 0)
- {
- System.out.print("\n\n");
- }
- if (i == (PLAYERS_PER_ROUND -1) && totalWinners == 0)
- {
- noWinRounds++;
- }
- }
- //---------------------------------------------------------------------------------------------------------
- // Displaying totals and prompting for another round.
- //---------------------------------------------------------------------------------------------------------
- System.out.print("------------------------------------------------------------------------------------------\n\n");
- System.out.print("The total number of winners in this round of " + PLAYERS_PER_ROUND + " players: " + totalWinners + "\n");
- System.out.print("The number of rounds without a winner is : " + noWinRounds + ".\n\n");
- if (endLoop.equals("win") && totalWinners < 1)
- {
- System.out.print("No winner yet lets keep going!\n");
- }
- else
- {
- System.out.print("\nWould you like to play again? Type no to exit. Type win to play until there is a winner.\n\n");
- endLoop = input.next();
- endLoop = endLoop.toLowerCase();
- }
- totalWinners = 0;
- }
- while (!endLoop.equals("no"));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement