0% found this document useful (0 votes)
40 views2 pages

Add Congradulation After Completing

The document describes a hangman game program written in Java. It generates a random word from an array, displays underscores for the letters, takes user input for guesses, and replaces underscores with correct letters while tracking attempts remaining.
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)
40 views2 pages

Add Congradulation After Completing

The document describes a hangman game program written in Java. It generates a random word from an array, displays underscores for the letters, takes user input for guesses, and replaces underscores with correct letters while tracking attempts remaining.
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/ 2

import java.util.

Scanner;
public class MyProgram
{//class
public static void main(String[] args)
{//main
int tries=5;
Scanner scan=new Scanner(System.in);
String[] words={"fit", "java", "assignment",
"game", "hello","internet", "face","programming",
"happiness","mother","top","may" ,"rich","miracle"};
//---------------------- create a index between 0 till 14--------------

double r=Math.random()*14;

System.out.println((int)r);

//----------------------------------------------------------------------

String word=words[(int)r];
System.out.println(word);
//-------------------------- Step 2 (create one string for selected word)
StringBuilder guesseWord=new StringBuilder(word.length());
System.out.println(guesseWord);
//---- add _ inside the guesseWord ---------------------
for(int i=0;i<word.length();i++)
guesseWord.append("-");
System.out.println("guesse this Word-----> \n"+guesseWord);
//----------------------------------------------------------------------
while (tries>0 && guesseWord.indexOf("-")!=-1)
//------- while
{//while
System.out.println("Enter a letter: ");
//----------------- Step 3)get only a letter from user
char guess=scan.next().charAt(0);
//----- for test------------------------------------
//System.out.println("guesseletter is-----> \n"+guess);
//------------ step 4 (Subsitude inside word)
boolean found=false;
//System.out.println("found befor for loop: "+ found);
for (int i=0;i<word.length();i++)
{// iloop
if (word.charAt(i)==guess)
{
guesseWord.setCharAt(i,guess);
System.out.println("so far-----> \n"+guesseWord);
System.out.println("It is a correct letter! **"+tries+ " times
left" );
found=true;
//System.out.println("found situation: "+ found);}

}//iloop
if (found==false)
{
tries--;
System.out.println(" OOPs,It is wrong letter "+tries+ " times
left!" );
}
if(tries==0)
System.out.println("Sorry! Game is over!");
//----- while
}//while

//----------------------------------------------------------------------
}//main
}//class

You might also like