I need help creating a JAVA program that computes the following 1. Roll two rand
ID: 3910467 • Letter: I
Question
I need help creating a JAVA program that computes the following 1. Roll two random dice If the first roll is 7 or 11 the game ends and you win; OR if the first roll is 2, 3, 12 the game ends and you lose; OR if you roll a 4, 5, 6, 8, 9, 10 this is called the point and the game continues. If a point was rolled in step 2 ...continue to roll until one of two things happen: if the user rolls a 7 the game ends and they lose; OR if they roll their point again before rolling a 7 the game ends and they win. Program should run for as many full games as they want to play.
Explanation / Answer
Given below is the code for the question.
To indent code in eclipse , select code by pressing ctrl+a and then indent using ctrl+i
Please do rate the answer if it was helpful. Thank you
Game.java
-========
import java.util.Random;
import java.util.Scanner;
public class Game {
private static Random rand = new Random();
private static int roll2Dice(){
int d1 = rand.nextInt(6) + 1;
int d2 = rand.nextInt(6) + 1;
return d1 + d2;
}
public static void main(String[] args) {
String ans = "y";
int point;
int roll;
Scanner keyboard = new Scanner(System.in);
do{
point = roll2Dice();
System.out.println("First roll: " + point);
if(point == 7 || point == 11){
System.out.println("You win!");
}
else if(point == 2 || point == 3 || point == 12){
System.out.println("You loose!");
}
else{
System.out.println("Point: " + point);
do{
roll = roll2Dice();
System.out.println("Rolled: " + roll);
}while(roll != 7 && roll != point);
if(roll == point)
System.out.println("You win! Rolled point before 7");
else
System.out.println("You loose! Rolled a 7 before point");
}
System.out.print("Play again y/n ? ");
ans = keyboard.next();
System.out.println();
}while(ans.equalsIgnoreCase("y"));
}
}
output
------
First roll: 6
Point: 6
Rolled: 8
Rolled: 11
Rolled: 7
You loose! Rolled a 7 before point
Play again y/n ? y
First roll: 6
Point: 6
Rolled: 5
Rolled: 3
Rolled: 4
Rolled: 9
Rolled: 7
You loose! Rolled a 7 before point
Play again y/n ? y
First roll: 10
Point: 10
Rolled: 6
Rolled: 9
Rolled: 5
Rolled: 8
Rolled: 11
Rolled: 10
You win! Rolled point before 7
Play again y/n ? y
First roll: 7
You win!
Play again y/n ? y
First roll: 6
Point: 6
Rolled: 4
Rolled: 3
Rolled: 10
Rolled: 9
Rolled: 7
You loose! Rolled a 7 before point
Play again y/n ? n
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.