Answer to Question #295322 in Java | JSP | JSF for sam

Question #295322

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.



1
Expert's answer
2022-02-08T12:48:57-0500
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!");
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS