TizzyT

GuessingGame

Oct 3rd, 2018
319
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.27 KB | None | 0 0
  1. package com.company;
  2. import java.util.Scanner;
  3. public class Main {
  4.     public static void main(String[] args) {
  5.         Scanner kb = new Scanner(System.in);
  6.         System.out.println("Enter a string to be guessed: ");
  7.         String secret = kb.nextLine();
  8.         System.out.print("\033[H\033[2J");
  9.         int wrongs = 0;
  10.         String corrects = "";
  11.         while (wrongs < 6 && corrects.length() < secret.length()) {
  12.             String indexes = "";
  13.             System.out.println("Guess a character:");
  14.             String guess = kb.nextLine();
  15.             for (int i = 0; i < secret.length(); i++)  if (secret.charAt(i) == guess.charAt(0)) indexes += i + ", ";
  16.             if (indexes.length() == 0) {
  17.                 wrongs++;
  18.                 System.out.println("Your guess '" + guess + "' was wrong\nYou have guessed wrong " + wrongs + " times.");
  19.             } else {
  20.                 System.out.println("'" + guess.charAt(0) + "' is at index " + indexes + "Congrats!!!");
  21.                 if(!corrects.contains(guess)) for (int i = 0; i < indexes.length() / 3; i++) corrects += guess;
  22.             }
  23.         }
  24.         if (wrongs < 6) System.out.println("You've revealed the string: " + secret);
  25.         else System.out.println("You failed to reveal: " + secret);
  26.     }
  27. }
Advertisement
Add Comment
Please, Sign In to add comment