Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 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