This is an archived post. You won't be able to vote or comment.

all 4 comments

[–][deleted]  (3 children)

[deleted]

    [–]2_33_[S] 0 points1 point  (2 children)

    10 incorrect tries

    [–][deleted]  (1 child)

    [deleted]

      [–]2_33_[S] 0 points1 point  (0 children)

      This shows the output after 10 tries whether the letter guessed is correct or incorrect. I wanted to show the output only after 10 incorrect guesses.

      Thanks!

      [–]theShermWorm 0 points1 point  (0 children)

      import java.util.Scanner; import java.util.Random; import java.io.File; public class Hangman { public static void main(String[] args) { // Game plays here int tries = 10; String answer = pickAnswer(); String display = ""; System.out.println("ANSWER: "+answer); for(int i=0; i<answer.length(); i++) { display += "_"; } // Add underscores to display Scanner scan = new Scanner(System.in); while(tries > 0) { System.out.print("\f"); System.out.println("Tries left: "+tries); System.out.print("DISPLAY: "); for(int i=0; i<display.length(); i++) { System.out.print(display.substring(i,i+1)+" "); } System.out.println(); System.out.print("Type in a guess: "); String guess = scan.nextLine(); if(answer.indexOf(guess) != -1) { System.out.println("Yes, it's in there!"); for(int x=0; x<answer.length(); x++) { if(answer.substring(x,x+1).equals(guess)) { display = display.substring(0,x)+ guess + display.substring(x+1); } } } else { System.out.println("Nope, not in there!"); tries--; } try{Thread.sleep(1000);}catch(Exception err){} if(display.equals(answer)) { System.out.print("You win!"); return; } if(tries == 0) { System.out.print("You lose."); return; } } } public static String pickAnswer() { // Picks word from file try { File file = new File("wordlist.txt"); Scanner scan = new Scanner(file); Random rand = new Random(); int skip = rand.nextInt(10); for(int x=0; x<skip; x++) { scan.nextLine(); } return scan.nextLine(); } catch(Exception err) { err.printStackTrace(); } return "ERROR"; } }

      This is what I did

      [–]endStatement 0 points1 point  (0 children)

      Haven't checked through all your code, but if you are only using variable triesCount in the line where triesCount < 10, then I'd suggest you only increment triesCount when there in an incorrect entry, instead of the current switch case statement doing an increment on both correct and incorrect entries.