- Simplest Guessing Numbers Game code :
import java.util.Scanner;
class ab
{
public static class game
{
void guess()
{
}
}
public static void main(String []args)
{
Scanner a = new Scanner(System.in);
int value = 88;
int turn = 1;
int num = a.nextInt();
while(num != 88)
{
System.out.println("Unvalid Choice..Try Again");
num = a.nextInt();
turn ++;
}
System.out.println(" It took you "+ turn +"Turns to find number");
}
}
--------------------------------------------
- Advance level Guessing Numbers Game with random Numbers
import java.util.Scanner;
import
java.util.Random;
class
GuessingGame
{
public
static void cls()
{
for
(int i = 0; i < 50; ++i) System.out.println();
};
public
static void getch()
{
System.out.println("\n
Press any key to continue");
Scanner
get = new Scanner(System.in);
String
a = get.nextLine();
}
public
static class game
{
int
value=88;
Random
rand = new Random();
int
num = rand.nextInt(100);
void
guess()
{
int
turn=1;
for(;turn
<= value;turn++)
{
int
turns=11-turn;
if(value==10)
{
System.out.println("Remaining
Turns : "+ turns);
}
System.out.print("Guess
Number and press Enter \n Your Guess : ");
Scanner
n = new Scanner(System.in);
int
number = n.nextInt();
if(number!=num)
{
System.out.println("\n
Wrong number. Try again");
getch();
}
else
{
System.out.println("\n
It took you " +turn+ " turns to guess the number");
break;
}
cls();
}
}
};
public
static void main(String []args)
{
game
game1 = new game();
Scanner
b = new Scanner(System.in);
String
evenOdd;
cls();
System.out.println("Please
Select Level \n\n Press 1 for Easy \n Press 2 for Intermediate \n
Press 3 for Advance \n");
int
level = b.nextInt();
switch(level)
{
case
1:
cls();
System.out.println("\n>>
Hint : Number is between 1-100 <<\n");
game1.guess();
break;
case
2:
cls();
if(game1.num%2==0)
{
evenOdd="EVEN";
}
else
{
evenOdd="ODD";
}
System.out.println("\n>>
Hint : Number is a "+evenOdd+" value");
game1.guess();
break;
case
3:
cls();
System.out.println("\n>>
You have just 10 turns for guessing Number <<\n");
game1.value=10;
game1.guess();
break;
default:
System.out.println("Wrong
Selection");
}
}
}