0% found this document useful (0 votes)
29 views

String Manipulation

The document contains code for a guessing game that: 1) Randomly selects a color from an array and hides it with asterisks. 2) Allows the user 3 attempts to guess letters in the color. 3) Reveals correctly guessed letters in the hidden word after each turn. 4) Ends when the user guesses the full word or uses all attempts, displaying the result.

Uploaded by

Tejo, Reymund C.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views

String Manipulation

The document contains code for a guessing game that: 1) Randomly selects a color from an array and hides it with asterisks. 2) Allows the user 3 attempts to guess letters in the color. 3) Reveals correctly guessed letters in the hidden word after each turn. 4) Ends when the user guesses the full word or uses all attempts, displaying the result.

Uploaded by

Tejo, Reymund C.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

import java.util.

*;

class Guessmybrief{
public static void main(String[] args) {
Scanner input = new Scanner(System.in);

String[] words = {"", "yellow", "pink", "red", "blue"};


Random random = new Random();
int index = random.nextInt(words.length);
String word = words[index];
String hidden = new String(new char[word.length()]).replace('\0', '*');

int attempts = 1;
boolean guessed = false;

System.out.println("Guess the color of my brief,yellow,pink,red,blue, You


have 3 attempts.");
while (!guessed && attempts < 3) {
System.out.println(hidden);
System.out.print("Guess a letter: ");
char guess = input.next().charAt(0);

if (word.indexOf(guess) == -1) {
attempts++;
System.out.println("Wrong guess! You have " + (4 - attempts) + "
attempts left.");
} else {
char[] hiddenArray = hidden.toCharArray();
for (int i = 0; i < word.length(); i++) {
if (word.charAt(i) == guess) {
hiddenArray[i] = guess;
}
}
hidden = String.valueOf(hiddenArray);
}

if (hidden.equals(word)) {
guessed = true;
System.out.println("Congratulations! You guessed the color of my
brief word: " + word);
}
}

if (!guessed) {
System.out.println("Sorry, wala na!! ang sagot kasi ay " + word);
}
}
}

You might also like