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

CWH 50

The document describes a Java program that allows a user to play a 'Guess the Number' game. It generates a random number, takes user input, and checks if the input matches the random number, tracking the number of guesses.

Uploaded by

akshayghodki520
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)
17 views2 pages

CWH 50

The document describes a Java program that allows a user to play a 'Guess the Number' game. It generates a random number, takes user input, and checks if the input matches the random number, tracking the number of guesses.

Uploaded by

akshayghodki520
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

package com.

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

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

public int getNoOfGuesses() {


return noOfGuesses;
}

public void setNoOfGuesses(int noOfGuesses) {


this.noOfGuesses = noOfGuesses;
}

Game(){
Random rand = new Random();
this.number = rand.nextInt(100);
}
void takeUserInput(){
System.out.println("Guess the number");
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 cwh_50_ex3sol {
public static void main(String[] args) {
/*
Create a class Game, which allows a user to play "Guess the Number"
game once. Game should have the following methods:
1. Constructor to generate the random number
2. takeUserInput() to take a user input of number
3. isCorrectNumber() to detect whether the number entered by the user
is true
4. getter and setter for noOfGuesses
Use properties such as noOfGuesses(int), etc to get this task done!
*/

Game g = new Game();


boolean b= false;
while(!b){
g.takeUserInput();
b = g.isCorrectNumber();
}

}
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy