Sorry, your guess is too high. Guess a number: 74 Sorry, your guess is too high.
ID: 3586390 • Letter: S
Question
Sorry, your guess is too high. Guess a number: 74 Sorry, your guess is too high. Guess a number: 72 Sorry, your guess is too low. Guess a number: 73 You win You guessed the secret number after 5 guess(es). Your closest guess was 72. Would you like to play again? (y/n): n Exercise 2: Word game a new program, WordGuessingGame. Using a while loop, create a game for the user. The game requires the user to guess a secret word. The game does not end until the user guesses the word. After they win the game, they are prompted to choose to play again. The secret word that user must guess is "valentine". It is a case-sensitive analysis With each guess... If the guess does not have an equal number of characters, tell the user if it is too long or too short If the guess has the same number of characters, tell the user how many characters are correct Welcome to the word guessing game. Guess a word: borp The word is longer Incorrect. Guess again: turtle The word is longer Incorrect. Guess again: cowabungazzzz The word is shorter Incorrect. Guess again: Valenbine You have 7 letters correct Incorrect. Guess again: VALENTINE You have letters correct Incorrect. Guess again: valentine You win! Would you like to play again? (y/n): nExplanation / Answer
// programming in JAVA
import java.util.*;
public class WordGuessingGame {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String secret_word = "valentine";
int len = secret_word.length();
System.out.println("Welcome to the word guessing game.");
System.out.print("Guess a word: ");
String word = input.nextLine(); //input the string
while(!secret_word.equals(word))
{
int word_len = word.length();
if(len > word_len)
System.out.println("The word is longer");
else if(len < word_len)
System.out.println("The word is shorter");
else {
int count = 0;
for(int i=0;i<word_len;i++)
{
char temp = word.charAt(i);
if(secret_word.indexOf(temp) != -1) {
word.replace(temp,'@');
count++;
}
}
System.out.println("You have "+count+" correct letters");
}
System.out.print("Incorrect. Guess again:");
word = input.nextLine();
if(secret_word.equals(word)) {
System.out.println("You win");
System.out.println("Would you like to play again (y/n): ");
char choice = input.next().charAt(0);
if(choice == 'n')
break;
else {
System.out.print("Guess a word: ");
word = input.nextLine();
}
}
}
}
}
// len same the print no of same letters in example for valenbine they show 7 same letter but actuallu they are either 5 or 8
//if each letter consider as seperate enity then 8 letters are same and if one letter is consider only one time then 5 same letters
//above program consider the first condition
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.