Question: please explain this code. Please explain Hang method line by line. and please also add somemethod or statement that keeps track of letter that user
please explain this code. Please explain Hang method line by line. and please also add somemethod or statement that keeps track of letter that user tried. like"You tried x,y,z
1 import java.util.Scanner; 2 3 public class Hangmans 4 { 5 6 private static String[] words = {"min jae", "south county", "chuck", "computer science"}; 7 private static String word = words[(int) (Math.random() * words.length)]; 8 private static String guessWord = new String(new char[word.length()]).replace("\0", "*"); 9 private static int count = 0; 10 11 public static void main(String[] args) 12 { 13 Scanner sc = new Scanner(System.in); 14 15 while (count < 7 && guessWord.contains("*")) 16 { 17 System.out.println(); 18 System.out.println("Guess a letter"); 19 System.out.println("HINT: There can be space between a word and there is no upper case"); 20 21 System.out.println(guessWord); 22 String guess = sc.nextLine(); 23 24 hang(guess); 25 } 26 sc.close(); 27 } 28 29 public static void hang(String guess) 30 { 31 String newWord = ""; 32 for (int i = 0; i < word.length(); i++) 33 { 34 //comparing if input word is in the letter// 35 if (word.charAt(i) == guess.charAt(0)) 36 { 37 newWord += guess.charAt(0); 38 } 39 // 40 else if (guessWord.charAt(i) != '*') 41 { 42 newWord += word.charAt(i); 43 } 44 else 45 { 46 newWord += "*"; 47 } 48 } 49 50 //if the input word is wrong(nothing has changed), raise the count and call execution method// 51 if (guessWord.equals(newWord)) 52 { 53 count++; 54 System.out.println("word you tried : " + guess); 55 execution(); 56 } 57 //correct guess 58 else 59 { 60 guessWord = newWord; 61 } 62 //if player gets all letter correct// 63 if (guessWord.equals(word)) 64 { 65 System.out.println("Correct. The word was " + word); 66 } 67 } 68 69 public static void execution() 70 { 71 if (count == 1) 72 { 73 System.out.println("Wrong"); 74 System.out.println(); 75 System.out.println(); 76 System.out.println(); 77 System.out.println(); 78 System.out.println("_______"); 79 System.out.println(); 80 } 81 if (count == 2) 82 { 83 System.out.println("Wrong"); 84 System.out.println(" |"); 85 System.out.println(" |"); 86 System.out.println(" |"); 87 System.out.println(" |"); 88 System.out.println(" |"); 89 System.out.println(" |"); 90 System.out.println(" |"); 91 System.out.println("___|___"); 92 } 93 if (count == 3) 94 { 95 System.out.println("Wrong"); 96 System.out.println(" ____________"); 97 System.out.println(" |"); 98 System.out.println(" |"); 99 System.out.println(" |"); 100 System.out.println(" |"); 101 System.out.println(" |"); 102 System.out.println(" |"); 103 System.out.println(" | "); 104 System.out.println("___|___"); 105 } 106 if (count == 4) 107 { 108 System.out.println("Wrong"); 109 System.out.println(" ____________"); 110 System.out.println(" | _|_"); 111 System.out.println(" | / \\"); 112 System.out.println(" | | |"); 113 System.out.println(" | \\_ _/"); 114 System.out.println(" |"); 115 System.out.println(" |"); 116 System.out.println(" |"); 117 System.out.println("___|___"); 118 } 119 if (count == 5) 120 { 121 System.out.println("Wrong"); 122 System.out.println(" ____________"); 123 System.out.println(" | _|_"); 124 System.out.println(" | / \\"); 125 System.out.println(" | | |"); 126 System.out.println(" | \\_ _/"); 127 System.out.println(" | |"); 128 System.out.println(" | |"); 129 System.out.println(" |"); 130 System.out.println("___|___"); 131 } 132 if (count == 6) 133 { 134 System.out.println("Wrong"); 135 System.out.println(" ____________"); 136 System.out.println(" | _|_"); 137 System.out.println(" | / \\"); 138 System.out.println(" | | |"); 139 System.out.println(" | \\_ _/"); 140 System.out.println(" | |"); 141 System.out.println(" | |"); 142 System.out.println(" | / \\ "); 143 System.out.println("___|___ / \\"); 144 } 145 if (count == 7) 146 { 147 System.out.println("You just killed him lol lol lol lol lol"); 148 System.out.println(" ____________"); 149 System.out.println(" | _|_"); 150 System.out.println(" | / \\"); 151 System.out.println(" | | |"); 152 System.out.println(" | \\_ _/"); 153 System.out.println(" | _|_"); 154 System.out.println(" | / | \\"); 155 System.out.println(" | / \\ "); 156 System.out.println("___|___ / \\"); 157 System.out.println("The word was " + word); 158 } 159 }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
