Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package class170629;
- import java.util.Random;
- import java.util.Scanner;
- public class arrayLoto {
- public static void main(String[] args) {
- /*
- * Loto game
- * input: 6 different integer numbers in the range [1..49]
- * process: 1) computer selects 7 integer different numbers in the range [1..49]
- * 2) count the same numbers that player and computer has selected: winning numbers
- * output: the number of winning numbers
- */
- // create a scanner
- Scanner s = new Scanner(System.in);
- // create a Random object
- Random rnd = new Random();
- // array for player numbers
- int[] guess = new int[6];
- // read 6 different numbers from the user
- System.out.println("Enter your six different numbers (1 to 49): ");
- for (int i = 0; i < guess.length; i += 1) {
- boolean used; // if true: this number is in use, ignore it
- do {
- used = false;
- do {
- guess[i] = s.nextInt();
- } while (guess[i] < 1 || guess[i] > 49);
- // has this number been used before?
- for (int j = 0; j < i; j += 1) {
- if (guess[i] == guess[j]) {
- used = true;
- break;
- }
- }
- } while (used);
- }
- s.close();
- // array for winning numbers
- int[] win = new int[7];
- // selecting winning numbers
- System.out.println("Computer is thinking, please wait . . .");
- // select 7 different numbers randomly
- for (int i = 0; i < win.length; i += 1) {
- boolean used; // if true: this number is in use, ignore it
- do {
- used = false;
- win[i] = 1 + rnd.nextInt(49);
- // has this number been used before?
- for (int j = 0; j < i; j += 1) {
- if (win[i] == win[j]) {
- used = true;
- break;
- }
- }
- } while (used);
- }
- // print winning numbers
- System.out.println("\nThe computer numbers are:");
- for (int num : win) {
- System.out.printf("%d ", num);
- }
- System.out.println();
- // print player numbers and a message for winning number
- System.out.println("your numbers are:");
- int winCount = 0; // counter for winning numbers
- for (int guessedNumber : guess) {
- String msg = "";
- // check if this number wins
- for (int winningNumber : win) {
- if (winningNumber == guessedNumber) {
- msg = "(win)";
- winCount += 1;
- break;
- }
- }
- // print the player number with a message
- System.out.printf("%d%s ", guessedNumber, msg);
- }
- System.out.printf("\n\nYou have %d winning numbers!", winCount);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement