Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
3 views1 page

Guess The Number Game Java

Uploaded by

AKSHAT RAUTHAN
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)
3 views1 page

Guess The Number Game Java

Uploaded by

AKSHAT RAUTHAN
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

//Guess the no game made by Akshat.

Wrote all the code all by myself but had to


optimize it using reference. Logic is clear😎😎

import java.util.Scanner;
import java.util.Random;

class Game{
int number;
int inputNumber;
int noOfGuesses = 0;

public void setNoOfGuesses(){


noOfGuesses = noOfGuesses;
}

Game(){
Random rand = new Random();
this.number = rand.nextInt(100);
}
public void takeUserInput(){
Scanner sc = new Scanner(System.in);
inputNumber = sc.nextInt();
}
boolean isCorrectNumber(){
noOfGuesses++;
if (inputNumber==number){
System.out.format("Yes you guessed it right, it was %d\nYou guessed it
in %d attempts", number, noOfGuesses);
return true;
}
else if(inputNumber<number){
System.out.println("Too low...");
}
else if(inputNumber>number){
System.out.println("Too high...");
}
return false;
}
}

public class Ex3_Guess_The_No {


public static void main(String[] args) {
Game g = new Game();
boolean b = false;
while (!b){
g.takeUserInput();
b = g.isCorrectNumber();
}
}
}

You might also like