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

-can you solve this question in java programming? : Write a program that plays a

ID: 3740937 • Letter: #

Question

-can you solve this question in java programming?

: Write a program that plays a simple dice game between the computer and the user. The computer throws the dice and a random number is generated between 1 and 6. Your program asks the user to guess what the number is. If the user’s guess is higher than the random number, the program should display “That is too high – Guess again”. If the user’s guess is lower than the random number, the program should display “That is too low – Guess again”. If the user guesses correctly, “Congrats! You nailed it!”, is displayed to the user. The program should use a loop that repeats until the user correctly guesses the random number. Remember to keep track of the number of guesses that the user makes (see sample output). You may want to include a sentinel value (of -1), so that the user may exit the dice game at any time.

Explanation / Answer

// save this java code as Dice.java

import java.util.*;

public class Dice

{

public static int randNum()

{

// this function is to get a random number between 1-6

Random rand = new Random();// this is a predefined class for random numbers

int r=rand.nextInt(7); // here nextInt(7) will generate a random number below 7

if (r>0) return r; // if the generated number is betwee 1-6 then return

else return randNum(); // if the generated number is 0

// then regenerate a new number

// because a dice dosen't contain a '0' on it

}

public static void main(String[] args)

{

Scanner input = new Scanner(System.in);// to take the input from the keyboard form the user

int guess=0,r;// guess is initially 0 and user will enter his guess number everytime

// r is to store the randomely generate number

while(guess!=-1)

{

// these three lines are just for good design

// you can remove it if you don't want

System.out.println("+---+");

System.out.println("| x |");

System.out.println("+---+");

r=randNum();// generating a random number and storing it into r

System.out.print("Guess The number : ");

guess = input.nextInt();// taking the input from the user to guess

if(guess==-1)

{

// if he entered -1 then

// here -1 is using as the sentinel to stop the program

System.out.println("Exiting!");

break;

}

else if (guess>6)

{

System.out.println("Sorry! That's an Invalid number!");

System.out.println("A Dice dosen't Contain a number grater than 6 or 0");

System.out.println("Try again!");

}

else if(guess>r)

{

// if guees number is > generated random number then print

System.out.println("That is too high - Guess again!");

}

else if(guess==r)

{

// if he guesses correctly then

System.out.println("+---+");

System.out.println("| "+r+" |");

System.out.println("+---+");

System.out.println("Congrats! You nailed it!");

break;// stoping after correct guess

}

else if(guess<r)

{

// if guess number less than random number

System.out.println("That is too low - Guess again!");

}

}

}

}