Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In java, write a method called addDice that accepts a Scanner for the console as

ID: 3630977 • Letter: I

Question

In java, write a method called addDice that accepts a Scanner for the console as a parmeter and prompts for a desired sum, then repeatadly simulates rolling two die until they reach the desired sum.

Here is a sample Output:

Desired dice Sum: 8
4 and 3 = 7
4 and 2 = 6
5 and 2 = 7
3 and 5 = 8

Also, if the user asks for a number impossible to roll on two die, like the number 14 the result should return the word invalid. A scanner, a random, and a while loop MUST BE USED... do not use do/while loops.

Explanation / Answer

public static void main(String[] args)
{
Scanner kb = new Scanner(System.in);

System.out.print("Desired dice sum: ");
int sum = kb.nextInt();

// check invalid
if(sum < 2 || sum > 12)
{
System.out.println("INVALID");
return;
}

int die1 = 0, die2 = 0;

while(die1+die2 != sum)
{
// roll dice
die1 = (int)(Math.random()*6+1);
die2 = (int)(Math.random()*6+1);

System.out.println(die1+" and "+die2+" = "+(die1+die2));
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote