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

0 Lab4- ITTO Exercise 2: Word game Create a new program, WordQuessingGame. Using

ID: 3585706 • Letter: 0

Question

0 Lab4- ITTO Exercise 2: Word game Create a new program, WordQuessingGame. 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 untl the user guesses the word. Ater 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 · If the guess does not have an equal number ofcharacters, tel te 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 gane Guesa a word: borp The word is longer Incorrect. Guess againi turtle The word is longer Gueas againi cowabangazzzz The word is shorter Incorreet. Guess againi Valenbine You have7 letters correct Incorrect. Guess agains VALENTINE Tou winl Would you like to play again? Y/n: n

Explanation / Answer

import java.util.Scanner;

class Main {
public static void main(String[] args) {
  
String s2 ="valentine";
System.out.println("Welcome to the word guessing game ");
System.out.print("Guess a word:");
  
while (true) {

Scanner s = new Scanner(System.in);
String s1 = s.nextLine();
String p =" y";
  
if (p.equals("n")) {
break;
}
if(s1.length()>s2.length()){
System.out.println("The word is shorter");
System.out.print("Incorrect. Guess again: ");
}
if(s1.length()<s2.length()){
System.out.println("The word is longer");
System.out.print("Incorrect. Guess again: ");
}

if(s1.length()==s2.length() && !s1.equals(s2)){
int c=0;
for(int i=0; i<s1.length(); i++){
String string1 = Character.toString(s1.charAt(i));
String string2 = Character.toString(s2.charAt(i));
if(string1.equals(string2)){
c++;}
}
System.out.println("you have "+c+ " letter correct");
System.out.print("Incorrect. Guess again: ");
}

if(s1.length()==s2.length() && s1.equals(s2)){
System.out.println("You Win!");
System.out.print("Would you like to play again? (y/n) ");
Scanner sss = new Scanner(System.in);
p = sss.nextLine();
if(p.equals("n")){
break;
}
}
}
}
}