-
Notifications
You must be signed in to change notification settings - Fork 0
/
MAIN
85 lines (78 loc) · 3.26 KB
/
MAIN
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import java.util.Scanner;
import java.util.Random;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Random random = new Random();
// Math Quiz Game
System.out.println("Welcome to the Math Quiz Game!");
System.out.println("What is 5 + 7?");
int mathAnswer = scanner.nextInt();
if (mathAnswer == 12) {
System.out.println("Correct!");
} else {
System.out.println("Incorrect! The correct answer is 12.");
}
// Word Association Game
System.out.println("Welcome to the Word Association Game!");
System.out.println("Enter a word: ");
String word1 = scanner.next();
System.out.println("Enter a word associated with " + word1 + ": ");
String word2 = scanner.next();
// Your association logic here
System.out.println("Your association is correct!");
// Code Breaker Game
System.out.println("Welcome to the Code Breaker Game!");
System.out.println("Guess the 4-digit code (each digit is between 1 and 9): ");
int codeToGuess = random.nextInt(9000) + 1000;
int attempts = 0;
while (true) {
int guess = scanner.nextInt();
attempts++;
if (guess == codeToGuess) {
System.out.println("Congratulations! You guessed the code " + codeToGuess + " in " + attempts + " attempts!");
break;
} else {
System.out.println("Incorrect guess! Try again.");
}
}
// Trivia Quiz Game
System.out.println("Welcome to the Trivia Quiz Game!");
System.out.println("Who is the author of 'To Kill a Mockingbird'?");
String triviaAnswer = scanner.next();
if (triviaAnswer.equalsIgnoreCase("Harper Lee")) {
System.out.println("Correct!");
} else {
System.out.println("Incorrect! The correct answer is Harper Lee.");
}
// Hangman Game
System.out.println("Welcome to Hangman!");
String secretWord = "banana";
StringBuilder guessedWord = new StringBuilder("_".repeat(secretWord.length()));
int attemptsLeft = 6;
while (attemptsLeft > 0) {
System.out.println("Guessed word: " + guessedWord);
System.out.println("Attempts left: " + attemptsLeft);
System.out.println("Guess a letter: ");
char guess = scanner.next().charAt(0);
if (secretWord.contains(String.valueOf(guess))) {
for (int i = 0; i < secretWord.length(); i++) {
if (secretWord.charAt(i) == guess) {
guessedWord.setCharAt(i, guess);
}
}
if (!guessedWord.toString().contains("_")) {
System.out.println("Congratulations! You guessed the word: " + secretWord);
break;
}
} else {
attemptsLeft--;
System.out.println("Incorrect guess! Try again.");
}
}
if (attemptsLeft == 0) {
System.out.println("Sorry, you ran out of attempts. The word was: " + secretWord);
}
scanner.close();
}
}