Design and implement a Java program for programming exercise 6.30, page 241, fro
ID: 671954 • Letter: D
Question
Design and implement a Java program for programming exercise 6.30, page 241, from the textbook (name it CrapsGame). Rolling a dice is simply generating a random integer number between 1 and 6. Write a separate method for this function, call it rollDice() to return the generated number. Follow the game rules stated in the problem statement. Use escape characters to format the outputs as shown in the sample outputs.
**6.30 (Game: craps) Craps is a popalar dice game played in casinos. Write a program to play a variation of the game, as follows: Roll two dice. Each die has six faces representing values 1,2,.. and 6, respec- tively. Check the sum of the two dice. If the sum is 2, 3, or 12 (called craps), you lose; if the sum is 7 or 11 (called nafural), you win; if the sum is another value i.e., 4,5, 6,8,9, or 10), a point is established. Continue to roll the dice until either a 7 or the same point value is rolled. If 7 is rolled, you lose. Otherwise, you win Your program acts as a single player. Here are some sample runs You rolled 56-11 You win You rolled 12-3 You lose You rolled 4+4-8 point is 8 You rolled 62-8 You win You rolled 32-5 point is 5 You rolled 25-7 You loseExplanation / Answer
package mani;
import java.util.Random;
public class CrapsGame {
public static int rollDice(){
Random r=new Random();
int n=r.nextInt(6)+1;
return n;
}
public static void main(String[] args) {
int n=1;
int point=0;
int d1=rollDice();
int d2=rollDice();
int sum=d1+d2;
System.out.println("You rolled "+d1+" + "+d2+" = "+sum);
if(sum==2||sum==3||sum==12){
System.out.println("You Lose");
n=0;
}else if(sum==7||sum==11){
System.out.println("You win");
n=0;
}else{
System.out.println("point is "+ sum);
point=sum;
do{
d1=rollDice();
d2=rollDice();
sum=d1+d2;
System.out.println("You rolled "+d1+" + "+d2+" = "+sum);
if(sum==7){
System.out.println("You Lose");
n=1;
}else if(sum==point){
System.out.println("You win");
n=1;
}else{
n=0;
continue;
}
}while(n==0);
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.