1 Develop an algorithm for a Java program that randomly pick

1. Develop an algorithm for a Java program that randomly pick a whole number between 1 and 100 inclusive. It should then ask the user to guess the number. In response to the guess, have the program respond “Too High”, “Too Low” or “That’s Right”. If the guess is right, the program should end. Otherwise, the program should allow the user to make another guess and continue on until they get it right. A user should be able to abort the program by entering a negative number. At the end, tell the user how many guesses they made before finding the correct number – but only if they are successful. Finally, if the user guesses correctly OR aborts, they should be given the opportunity to start over with a new number.

Solution

since the starting point of java program is main method.

main(){

   do{
       GuessTheNoGame();
       ch = want to start the game again // ask user of want to start the game
   }while(ch!=N || ch!=n)
}

GuessTheNoGame()
   int randomnumber = generateRandomNumer()
   int count = 0;
   do
   {
       enterednumber = enter any no between 1 and 100; //user will enter any number between 1 and 100
       if(enterednumber <0)
           exit from loop;

       if(enterednumber == randomnumber)
       {
           print That\'s right;
           print \"you have taken \"+count+\" chances\";
           exit from loop //to exit from the program if guess is right
       }
       if(enterednumber >randomnumber)
       {  
           print \"Too high\"
           count++;
       }
       else if(enterednumber <randomnumber)
       {
           print \"too low\"
           count++;
       }
   }while(true)
  
end; //end of guessthenogame

generateRandomNumer()
generate a random number between 1 and 100
end; // end of generateRandomNumer

1. Develop an algorithm for a Java program that randomly pick a whole number between 1 and 100 inclusive. It should then ask the user to guess the number. In re

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site