Skip to main content

Write a program to allow two players to play a game of dice. Each player has 5 consecutive Chances of rolling the dice. The player who has higher to total score is declared the winner. In case of a tie, the player who threw a lower number on the first roll is declared the winner

Write a program to allow two players to blay a game of dice. Each player has 5 consecutive Chances of rolling the dice. The player who has higher to tal score is declared the winner. In case of a tie, the player who threw a lower number on the first roll is declared the winner
Code:
import java.util.Scanner;
public class Main {
 public static void main(String[] args) {
  Scanner sc = new Scanner(System.in);
  int scoreA[] = new int[5];
  int scoreB[] = new int[5];
  int tscoreA = 0, tscoreB = 0;
  System.out.println("Player 1 Plays:");
  for (int i = 0; i < 5; i++) {
   System.out.println("Press 1 to roll the dice");
   sc.next();
   int r = (int)(Math.random() * 6) + 1;
   scoreA[i] = r;
   tscoreA += r;
  }
  System.out.println("Player 2 Plays:");
  for (int i = 0; i < 5; i++) {
   System.out.println("Press 1 to roll the dice");
   sc.next();
   int r = (int)(Math.random() * 6) + 1;
   scoreB[i] = r;
   tscoreB += r;
  }
  System.out.println("The dice rolls of player 1 are");
  for (int i = 0; i < 5; i++)
   System.out.println(scoreA[i]);
  System.out.println("The dice rolls of player 2 are");
  for (int i = 0; i < 5; i++)
   System.out.println(scoreB[i]);
  if (tscoreA > tscoreB)
   System.out.println("Player 1 Wins");
  else if (tscoreB > tscoreA)
   System.out.println("Player 2 Wins");
  else {
   if (scoreA[0] < scoreB[0])
    System.out.println("Player 1 Wins");
   else if (scoreA[0] > scoreB[0])
    System.out.println("Player 2 Wins");
   else
    System.out.println("It is a tie");
  }
 }
}

Output

Popular posts from this blog