Answer to Question #91802 in Java | JSP | JSF for Havierwrld

Question #91802
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
2019-07-19T07:48:04-0400
import java.util.Scanner;

public class Main {
    private static Scanner scanner = new Scanner(System.in);
    public static void main(String[] args) {
        int score=0;
        int secretNumber = (int)Math.random()*5+1; //secret number formula
        int temp;
        System.out.println("Input number of rounds");
        int numberOfRounds = inputInt();
        System.out.println("Start the game!!");
        while (numberOfRounds>0) {
            System.out.println("Enter number");
            temp=inputInt();
            temp=temp%secretNumber;
            if (temp==0) score++;
            else if (temp%2==0) score=score+3;
            else score=score-3;
            numberOfRounds--;
        }
        System.out.println("Final score: " + score);
        if (score>0) System.out.println("YOU WIN!!!");
        else System.out.println("LOSE=(");
        scanner.close();
    }

    private static int inputInt() {
        int i=0;
        while (i<=0) {
            i=Integer.parseInt(scanner.next());
        }
        return i;
    }

}

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