Write a program that repeatedly prompts the user to enter a secret word or phras
ID: 3631003 • Letter: W
Question
Write a program that repeatedly prompts the user to enter a secret word or phrase, until they enter it correctly. Case insensitive comparison should be used. The secret word is anything you want it to be. It is "hard-coded" within the program. For example a program run might look like this, if the secret word is "Dijkstra":Hint: the secret word is the last name of a famous computer scientist
Enter the secret word > Wirth
Enter the secret word > hopper
Enter the secret word > dijkstra
Correct! Check out Edsger W. Dijkstra on wikipedia!
Explanation / Answer
import java.util.*; public class word { public static void main(String[] args) { // declare the necessary variables String secret = "Dijkstra"; secret = secret.toLowerCase(); int checker = 0; // declare a Scanner object to read input Scanner sc = new Scanner(System.in); // read input and process them accordingly //print out hint. System.out.println("Hint: the secret word is the last name of a famous computer scientist"); System.out.print("Enter the secret word > "); //loop to check if the key enter matches secret key do{ // if matches, print out message and stop looping String input = sc.next(); if(secret.equals(input.toLowerCase())){ checker = 1; System.out.println("Correct! Check out Edsger W. Dijkstra on wikipedia!"); } else { System.out.print("Enter the secret word > "); } } while(checker ==0); } }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.