In: Computer Science
Create a simple dice game in Java. Add screenshots and the program here.
JAVA CODE :
import java.util.*;
public class Main
{
public static void main(String[] args) {
Scanner s = new
Scanner(System.in);
System.out.print("Whats the from
the dice do you want (1 to 6): ");
int n = s.nextInt(); // read the
roll value from the user
int count = 0;
Random rand = new Random();
while(true)
{
int num = rand.nextInt(6) + 1; //
generating the random number from 1 to 6
count++; // counting the
turns
if(n == num)
{
break;
}
}
// print the no of turns that it
takes to roll
System.out.println("It takes " +
count + " turns to get a value from a rolled dice");
}
}
OUTPUT :