Game playing with number with computer in java

 package com.company;

import java.util.Scanner;
import java.util.Random;
class Game {
int number;
int Inputnumber;
int NumberofGuesses = 0;

public int getNumberofGuesses() {
return NumberofGuesses;
}

public void setNumberofGuesses(int numberofGuesses) {
this.NumberofGuesses = numberofGuesses;
}

Game() {
Random rn = new Random();
this.number = rn.nextInt(100);
}

void takeUserInput() {
System.out.println("enter your choice");
Scanner sc = new Scanner(System.in);
Inputnumber = sc.nextInt();
}

boolean isCorrectNumber() {
NumberofGuesses++;
if ( Inputnumber== number) {
System.out.format("yes you guessed it right, it was %d\n you get it in %d attempts ", number, NumberofGuesses);
return true;
}
else if (Inputnumber < number) {
System.out.println("too low..");
}
else if (Inputnumber > number) {
System.out.println("too high..");
}
return false;
}

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

Comments

Popular posts from this blog

priority_queue

Alignment in css

Queue data structure in c++