Rolling Dices through Random Class in Java

Problem:

Nour and Jad start-out the game with a cumulative score of zero. Then each player rolls three dices and a random number appears for each dice. The summation of each number is added in each player’s score. The winner is the player that has the highest score in x turns based on what the players initially decided. Note: If Jad’s score and Nour’s score are equal, let the program prints: Jad and Nour have equal scores, else let the program print who is the winner.

The output should be as follows:

Output:

Please enter the number of turns:
3
Jad's score is: 26
Nour's score is: 27
Nour is the winner

Solution:

import java.util.Random;
import java.util.Scanner;
/**
 *
 * @author George Chalhoub
 */
public class Problem3 {
    public static void main(String args[]){
        Scanner input = new Scanner(System.in);
        Random randomdice = new Random();
     
        int sumjad = 0;
        int sumnour = 0;
   
        int jadDice1 = randomdice.nextInt(7);
        int jadDice2 = randomdice.nextInt(7);
        int jadDice3 = randomdice.nextInt(7);
        int nourDice1 = randomdice.nextInt(7);
        int nourDice2 = randomdice.nextInt(7);
        int nourDice3 = randomdice.nextInt(7);
        sumjad = sumjad + jadDice1 + jadDice2 + jadDice3;
        sumnour = sumnour + nourDice1 + nourDice2 + nourDice3;
     
        System.out.println("Jad's score is: " + sumjad);
        System.out.println("Nour's score is: " + sumnour);
        if (sumjad == sumnour)
                System.out.println("Jad and Nour have the same scores");
        else
            if (sumjad > sumnour)
                System.out.println("Jad is the winner");
                else
                    System.out.println("Nour is the winner");
    }
    
}

    

No comments:

Post a Comment