Words Reverse Exercise
Additional Exercises
import java.util.Scanner;
import java.util.Random;
import java.util.Scanner;
public class Main {
static String[] words = {"apple", "banana", "cherry", "orange", "grape", "watermelon, pear, cherry, pomegranate, kiwi, guava"};
static String word = words[new Random().nextInt(words.length)];
static int count = 0;
static char[] dashes = new char[word.length()];
public static void main(String[] args) {
for (int i = 0; i < dashes.length; i++) {
dashes[i] = '_';
}
Scanner input = new Scanner(System.in);
boolean gameOver = false;
while (!gameOver) {
System.out.println(dashes);
System.out.print("You have seven chances to get the word wrong. The category is fruit. ");
System.out.print("Guess a letter: ");
char guess = input.next().charAt(0);
boolean correctGuess = false;
for (int i = 0; i < word.length(); i++) {
if (word.charAt(i) == guess) {
System.out.println("Correct!");
dashes[i] = guess;
correctGuess = true;
}
}
if (!correctGuess) {
System.out.println("Wrong guess, try again");
count++;
hangmanImage();
}
if (count == 7) {
System.out.println("You lost, the word was: " + word);
gameOver = true;
}
if (new String(dashes).equals(word)) {
System.out.println("Congratulations, you guessed the word!");
gameOver = true;
}
}
}
public static void hangmanImage() {
if (count == 1) {
System.out.println(" +---+");
System.out.println(" | |");
System.out.println(" |");
System.out.println(" |");
System.out.println(" |");
System.out.println(" |");
System.out.println("=========");
} else if (count == 2) {
System.out.println(" +---+");
System.out.println(" | |");
System.out.println(" O |");
System.out.println(" |");
System.out.println(" |");
System.out.println(" |");
System.out.println("=========");
} else if (count == 3) {
System.out.println(" +---+");
System.out.println(" | |");
System.out.println(" O |");
System.out.println(" | |");
System.out.println(" |");
System.out.println(" |");
System.out.println("=========");
} else if (count == 4) {
System.out.println(" +---+");
System.out.println(" | |");
System.out.println(" O |");
System.out.println(" /| |");
System.out.println(" |");
System.out.println(" |");
System.out.println("=========");
} else if (count == 5) {
System.out.println(" +---+");
System.out.println(" | |");
System.out.println(" O |");
System.out.println(" /|\\ |");
System.out.println(" |");
System.out.println(" |");
System.out.println("=========");
} else if (count == 6) {
System.out.println(" +---+");
System.out.println(" | |");
System.out.println(" O |");
System.out.println(" /|\\ |");
System.out.println(" / |");
System.out.println(" |");
System.out.println("=========");
} else if (count == 7) {
System.out.println(" +---+");
System.out.println(" | |");
System.out.println(" O |");
System.out.println(" /|\\ |");
System.out.println(" / \\ |");
System.out.println(" |");
System.out.println("=========");
}
}
break;
}
Main.main(null);