Skip to content

Commit cd47a5c

Browse files
Merge pull request #428 from Alisha-786/my-branch
Created a word riddle game in java
2 parents 35b3144 + e9f63ee commit cd47a5c

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import java.util.Scanner;
2+
3+
public class WordRiddleGame {
4+
5+
public static void main(String[] args) {
6+
Scanner scanner = new Scanner(System.in);
7+
8+
// Define a list of riddles and their answers
9+
String[] riddles = {
10+
"I am taken from a mine, and shut up in a wooden case, from which I am never released, and yet I am used by almost every person. What am I?",
11+
"I speak without a mouth and hear without ears. I have no body, but I come alive with the wind. What am I?",
12+
// Add more riddles here
13+
};
14+
String[] answers = {
15+
"Pencil",
16+
"Echo",
17+
// Add more answers here
18+
};
19+
20+
// Game loop
21+
for (int i = 0; i < riddles.length; i++) {
22+
System.out.println("Riddle " + (i + 1) + ": " + riddles[i]);
23+
System.out.print("Your answer: ");
24+
String userAnswer = scanner.nextLine();
25+
26+
if (userAnswer.equalsIgnoreCase(answers[i])) {
27+
System.out.println("Correct! You solved the riddle.");
28+
} else {
29+
System.out.println("Incorrect. The answer is: " + answers[i]);
30+
}
31+
32+
// Ask if the player wants to play another riddle
33+
System.out.print("Do you want to play another riddle? (yes/no): ");
34+
String playAgain = scanner.nextLine().toLowerCase();
35+
if (!playAgain.equals("yes")) {
36+
break; // Exit the game if the user doesn't want to play again
37+
}
38+
}
39+
40+
System.out.println("Thanks for playing!");
41+
scanner.close();
42+
}
43+
}

0 commit comments

Comments
 (0)