In: Computer Science
Using Eclipse
(RollTwoDice) Write a program RollTwoDice that repeatedly rolls two 6-sided dice until they arrive at a given desired sum that you read in from user.
1) the source code (.java file), and
2) the screenshot of running results of each question
I have written the program using JAVA PROGRAMMING LANGUAGE.
OUTPUT :
CODE :
//imported the require modules
import java.util.Random;
import java.util.Scanner;
//Main class
class Main {
//Main method
public static void main(String[] args) {
int dice1=0,dice2=0,sum=0,userinput;//declared the Integer variables
Random rand = new Random();//Random object
Scanner input = new Scanner(System.in);//scanner object
System.out.print("Please Enter the number inbetween(2 to 12 both inclusive) : ");//print statement
userinput = input. nextInt();//taking user input for the Integer
System.out.print("\nDices combinations : \n");
while(sum != userinput){
dice1 = rand.nextInt(6)+1;//rolling dice1
dice2 = rand.nextInt(6)+1;//rolling dice2
sum = dice1 + dice2 ;
System.out.println(Integer.toString(dice1)+"\t"+Integer.toString(dice2));
}
// Final output to print on the console
System.out.println("\n\nOUTPUT : \nNumber got in dice1 : "+Integer.toString(dice1));
System.out.println("Number got in dice1 : "+Integer.toString(dice2));
}
}
Thanks..