The user gets 3 tries to guess the lucky number (set the lucky number =4) Write
ID: 3782099 • Letter: T
Question
The user gets 3 tries to guess the lucky number (set the lucky number =4) Write generalized code (i.e. if I want to change the lucky number value, I change it at only one location within the program) The user is prompted to enter a number between 1 and 10 The entered number is compared to the lucky number, if it Is equal to the lucky number, tell the user that they guessed the correct number, if not tell them to guess again. After each guess, tell them how many guesses they have left. Store all the user guesses in a matrix called 'user_guesses'. At the end of the game display the numbers guessed by the user. HWS program details: The user gets as many tries as required to guess the lucky number (set the lucky number =4) Write generalized code The user is prompted to enter a number between 1 and 10 The entered number is compared to the lucky number, if it is equal to the lucky number, tell the user that they guessed the correct number, if not tell them to guess again. When they eventually guess the correct number, tell them how many tries it took them to guess the correct number.Explanation / Answer
PROGRAM CODE:
HW4
package array;
import java.util.Scanner;
public class HW4 {
public static void main(String[] args) {
int luckyNumber = 4, max_guesses = 3, guess, counter = 0;
int user_guesses[] = new int[max_guesses];
Scanner sc = new Scanner(System.in);
while(counter<max_guesses)
{
System.out.print(" Enter a number between 1 and 10: ");
guess = sc.nextInt();
user_guesses[counter++] = guess;
if(guess == luckyNumber)
System.out.println("Your guess is correct !");
else
System.out.println("Your guess is wrong !");
System.out.println("You have " + (max_guesses-counter) + " guesses left");
}
//Printing all the guesses
System.out.println(" Your guesses ");
for(int i=0; i<user_guesses.length; i++)
{
System.out.println(user_guesses[i]);
}
}
}
OUTPUT:
Enter a number between 1 and 10: 1
Your guess is wrong !
You have 2 guesses left
Enter a number between 1 and 10: 3
Your guess is wrong !
You have 1 guesses left
Enter a number between 1 and 10: 4
Your guess is correct !
You have 0 guesses left
Your guesses
1
3
4
HW5:
package array;
import java.util.Scanner;
public class HW5 {
public static void main(String[] args) {
int luckyNumber = 4, max_guesses = 1000, guess, counter = 0;
Scanner sc = new Scanner(System.in);
boolean isContinued = true;
while(isContinued)
{
System.out.print(" Enter a number between 1 and 10: ");
guess = sc.nextInt();
if(guess == luckyNumber)
{
System.out.println("Your guess is correct !");
isContinued = false;
}
else
System.out.println("Your guess is wrong !");
counter++;
}
System.out.println(" You took " + counter + " guesses to find the lucky number");
}
}
OUTPUT:
Enter a number between 1 and 10: 2
Your guess is wrong !
Enter a number between 1 and 10: 3
Your guess is wrong !
Enter a number between 1 and 10: 6
Your guess is wrong !
Enter a number between 1 and 10: 1
Your guess is wrong !
Enter a number between 1 and 10: 9
Your guess is wrong !
Enter a number between 1 and 10: 4
Your guess is correct !
You took 6 guesses to find the lucky number
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.