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

How to solve q5? And is it right what I\'m doing or not?!! //Lab 6 Assignment Wr

ID: 3572106 • Letter: H

Question

How to solve q5? And is it right what I'm doing or not?!! //Lab 6 Assignment Write a Java Program (GuessNumber) that Declare a named constant SECRETNUMBER and assign it 5 2) Display the message: Welcome to Guess The Number Program 3)Prompts the user to enter a number between 1 and 10, this is the user's guess Save this number in an integer variable userNumber 4) Use a while loop to allow user to guess the number at maximum three times .After each ttempt gives a message Try again if all three attempts fail, give a message "You were allowed three times to guess. Thanks for using this program. 6) If user enters 5 giv him/her a message "Congratulations, you guessed the number.!

Explanation / Answer

/* This code has been done in java.compiled on eclipse */
import java.util.Scanner;
public class HelloWorld{
/* declaring secretnumber=5 */
public static final int SECRETNUMBER = 5;

public static void main(String []args){

Scanner input=new Scanner(System.in);
System.out.println("**********************************");
System.out.println("* Welcome to Guess the Number Program *");
System.out.println("**********************************");
/* variable to keep track of whether has tried more than 3 times */
int attempts=0;
/* variable to store user entered number */
int userNumber;
/* ask the user to enter an int from 1 to 10 for maximum 3 times */
while(attempts<3){
System.out.println("Enter an Int from 1 to 10 : ");
userNumber=input.nextInt();
/* if user entered number is not equal to secretnumber */
if(userNumber != SECRETNUMBER){
System.out.println("Try again ");
attempts++;
}
else
break;

}
/* PRINT congrats IF attempts is less than 3 */
if(attempts < 3)
System.out.println("Congratulations, you guessed the number.!!");
/* else print unsuccessful */
else
System.out.println("You are allowed three times to guess.Thanks for using this program. ");
}
}

/*******OUTPUT************
**********************************
* Welcome to Guess the Number Program *
**********************************
Enter an Int from 1 to 10 :   
4   
Try again   
Enter an Int from 1 to 10 :   
5   
Congratulations, you guessed the number.!!
*********OUTPUT*********************/

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