Write a java program that generates a random integer between 1 and 0 (and display the
integer), and then asks the user to guess 1 or 0 (input 1 or 0). The game repeats 5 times.
Compare the user input with the generated random number each time. Every time the
user guesses it right, display “bingo”.
Answers
import java.util.Scanner;
public class GuessGame {
public static void main(String[ ] args) {
for (int i = 0; i < 5; i++) {
System.out.print("\nGuess 1 or 0 - ");
int guess = new Scanner(System.in).nextInt( ),
randomNumber = (int) (Math.random( ) * 2);
System.out.println(guess == randomNumber ? " Bingo!" : " Nope!");
}
}
}
Question:-
Write a java program that generates a random integer between 1 and 0 and ask the user yo guess 1 or 0. The game repeats 5 times. Every time the user guesses right, display bingo.
Program:-
import java.util.*;
class Random
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int i=1;
while(i++<=5)
{
System.out.print("Enter your number (1 or 0): ");
int n=sc.nextInt();
int r=(int)(Math.random()*2);
System.out.println((r==n)?"Bingo!! ": "Wrong Choice. ");
}
}
}