I dont understand whats wrong with my code. I have created a word guess game where on the menu the user has to select an option so a letter is generated in the word a bit like hangman but it does not appear to select a random letter everytime...Im a beginner to Java, any help would really be appreciated. It also does not appear to display the message : You lost. no attempts left...
import java.util.Random; public class Game { private Word w; private int attempts; Game(){ startGame(); } private void startGame(){ Database db = new Database(); w = new Word (db.getRandomWord()); attempts = 10; } private int showDisplayOptions(){ int input = 3; System.out.println("Your Options:"); System.out.println("Guess another letter 1"); System.out.println("Guess the word 2"); System.out.println("Give up 3"); System.out.println(); boolean validInput =false; while(!validInput){ System.out.print("Enter your choice: "); InputReader in = new InputReader(); input = in.getInt(); if (!(input==1 || input==2 || input==3)) { validInput=false; System.out.println("Invalid, Please enter options 1-3"); } else validInput=true; } return input; } public void run(){ boolean exit = false; while (!exit){ if (attempts<1) { System.out.println("You lost. No attempts left"); startGame(); } System.out.println("You know: "); System.out.println(w.toString()); System.out.println("You have "+attempts+" attempts left"); System.out.println(); switch (showDisplayOptions()) { case 1:chooseLetter(); break; case 2:chooseWord(); break; case 3: exit = true; break; default:exit = true; break; } } } private void chooseLetter() { boolean chosen=false; Random genericGenerator = new Random(); char letter = (char) (genericGenerator.nextInt(26)+65); chosen=w.hasLetter(letter); attempts--; } private void chooseWord() { InputReader in = new InputReader(); boolean chosen; System.out.println("\nEnter word: "); String letter = in.getString(); chosen = w.isRight(letter); } }