Alternative method to make the game
import java.util.Random; //Calling Random Class
import java.util.Scanner; //Calling Scanner Class
public class Play
{
public static void main(String []args)
{
System.out.println("Wel-Come to our guessing game"); //Instruction to users
Random num= new Random();
int num1= num.nextInt(100);
for(int i=1; i<10;i++)
{
System.out.println(); //Printing blank line to make result better
System.out.println(+i+". Please Enter your Guess"); //Instruction to user to input value
int a= new Scanner(System.in).nextInt(); //Getting value from user
if (a==num1) //Condition
{
System.out.println(" --> Winner !!!"); //Result printing
break;
}
else if (a<num1) // Another Condition
{
System.out.println(" --> Raise Your Guess"); //Result printing
}
else //Else Statement
{
System.out.println(" --> Guess Lower"); //Result printing
}
}
System.out.println("The number is "+num1); //Printing value if user cant guess it.
}
}
This program also used the Scanner class. If you have any query about Scanner class then visit this site. (https://programmerbrothers.blogspot.com/2019/04/scanner-class-in-java-scanner-is-class.html)
It use the increment operators(++) like :- a++ and n++. To gain more knowledge about Increment and the Decrement click this link. (https://programmerbrothers.blogspot.com/2019/04/increment-anddecrement-operator-as.html)
Have a look at the previous method to make the same game in this link.(https://programmerbrothers.blogspot.com/2019/05/guess-number-and-win-prizegame.html)
Output:
import java.util.Random; //Calling Random Class
import java.util.Scanner; //Calling Scanner Class
public class Play
{
public static void main(String []args)
{
System.out.println("Wel-Come to our guessing game"); //Instruction to users
Random num= new Random();
int num1= num.nextInt(100);
for(int i=1; i<10;i++)
{
System.out.println(); //Printing blank line to make result better
System.out.println(+i+". Please Enter your Guess"); //Instruction to user to input value
int a= new Scanner(System.in).nextInt(); //Getting value from user
if (a==num1) //Condition
{
System.out.println(" --> Winner !!!"); //Result printing
break;
}
else if (a<num1) // Another Condition
{
System.out.println(" --> Raise Your Guess"); //Result printing
}
else //Else Statement
{
System.out.println(" --> Guess Lower"); //Result printing
}
}
System.out.println("The number is "+num1); //Printing value if user cant guess it.
}
}
Comments
Post a Comment