-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNameGuess.java
More file actions
40 lines (32 loc) · 1.26 KB
/
NameGuess.java
File metadata and controls
40 lines (32 loc) · 1.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import java.util.Scanner;
import java.util.Random;
public class NameGuess {
public static void main(String[] args) {
String[] names = {"Iris", "Brian", "Mak", "Seb", "Sean", "Will", "Rishaan"};
Random random = new Random();
Scanner scanner = new Scanner(System.in);
String chosenName = names[random.nextInt(names.length)];
char[] maskedName = new char[chosenName.length()];
for (int i = 0; i < maskedName.length; i++) {
maskedName[i] = '*';
}
System.out.println("guess name: " + new String(maskedName));
while (new String(maskedName).contains("*")) {
System.out.print("enter a letter: ");
char guessedLetter = scanner.nextLine().charAt(0);
boolean found = false;
for (int i = 0; i < chosenName.length(); i++) {
if (chosenName.charAt(i) == guessedLetter) {
maskedName[i] = guessedLetter;
found = true;
}
}
if (!found) {
System.out.println("letter not in the name.");
}
System.out.println(maskedName);
}
System.out.println("correct: the name was: " + chosenName);
scanner.close();
}
}