-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTypingTutor.java
More file actions
55 lines (46 loc) · 2.13 KB
/
TypingTutor.java
File metadata and controls
55 lines (46 loc) · 2.13 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import java.util.Random;
import java.util.Scanner;
public class TypingTutor {
public static void main(String[] args) {
// Generate a random string of letters
String randomString = generateRandomString(1, 10); // Random int for desired length
// Display the random string to the user
System.out.println("Generated String: " + randomString);
// Prompt the user to replicate the string
System.out.print("Please replicate the string: ");
Scanner scanner = new Scanner(System.in);
String userInput = scanner.nextLine();
scanner.close();
// Check if the user's input matches the random string exactly (case-sensitive)
if (userInput.equals(randomString)) {
System.out.println("Congratulations! You matched the string exactly.");
}
// Check if the user's input matches the random string with case ignored
else if (userInput.equalsIgnoreCase(randomString)) {
System.out.println("Good job! Your input matches the string (ignoring case).");
}
// If neither of the above conditions is met, inform the user that they failed
else {
System.out.println("Sorry, your input does not match the string. Please try again.");
}
}
// Method to generate a random string of letters
public static String generateRandomString(int min, int max) {
Random random = new Random();
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < (max-min)+1; i++) {
//Generate a random number between 0 and 1 to determine whether to add capital or lowercase letter
int randomCase = random.nextInt(2);
char randomChar;
if (randomCase == 0){
//adds random capital letter (A-Z)
randomChar = (char) (random.nextInt(26)+'A');
}else{
//adds a random lowercase letter (a-z)
randomChar = (char) (random.nextInt(26)+'a');
}
stringBuilder.append(randomChar);
}
return stringBuilder.toString();
}
}