import java.util.*;
class GuessANumber extends Thread
{
private int number;
public GuessANumber(int number)
{
this.number = number;
}
public void run()
{
int counter = 0;
int guess = 0;
do
{
guess = (int) (Math.random() * 100 + 1);
System.out.println(this.getName()+ " guesses " + guess);
counter++;
}while(guess != number);
System.out.println("** Correct! " + this.getName()+ " in " + counter + " guesses.**");
}
}
public class ThreadGuess
{
public static void main(String [] args)
{
Scanner s=new Scanner(System.in);
System.out.println("Enter the number:");
int i=s.nextInt();
GuessANumber T1 = new GuessANumber(i);
T1.start();
}
}
Output:
class GuessANumber extends Thread
{
private int number;
public GuessANumber(int number)
{
this.number = number;
}
public void run()
{
int counter = 0;
int guess = 0;
do
{
guess = (int) (Math.random() * 100 + 1);
System.out.println(this.getName()+ " guesses " + guess);
counter++;
}while(guess != number);
System.out.println("** Correct! " + this.getName()+ " in " + counter + " guesses.**");
}
}
public class ThreadGuess
{
public static void main(String [] args)
{
Scanner s=new Scanner(System.in);
System.out.println("Enter the number:");
int i=s.nextInt();
GuessANumber T1 = new GuessANumber(i);
T1.start();
}
}
Output: