Advertisement
mmayoub

Loto game

Jul 3rd, 2017
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.46 KB | None | 0 0
  1. package class170629;
  2.  
  3. import java.util.Random;
  4. import java.util.Scanner;
  5.  
  6. public class arrayLoto {
  7.  
  8.     public static void main(String[] args) {
  9.         /*
  10.          * Loto game
  11.          * input: 6 different integer numbers in the range [1..49]
  12.          * process: 1) computer selects 7 integer different numbers in the range [1..49]
  13.          *          2) count the same numbers that player and computer has selected: winning numbers
  14.          * output: the number of winning numbers
  15.          */
  16.  
  17.         // create a scanner
  18.         Scanner s = new Scanner(System.in);
  19.         // create a Random object
  20.         Random rnd = new Random();
  21.  
  22.         // array for player numbers
  23.         int[] guess = new int[6];
  24.  
  25.         // read 6 different numbers from the user
  26.         System.out.println("Enter your six different numbers (1 to 49): ");
  27.         for (int i = 0; i < guess.length; i += 1) {
  28.  
  29.             boolean used; // if true: this number is in use, ignore it
  30.             do {
  31.                 used = false;
  32.                 do {
  33.                     guess[i] = s.nextInt();
  34.                 } while (guess[i] < 1 || guess[i] > 49);
  35.  
  36.                 // has this number been used before?
  37.                 for (int j = 0; j < i; j += 1) {
  38.                     if (guess[i] == guess[j]) {
  39.                         used = true;
  40.                         break;
  41.                     }
  42.                 }
  43.             } while (used);
  44.         }
  45.  
  46.         s.close();
  47.  
  48.         // array for winning numbers
  49.         int[] win = new int[7];
  50.  
  51.         // selecting winning numbers
  52.         System.out.println("Computer is thinking, please wait . . .");
  53.  
  54.         // select 7 different numbers randomly
  55.         for (int i = 0; i < win.length; i += 1) {
  56.             boolean used; // if true: this number is in use, ignore it
  57.             do {
  58.                 used = false;
  59.                 win[i] = 1 + rnd.nextInt(49);
  60.  
  61.                 // has this number been used before?
  62.                 for (int j = 0; j < i; j += 1) {
  63.                     if (win[i] == win[j]) {
  64.                         used = true;
  65.                         break;
  66.                     }
  67.  
  68.                 }
  69.             } while (used);
  70.         }
  71.  
  72.         // print winning numbers
  73.         System.out.println("\nThe computer numbers are:");
  74.         for (int num : win) {
  75.             System.out.printf("%d ", num);
  76.         }
  77.         System.out.println();
  78.  
  79.         // print player numbers and a message for winning number
  80.         System.out.println("your numbers are:");
  81.  
  82.         int winCount = 0; // counter for winning numbers
  83.         for (int guessedNumber : guess) {
  84.             String msg = "";
  85.             // check if this number wins
  86.             for (int winningNumber : win) {
  87.                 if (winningNumber == guessedNumber) {
  88.                     msg = "(win)";
  89.                     winCount += 1;
  90.                     break;
  91.                 }
  92.             }
  93.             // print the player number with a message
  94.             System.out.printf("%d%s ", guessedNumber, msg);
  95.         }
  96.  
  97.         System.out.printf("\n\nYou have %d winning numbers!", winCount);
  98.  
  99.     }
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement