Age guessing game made in Java

I was watching a certain TV program

BS has a programming education program for children called "Prosta" based on Scratch. See it being broadcast. In the times I saw, I tried to guess the age of Mr. Ando with a small number of questions. Since there was a corner, I tried to make an age guessing game in Java with reference to that part.

Main.java


package andouteacher;

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

//It is a game to guess the age of Mr. Ando
//Let's guess the age among the specified number of answers
//The trick is a 2-minute search
//Let's narrow down the candidates with an efficient answer

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-sensei look like?");
		System.out.println("The chance to answer"+ limit +"Up to times");
		for(int i = 0; i < limit; i++) {
			int answer = sc.nextInt();
			if(answer == age) {
				System.out.println("Is the correct answer");
				break;
			} 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 a year 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 times. This is a variable called limit of type int. It's a very simple game, but you can enjoy it unexpectedly when you play it. If you are interested, please run this code and play with it (^-^)

Postscript

By the way, Professor Ando is a little nervous when asked "Are you over 70?" (This is a story of an exchange that was in the program and has nothing to do with this program ...)

Recommended Posts

Age guessing game made in Java
I made a rock-paper-scissors game in Java (CLI)
I made roulette in Java.
I made a simple calculation problem game in Java
Rock-paper-scissors game for beginners in Java
I made an annotation in Java.
Sample vending machine made in Java
I made the "Sunshine Ikezaki game" I saw on Twitter in Java.
Sample vending machine made in Java (classification)
Java number guessing game (standard programming problem)
Partization in Java
Changes in Java 11
Pi in Java
FizzBuzz in Java
I made a primality test program in Java
Refactored GUI tools made with Java8 + JavaFX in 2016
[java] sort in list
Read JSON in Java
Interpreter implementation in Java
Make Blackjack in Java
Rock-paper-scissors app in Java
Constraint programming in Java
Put java8 in centos7
NVL-ish guy in Java
Combine arrays in Java
Callable Interface in Java
Comments in Java source
Azure functions in java
Format XML in Java
Simple htmlspecialchars in Java
Boyer-Moore implementation in Java
Hello World in Java
Use OpenCV in Java
webApi memorandum in java
Ping commands in Java
Various threads in java
Heapsort implementation (in java)
Zabbix API in Java
ASCII art in Java
Compare Lists in Java
POST JSON in Java
Express failure in Java
Create JSON in Java
Date manipulation in Java 8
Use PreparedStatement in Java
What's new in Java 9,10,11
Parallel execution in Java
Initializing HashMap in Java
A brief explanation of a maze game made in Java for a cousin of an elementary school student
Try using RocksDB in Java
Read binary files in Java 1
Avoid Yubaba's error in Java
Get EXIF information in Java
Save Java PDF in Excel
[Neta] Sleep Sort in Java
Edit ini in Java: ini4j
Java history in this world
Let Java segfault in 6 lines
Try calling JavaScript in Java
Try developing Spresense in Java (1)
Try functional type in Java! ①