[Java] Age guessing game made with Java

1 minute read

Watching a TV show

At BS, there is a programming education program for children that uses Scratch as “Prosta” See it being broadcast. Among the times I have seen, I will try to guess the age of Mr. Ando with a small number of questions Since there was a corner, I tried to make an age target game in Java with reference to that part.


package andouteacher;

import java.util.Random;
import java.util.Scanner;

// It is a game that guesses Ando's age
// guess the age within the specified number of answers
// The trick is to search for 2 minutes
// Let's narrow down the candidates with efficient answers

public class Main {
public static void main(String[] args) {
Random rnd = new Random();
int age = rnd.nextInt(80);
int limit = 5;

Scanner sc = new Scanner(System.in);
System.out.println("How old does Ando look?");
System.out.println("Opportunity to reply is "+ limit + "up to times");
for(int i = 0; i <limit; i++) {
int answer = sc.nextInt();
if(answer == age) {
} else if(answer >age){
System.out.println("younger than that");
} else {
System.out.println("older than that");
System.out.println("The correct answer is + age + "was old");

There was no limit to the number of questions you could ask in the program, but in this program The number of times is limited to 5. It is an int type variable called limit. It’s a very simple game, but when you play it, you can enjoy it surprisingly. If you are interested, please run this code and play with it (^-^)


By the way, when Ando-sensei asks, “Are you over 70?” (This is a story of the interaction that was on the program and has nothing to do with this program…)