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

1. Write an interactive program that prompts for a desired sum, then repeatedly

ID: 3566968 • Letter: 1

Question

1. Write an interactive program that prompts for a desired sum, then repeatedly rolls two six-sided dice until their sum is the desired sum. You are supposed to generate random numbers to simulate two dice. Here is the expected dialogue with the user (2-credit) Desired dice sum: 9 4 and 3 = 7 3 and S = 8 5 and ? = 11 5 and ? = 11 1 and S = 6 6 and 3 = 9 Finish the following code file: import java.util.*; public class TestDiceRoll { public static void main(String[) args) { Scanner console = new Scanner(System.in); System.out.print(''Desired dice sum: ''); int sum = console.nextInt(); // your code here }

Explanation / Answer

import java.util.Random;
import java.util.Scanner;


public class TestDiceRoll {
   /**
   * @param args
   */
   public static void main(String[] args) {
       int n,w;
       Scanner console=new Scanner(System.in);
       System.out.print("Desired dice Sum:");
       int sum =console.nextInt();
       Random r=new Random();
      
       n=r.nextInt(5)+1;
       w=r.nextInt(5)+1;
       while((n+w)!=sum){
           System.out.println(n+"and"+w);
           n=r.nextInt(5)+1;
           w=r.nextInt(5)+1;
       }
       System.out.println(n+"and"+w);
   }

}