Advertisement
Guest User

Untitled

a guest
Mar 29th, 2017
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.29 KB | None | 0 0
  1. import java.util.Scanner;
  2. import java.util.Random;
  3. import java.text.DecimalFormat;
  4.  
  5.  
  6. public class Powerball2
  7. {
  8. public static void main (String[] args)
  9. {
  10.  
  11. //---------------------------------------------------------------------------------------------------------
  12. // Header.
  13. //---------------------------------------------------------------------------------------------------------
  14.  
  15. System.out.print("\n****************************************************************************************\n");
  16. System.out.print(" Welcome to Powerball! You have to play to win!\n");
  17. System.out.print(" Change NUMBER_OF_COLUMNS to fit your console size.\n");
  18. System.out.print("****************************************************************************************\n\n");
  19.  
  20.  
  21. //---------------------------------------------------------------------------------------------------------
  22. // Variables and object initialization.
  23. //---------------------------------------------------------------------------------------------------------
  24. Scanner input = new Scanner (System.in);
  25. DecimalFormat format1 = new DecimalFormat ("00");
  26. final int PLAYERS_PER_ROUND = 20;
  27. final int NUMBERS_TO_PLAY = 3;
  28. final int NUMBER_OF_COLUMNS = 2; // Columns in output, Change to fit your console.
  29. int[][] powerballArray = new int[NUMBERS_TO_PLAY][PLAYERS_PER_ROUND];
  30. int[] winningNumbers = new int[NUMBERS_TO_PLAY];
  31. String endLoop = "no";
  32. int i = 0;
  33. int j = 0;
  34. int k = 0;
  35. int checkWin = 0;
  36. int totalWinners = 0;
  37. int noWinRounds = 0;
  38. Random rand = new Random();
  39.  
  40.  
  41. //---------------------------------------------------------------------------------------------------------
  42. // Creating and displaying the winning numbers.
  43. //---------------------------------------------------------------------------------------------------------
  44.  
  45. do
  46. {
  47. for (i = 0; i < NUMBERS_TO_PLAY; ++i)
  48. {
  49. winningNumbers[i] = rand.nextInt(10);
  50. }
  51. System.out.print("\nThe winning numbers for this round are : ");
  52. for (i = 0; i < NUMBERS_TO_PLAY; ++i)
  53. {
  54. System.out.print(winningNumbers[i] + " ");
  55. }
  56. //---------------------------------------------------------------------------------------------------------
  57. // Generating player numbers, checking for a win and displaying that
  58. //---------------------------------------------------------------------------------------------------------
  59.  
  60. System.out.print("\n\nHere are the players numbers for this drawing.\n\n");
  61.  
  62. for (j = 0; j < NUMBERS_TO_PLAY; ++j)
  63. {
  64. for (i = 0; i < PLAYERS_PER_ROUND; ++i)
  65. {
  66. powerballArray[j][i] = rand.nextInt(10);
  67. }
  68. }
  69.  
  70. for (i = 0; i < PLAYERS_PER_ROUND; ++i)
  71. {
  72. System.out.print(" Player " + format1.format(i+1) + ": ");
  73. for (j = 0; j < NUMBERS_TO_PLAY; ++j)
  74. {
  75. System.out.print(powerballArray[j][i] + " ");
  76. }
  77. for (j = 0; j < NUMBERS_TO_PLAY; ++j)
  78. {
  79. if (powerballArray[j][i] == winningNumbers[j])
  80. {
  81. checkWin++;
  82. }
  83. }
  84. if (checkWin == NUMBERS_TO_PLAY)
  85. {
  86. System.out.print("*** We have a winner ***");
  87. totalWinners++;
  88. noWinRounds = 0;
  89. }
  90. else
  91. {
  92. System.out.print(" ");
  93. }
  94. checkWin = 0;
  95. k++;
  96. if (k % NUMBER_OF_COLUMNS == 0 && NUMBER_OF_COLUMNS != 0)
  97. {
  98. System.out.print("\n\n");
  99. }
  100. if (i == (PLAYERS_PER_ROUND -1) && totalWinners == 0)
  101. {
  102. noWinRounds++;
  103. }
  104. }
  105.  
  106. //---------------------------------------------------------------------------------------------------------
  107. // Displaying totals and prompting for another round.
  108. //---------------------------------------------------------------------------------------------------------
  109.  
  110. System.out.print("------------------------------------------------------------------------------------------\n\n");
  111. System.out.print("The total number of winners in this round of " + PLAYERS_PER_ROUND + " players: " + totalWinners + "\n");
  112. System.out.print("The number of rounds without a winner is : " + noWinRounds + ".\n\n");
  113.  
  114.  
  115. if (endLoop.equals("win") && totalWinners < 1)
  116. {
  117. System.out.print("No winner yet lets keep going!\n");
  118. }
  119. else
  120. {
  121. System.out.print("\nWould you like to play again? Type no to exit. Type win to play until there is a winner.\n\n");
  122. endLoop = input.next();
  123. endLoop = endLoop.toLowerCase();
  124. }
  125. totalWinners = 0;
  126. }
  127. while (!endLoop.equals("no"));
  128. }
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement