21 Dice Game Write a program that plays a simple dice game b

21. Dice Game Write a program that plays a simple dice game between the computer and the user. When the program runs, a loop should repeat 10 times. Each iteration of the loop should do the following: • Generate a random integer in the range of 1 through 6. This is the value of the computer’s die. • Generate another random integer in the range of 1 through 6. This is the value of the user’s die. • The die with the highest value wins. (In case of a tie, there is no winner for that particular roll of the dice.) As the loop iterates, the program should keep count of the number of times the computer wins, and the number of times that the user wins. After the loop performs all of its iterations, the program should display who was the grand winner, the computer or the user.

Solution

/*   Java Program to Dice game */

public class Dice
{
   static int cwins=0,uwins=0;
   static int cval,uval;
    public static void main(String a[])
       {
           for(int i=1; i<=10 ;i++)                   // Loop itaration 10 times
           {
           cval=getRandomInteger(0,6);               //random number between 1 to 6 of computer
           uval=getRandomInteger(0,6);               //random number between 1 to 6 of USER
                if(cval<uval)
               {
                   uwins++;
                   System.out.println(\"Itaration \"+i+\"Dice values are Computer - \"+cval+\" User - \"+uval+\" - User is winner \ \");
               }
               else if(cval>uval)
               {
                       cwins++;
                       System.out.println(\"Itaration \"+i+\"Dice values are Computer - \"+cval+\" User - \"+uval+\" - Computer is winner \ \");
               }
               else
               {
                   System.out.println(\"Itaration \"+i+\"Dice values are Computer - \"+cval+\" User - \"+uval+\" - It\'s a tie \ \");
               }

           }
           if(cwins==uwins)                                     // grand winner conditons
           {

           System.out.println(\"Grand Winner \"+\"Computer wins - \"+cwins+\" User wins- \"+uwins+\" - It\'s a tie \ \");
           }
           else if(cwins<uwins)
           {
               System.out.println(\"Grand Winner \"+\"Computer wins - \"+cwins+\" User wins - \"+uwins+\" - User is winner \ \");
           }
           else
           {
               System.out.println(\"Grand Winner \"+\"Computer wins - \"+cwins+\" User wins - \"+uwins+\" - Computer is winner \ \");
           }
        }

  
       public static int getRandomInteger(int n1, int n2)                 //function to generate random number
       {
        return ((int) (Math.random()*(n1 - n2))) + n2;
        }

}

Note :Compile java program and then run.(javac Dice.java,java Dice)
Ouput :

Itaration 1Dice values are Computer - 4 User - 6 - User is winner

Itaration 2Dice values are Computer - 5 User - 6 - User is winner

Itaration 3Dice values are Computer - 1 User - 4 - User is winner

Itaration 4Dice values are Computer - 6 User - 5 - Computer is winner

Itaration 5Dice values are Computer - 6 User - 1 - Computer is winner

Itaration 6Dice values are Computer - 3 User - 2 - Computer is winner

Itaration 7Dice values are Computer - 3 User - 4 - User is winner

Itaration 8Dice values are Computer - 4 User - 2 - Computer is winner

Itaration 9Dice values are Computer - 2 User - 3 - User is winner

Itaration 10Dice values are Computer - 5 User - 3 - Computer is winner

Grand Winner Computer wins - 5 User wins- 5 - It\'s a tie

21. Dice Game Write a program that plays a simple dice game between the computer and the user. When the program runs, a loop should repeat 10 times. Each iterat
21. Dice Game Write a program that plays a simple dice game between the computer and the user. When the program runs, a loop should repeat 10 times. Each iterat

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site