Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Store a array of characters that is the same length as the secret word. Initialize the characters to <code>*</code> and when a match is found, using <code>[indexOf][1]</code>, reveal the found characters:</p> <pre><code>String secretWord = userInput.next(); int len = secretWord.length(); //Store the length which will be used to see if puzzle was solved. char[] temp = new char[len]; //Store a temp array which will be displayed to the user for(int i = 0; i &lt; temp.length; i++) //initialize the array { temp[i] = '*'; } System.out.print("\n"); System.out.print("Word to date: "); while (attempts &lt;= 10 &amp;&amp; attempts &gt; 0) { System.out.println("\nAttempts left: " + attempts); System.out.print("Enter letter: "); String test = userInput.next(); if(test.length() != 1) { System.out.println("Please enter 1 character"); continue; } char testChar = test.charAt(0); //Find matches int foundPos = -2; int foundCount = 0; //How many matches did we find while((foundPos = secretWord.indexOf(testChar, foundPos + 1)) != -1) { temp[foundPos] = testChar; //Update the temp array from * to the correct character foundCount++; len--; //Decrease overall counter } if(foundCount == 0) { System.out.println("Sorry, didn't find any matches for " + test); } else { System.out.println("Found " + foundCount + " matches for " + test); } //Print for(int i = 0; i &lt; temp.length; i++) { System.out.print(temp[i]); } System.out.println(); if(len == 0) { break; //Solved! } attempts--; } if(len == 0) { System.out.println("\n---------------------------"); System.out.println("Solved!"); } else { System.out.println("\n---------------------------"); System.out.println("Sorry you didn't find the mystery word!"); System.out.println("It was \"" + secretWord + "\""); } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload