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 (^-^)
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