The function random.randint from the random module can be used to produce an int
ID: 3753158 • Letter: T
Question
The function random.randint from the random module can be used to produce an integer from a range of values. For example, random.randint(1,6) produces the values 1 to 6 with equal probability. Such a value Using this technique, write a program that will allow the user to play either two rounds or six rounds of the popular dice game known as craps. This game program must ask the user to determine whether to play A player rolls a pair of dice If the sum of dice is either 3 or 8 on the first throw, the player wins. If the sum is 2, 7 or 10 on the first throw, the player loses. Any other sum becomes the player's point" on the first throw. To win, the player must continue rolling the dice until a roll matches the point. This is termed "making the After the first throw, if the player rolls a 12 before making the point, the player loses Hint: import random used to load in the random number module for all the random functions.Explanation / Answer
//Java program
import java.util.Random;
import java.util.Scanner;
public class Point {
public static void main(String args[]) {
int round=0,num1,num2,point=0,sum,i;
Random random =new Random();
Scanner in =new Scanner(System.in);
System.out.print("Enter round You want to play (2 or 6): ");
round = in.nextInt();
for( i=0;i<round;i++) {
num1=1+random.nextInt(6);
num2=1+random.nextInt(6);
sum=num1+num2;
System.out.println("Round "+(i+1)+" : "+sum+" ");
if(i==0) {
if(sum==3||sum==8) {System.out.println("you wins");break;}
else if(sum==2||sum==7||sum==10) {System.out.println("you loss");break;}
else point =sum;
}
else {
if(sum==12) {System.out.println("you loss");break;}
else if(sum==point) {System.out.println("you wins");break;}
}
}
if(i==round)System.out.println("you loss");
in.close();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.