Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company;
- import java.util.Scanner;
- public class Main {
- public static void main(String[] args) {
- Scanner kb = new Scanner(System.in);
- System.out.println("Enter a string to be guessed: ");
- String secret = kb.nextLine();
- System.out.print("\033[H\033[2J");
- int wrongs = 0;
- String corrects = "";
- while (wrongs < 6 && corrects.length() < secret.length()) {
- String indexes = "";
- System.out.println("Guess a character:");
- String guess = kb.nextLine();
- for (int i = 0; i < secret.length(); i++) if (secret.charAt(i) == guess.charAt(0)) indexes += i + ", ";
- if (indexes.length() == 0) {
- wrongs++;
- System.out.println("Your guess '" + guess + "' was wrong\nYou have guessed wrong " + wrongs + " times.");
- } else {
- System.out.println("'" + guess.charAt(0) + "' is at index " + indexes + "Congrats!!!");
- if(!corrects.contains(guess)) for (int i = 0; i < indexes.length() / 3; i++) corrects += guess;
- }
- }
- if (wrongs < 6) System.out.println("You've revealed the string: " + secret);
- else System.out.println("You failed to reveal: " + secret);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment