Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- public class HangMan {
- public static void main(String argsp[]) {
- Scanner in = new Scanner(System.in);
- boolean end = true;
- String word1 = "potato";
- String word2 = "tomato";
- int wrong = 0;
- for (int i = 0; i < word1.length(); i++) {
- System.out.print("_ ");
- }
- System.out.println("");
- char[] board = word1.toCharArray();
- for (int y = 0; y < board.length; y++) {
- board[y] = "_".toCharArray()[0];
- }
- while (end) {
- System.out.println("Enter your guess");
- String temp = in.next();
- char[] letterGuess = temp.toCharArray();
- for (int i = 0; i < word1.length(); i++) {
- if (letterGuess[0] == word1.charAt(i)) {
- board[i] = letterGuess[0];
- }
- else if (i == word1.length() - 1)
- {
- for (int z = 0; z < word1.length(); z++)
- {
- if (board[z] == letterGuess[0])
- {
- z = word1.length() + 1;
- }
- else
- {
- wrong++;
- z = word1.length() + 1;
- }
- }
- for (int z = 0; z < word1.length(); z++)
- {
- if (board[z] == "_".toCharArray()[0])
- {
- z = word1.length() + 1;
- }
- else if (java.util.Arrays.equals(board, word1.toCharArray()))
- {
- end = false;
- System.out.println("You have won!");
- z = word1.length() + 1;
- }
- }
- }
- }
- for (int x = 0; x < board.length;x++)
- {
- System.out.print(board[x]);
- }
- System.out.println();
- if (wrong >=5)
- {
- end = false;
- System.out.println("You killed him!");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement