please explain code precisely. especially method hang on line 37, why it it putt
ID: 3838362 • Letter: P
Question
please explain code precisely. especially method hang on line 37, why it it putting guess word at position 0?
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("", "*");
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
27 sc.close();
28 }
29
30 public static void hang(String guess)
31 {
32 String newWord = ""; //empty string//
33 //looping all elements And then compare the input word is present in the letter//
34 for (int i = 0; i < word.length(); i++)
35 {
36
37 if (word.charAt(i) == guess.charAt(0)) //If it exists, add input at position 0 to the newword
38 {
39 newWord += guess.charAt(0);
40 }
41 //
42 else if (guessWord.charAt(i) != '*') //add input at the ith position to the newword
43 {
44 newWord += word.charAt(i);
45 }
46 else //if this fails to add * to newword.
47 {
48 newWord += "*";
49 }
50 }
51
52 //if the input word is wrong(nothing has changed), raise the count and call execution method//
53 if (guessWord.equals(newWord))
54 {
55 count++;
56 System.out.println("word you tried : " + guess);
57 execution();
58 }
59 //correct guess
60 else
61 {
62 guessWord = newWord;
63 }
64 //if player gets it correct//
65 if (guessWord.equals(word))
66 {
67 System.out.println("Correct. The word was " + word);
68 }
69 }
70
71 public static void execution()
72 {
73 if (count == 1)
74 {
75 System.out.println("Wrong");
76 System.out.println();
77 System.out.println();
78 System.out.println();
79 System.out.println();
80 System.out.println("_______");
81 System.out.println();
82 }
83 if (count == 2)
84 {
85 System.out.println("Wrong");
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 System.out.println(" |");
93 System.out.println("___|___");
94 }
95 if (count == 3)
96 {
97 System.out.println("Wrong");
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 System.out.println(" | ");
106 System.out.println("___|___");
107 }
108 if (count == 4)
109 {
110 System.out.println("Wrong");
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 System.out.println(" |");
119 System.out.println("___|___");
120 }
121 if (count == 5)
122 {
123 System.out.println("Wrong");
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 System.out.println(" |");
132 System.out.println("___|___");
133 }
134 if (count == 6)
135 {
136 System.out.println("Wrong");
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 System.out.println(" | / \ ");
145 System.out.println("___|___ / \");
146 }
147 if (count == 7)
148 {
149 System.out.println("You just killed him lol lol lol lol lol");
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(" | / \ ");
158 System.out.println("___|___ / \");
159 System.out.println("The word was " + word);
160 }
161 }
162 }
Explanation / Answer
According to your program logic i understood that you want to print a message correct when the word enter by user is match with predefined array collection of words.
Other wise, it will continue print some message.
I debug your code and find out that when you given a correct word instead of showing correct, it shows some other message.
At 2 places, you did a minor mistakes.
In Hang method, you wrote this line if (word.charAt(i) == guess.charAt(0)).
According to this line, you always compare a each character of predefined word with only first character of user defined word, but you have to compare a each character of predefined word with user defined word.
So, you will achieve by doing like this
if (word.charAt(i) == guess.charAt(i))
Similarly, in next line you have to change like newWord += guess.charAt(i);
I attached a updated code. Please refer from it.
Updated Code:-
//please explain code precisely. especially method hang on line 37, why it it putting guess word at position 0?
import java.util.Scanner;
public class HelloWorld
{
private static String[] words = {"min jae", "south county", "chuck", "computer science"};
private static String word = words[(int) (Math.random() * words.length)];
private static String guessWord = new String(new char[word.length()]).replace("", "*");
private static int count = 0;
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
while (count < 7 && guessWord.contains("*"))
{
System.out.println();
System.out.println("Guess a letter");
System.out.println("HINT: There can be space between a word and there is no upper case");
System.out.println(guessWord);
String guess = sc.nextLine();
hang(guess);
}
sc.close();
}
public static void hang(String guess)
{
String newWord = ""; //empty string//
//looping all elements And then compare the input word is present in the letter//
for (int i = 0; i < word.length(); i++)
{
if (word.charAt(i) == guess.charAt(i)) //If it exists, add input at position 0 to the newword
{
newWord += guess.charAt(i);
}
//
else if (guessWord.charAt(i) != '*') //add input at the ith position to the newword
{
newWord += word.charAt(i);
}
else //if this fails to add * to newword.
{
newWord += "*";
}
}
//if the input word is wrong(nothing has changed), raise the count and call execution method//
if (guessWord.equals(newWord))
{
count++;
System.out.println("word you tried : " + guess);
execution();
}
//correct guess
else
{
guessWord = newWord;
}
//if player gets it correct//
if (guessWord.equals(word))
{
System.out.println("Correct. The word was " + word);
}
}
public static void execution()
{
if (count == 1)
{
System.out.println("Wrong");
System.out.println();
System.out.println();
System.out.println();
System.out.println();
System.out.println("_______");
System.out.println();
}
if (count == 2)
{
System.out.println("Wrong");
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 (count == 3)
{
System.out.println("Wrong");
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 (count == 4)
{
System.out.println("Wrong");
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 (count == 5)
{
System.out.println("Wrong");
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 (count == 6)
{
System.out.println("Wrong");
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 (count == 7)
{
System.out.println("You just killed him lol lol lol lol lol");
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("The word was " + word);
}
}
}
Output:-
Guess a letter
HINT: There can be space between a word and there is no upper case
*******
south county
word you tried : south county
Wrong
_______
Guess a letter
HINT: There can be space between a word and there is no upper case
*******
computer science
Guess a letter
HINT: There can be space between a word and there is no upper case
******e
min jae
Correct. The word was min jae
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.