So my issue is that I have most of my code set up, except the loops I have tried always seem to iterate extra times, throwing off the correct/incorrect count. I have been stuck trying different combinations of while and for loops and this was the last straw. Any help or ideas on how I can get it in the right direction would be really appreciated. I am a newbie so my code may be sloppy or whatever but as long as it works.Subclass for getting input
package hangman;
import java.util.Scanner;
public class Hangman {
private String word;
public void setWord() {
Scanner scan = new Scanner(System.in);
System.out.println("Enter the secret word: (Under 7 letters)");
this.word = scan.nextLine();
this.word = word;
}
public String getWord() {
return this.word;
}
}
Superclass containing the logic of the game
/*
* Hangman
* 5/23/18
* JDK Version 1.8.0
*/
package hangman;
import java.awt.*;
import java.util.Scanner;
import javax.swing.*;
public class PlayHangman {
public static void main(String[] args) {
Hangman hangman = new Hangman();
Scanner scn = new Scanner(System.in);
int triesCount = 0;
int correctCount = 0;
int triesLimit = 7;
hangman.setWord();
String secretWord = hangman.getWord();
int winCount = secretWord.length();
StringBuilder b = new StringBuilder(secretWord.length());
for (int i = 0; i < secretWord.length(); i++) {
b.append("*");
}
char[] secrectStrCharArr = secretWord.toCharArray();
int charCnt = secretWord.length();
for (int x = 0; triesCount < triesLimit; x++) {
System.out.println("Secrect Word :" + b.toString());
System.out.println("Guess a letter :");
char guessChar = scn.next().toCharArray()[0];
for (int i = 0; i < secrectStrCharArr.length; i++) {
if(correctCount == winCount)
{System.out.println("Congrats! You have won!");}
else if (guessChar == secrectStrCharArr[i]) {
b.setCharAt(i, guessChar);
correctCount++;hangmanImage(triesCount,correctCount);
} else if (guessChar != secrectStrCharArr[i]) {
triesCount++;
System.out.println("Incorrect: " + triesCount);
hangmanImage(triesCount, correctCount);
}
}
}
}
public static void hangmanImage(int triesCount, int correctCount) {
if (triesCount == 1) {
System.out.println("Wrong guess, try again - Incorrect: " + triesCount+" |Correct: "+correctCount);
System.out.println(" ____________");
System.out.println(" | _|_");
System.out.println(" | / \\");
System.out.println(" | | |");
System.out.println(" | \\_ _/");
System.out.println(" | ");
System.out.println(" | ");
System.out.println(" | ");
System.out.println(" | ");
System.out.println(" | ");
System.out.println(" | ");
}
if (triesCount == 2) {
System.out.println("Wrong guess, try again - Incorrect: " + triesCount+" | Correct: "+correctCount);
System.out.println(" ____________");
System.out.println(" | _|_");
System.out.println(" | / \\");
System.out.println(" | | |");
System.out.println(" | \\_ _/");
System.out.println(" | | ");
System.out.println(" | | ");
System.out.println(" | | ");
System.out.println(" | ");
System.out.println(" | ");
System.out.println(" | ");
}
if (triesCount == 3) {
System.out.println("Wrong guess, try again - Incorrect: " + triesCount+" | Correct: "+correctCount);
System.out.println(" ____________");
System.out.println(" | _|_");
System.out.println(" | / \\");
System.out.println(" | | |");
System.out.println(" | \\_ _/");
System.out.println(" | | / ");
System.out.println(" | | -/ ");
System.out.println(" | | ");
System.out.println(" | ");
System.out.println(" | ");
System.out.println(" | ");
}
if (triesCount == 4) {
System.out.println("Wrong guess, try again - Incorrect: " + triesCount+" | Correct: "+correctCount);
System.out.println(" ____________");
System.out.println(" | _|_");
System.out.println(" | / \\");
System.out.println(" | | |");
System.out.println(" | \\_ _/");
System.out.println(" | | / ");
System.out.println(" | | -/ ");
System.out.println(" | | ");
System.out.println(" | ");
System.out.println(" | ");
System.out.println(" | ");
}
if (triesCount == 5) {
System.out.println("Wrong guess, try again - Incorrect: " + triesCount+" |Correct: "+correctCount);
System.out.println(" ____________");
System.out.println(" | _|_");
System.out.println(" | / \\");
System.out.println(" | | |");
System.out.println(" | \\_ _/");
System.out.println(" | \\ | / ");
System.out.println(" | \\- | -/ ");
System.out.println(" | | ");
System.out.println(" | ");
System.out.println(" | ");
System.out.println(" | ");
}
if (triesCount == 6) {
System.out.println("Wrong guess, try again - Incorrect: " + triesCount+" |Correct: "+correctCount);
System.out.println(" ____________");
System.out.println(" | _|_");
System.out.println(" | / \\");
System.out.println(" | | |");
System.out.println(" | \\_ _/");
System.out.println(" | \\ | / ");
System.out.println(" | \\- | -/ ");
System.out.println(" | | ");
System.out.println(" | \\ ");
System.out.println(" | \\ ");
System.out.println(" | -- ");
}
if (triesCount == 7) {
System.out.println("Wrong guess, try again - Incorrect: " + triesCount+" |Correct: "+correctCount);
System.out.println(" ____________");
System.out.println(" | _|_");
System.out.println(" | / \\");
System.out.println(" | | |");
System.out.println(" | \\_ _/");
System.out.println(" | \\ | / ");
System.out.println(" | \\- | -/ ");
System.out.println(" | | ");
System.out.println(" | / \\ ");
System.out.println(" | / \\ ");
System.out.println(" | --- --- ");
}
if(triesCount>7)
{System.out.println("You lost");}
}
}