Number_guessing_game
package com.company;
import java.util.Random;
import java.util.Scanner;
public class game {
int no_of_guesses;
int computerinput;
int inputno;
public int getNo_of_guesses() {
return no_of_guesses;
}
public void setNo_of_guesses(int no_of_guesses) {
this.no_of_guesses = no_of_guesses;
}
game(){
Random s=new Random();
this.computerinput=s.nextInt(100);
}
public void takeuserinput(){
System.out.println("Enter the number ");
Scanner sc=new Scanner(System.in);
inputno=sc.nextInt();
}
boolean iscorrectnumber(){
no_of_guesses++;
if (inputno==computerinput ){
System.out.format("Yes you have guessed it correct it was %d \n you guessed it in %d attempts ",computerinput,no_of_guesses);
return true;
}
else if(inputno<computerinput){
System.out.println("Too low..");
}
else if(inputno>computerinput){
System.out.println("Too high..");
}
return false;
}
public static void main(String[] args) {
game g=new game();
boolean b =false;
while(!b){
g.takeuserinput();
b=g.iscorrectnumber();
}
}
}
o/p
Enter the number 50 Too high.. Enter the number 40 Too high.. Enter the number 20 Too high.. Enter the number 10 Too high.. Enter the number 5 Too low.. Enter the number 8 Too high.. Enter the number 7 Too high.. Enter the number 6 Yes you have guessed it correct it was 6 you guessed it in 8 attempts Process finished with exit code 0
Comments
Post a Comment