A program is required for a computer game. The user keys in the number of rounds he wishes to play.
For each round the user enters his lucky number. The program takes the number and divides it with a
secret number. If the remainder of the division is zero, it is considered a draw for the round and the total
score is incriminated by 1. Otherwise if it is any other even number, it is considered a win for the round
and the total score is incremented by 3. However if it is an odd number, it is considered a loss for the
round and the total score is decremented by 3. This is done until he completes his rounds. He wins if the
total score at the end is a positive number. Write a Java program to accomplish this.
import java.util.Random;
import java.util.Scanner;
public class Main {
public static final int MAGIC_NUMBER = new Random().nextInt(100) + 1;
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int score = 0;
System.out.println("Rounds:");
int rounds = in.nextInt();
for (int i = 0; i < rounds; i++) {
System.out.println("Lucky number:");
int luckyNumber = in.nextInt();
if (luckyNumber % MAGIC_NUMBER == 0) {
System.out.println("Draw(+1)");
score++;
} else if (luckyNumber % MAGIC_NUMBER % 2 == 0) {
System.out.println("Win(+3)");
score += 3;
} else {
System.out.println("Loss(-3)");
score -= 3;
}
System.out.println("Score " + score);
}
System.out.println(score > 0 ? "You win!" : "You lose!");
}
}
Comments
Leave a comment