Program:-
/*create a class Game, which allow a user to play "Guess the Number game"
Game should have the following methods:
1. Constructor to generate the random number.
2. takeUserInput() to take user Input or number.
3. isCorrectNumber() to detect weather the number entered by user is true.
use getter and setter for noOfGuesses.
use properties such as noOfGuesses(int), etc to get this task Done.*/
/*This code is written by Yash Nikhade that is me */
import java.util.Scanner;
import java.util.Random;
class Game{
int RanNo;
int UseNo;
int a;
Scanner sc = new Scanner(System.in);
Random ra = new Random();
public Game(){
RanNo = ra.nextInt(100);
}
public void takeUserInput(){
for(int i =1;i<=100;i++){
System.out.print("guess of number : ");
UseNo = sc.nextInt();
if(UseNo>RanNo){
System.out.println("The no is greater than the Random Number");
}
else if(UseNo<RanNo){
System.out.println("The no is less than the Random Number");
}
if(RanNo == UseNo){
break;
}
a=i;
}
}
//here getter and setter is used in wrong way so avoid this.
public int setGuess(int a){
return a;
}
public void getGuess(){
int b = 100-a-1;
System.out.println("Your total score is : " + b );
}
public void isCorrectNumber(){
if(RanNo == UseNo) {
System.out.println("Now you are correct");
}
}
}
public class GuessTheNumberV43 {
public static void main(String[] args) {
Game obj = new Game();
//System.out.println(obj.RanNo);-->this line print the number randomly chose by the computer.
obj.takeUserInput();
obj.isCorrectNumber();
obj.getGuess();
}
}


