I made a simple calculation problem game in Java

Addition game

Suddenly, I remembered a game called DS training for adults who train their brains. Training on how to quickly solve the addition recorded in the game I wanted to play, so I wrote the program myself. Below is the code.


import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Random;
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		int count = 0;
		long start = 0;
		long stop = 0;
		long timeresult = 0;
		Scanner sc = new Scanner(System.in);
		System.out.println("I will give you a calculation problem.");
		System.out.println("You will be asked 10 questions.");
		System.out.println("Start with Enter.");
		String anykey = sc.nextLine();

		for(int i = 0; i < 10; i++) {
			start = System.nanoTime();
			Random rnd = new Random();
			int figure1 = rnd.nextInt(30) + 1;
			int figure2 = rnd.nextInt(30) + 1;
			int result = figure1 + figure2;
			System.out.println(figure1 + " + " + figure2 + " = ?");
			int a = sc.nextInt();
			if(result == a) {
				System.out.println("Is the correct answer.");
			} else {
				System.out.println("It's an incorrect answer.");
		stop = System.nanoTime();
		timeresult = stop - start;
		double timesecond = timeresult / 100000000.0;
		BigDecimal bdt = new BigDecimal(String.valueOf(timesecond));
		BigDecimal bdt1 = bdt.setScale(2, RoundingMode.HALF_UP);
		System.out.println("The number of correct answers is" + count + "It was a question.");
		System.out.println("The time it took to solve" + bdt1 + "It was seconds.");

In this game, 10 questions are added in a row, and the player solves them. When the game is over, the number of correct answers and the clearing time will be displayed. I found out by actually playing it, but if you try to solve it quickly I make a calculation error. Also, in the DS brain training, I wrote the answer with a touch pen, but here is Because it is input with the keyboard, it is necessary to get used to inputting numbers quickly and continuously thought. It's a very simple game, but it's a lot of fun.

What i learned

I learned about the existence of a method called nanoTime. I feel that it is very versatile, and at the same time, it is suitable for programs of various genres. I thought it could be used.

If you want to add more

In the DS brain training, evaluations such as "walking class" and "Shinkansen class" are given depending on the training results. I remember it well. Even in this program, even if there is such an expression as a comprehensive evaluation of the number of correct answers and clearing time I thought it was interesting.

Recommended Posts

I made a simple calculation problem game in Java
I made a rock-paper-scissors game in Java (CLI)
I made a primality test program in Java
I made roulette in Java.
3 Implement a simple interpreter in Java
I created a PDF in Java.
I made a shopify app @java
A simple sample callback in Java
I made a simple recommendation function.
I made an annotation in Java.
Age guessing game made in Java
[Beginner] I made a program to sell cakes in Java
I tried a calendar problem in Ruby
[Ruby] I made a simple Ping client
I made a new Java deployment tool
I made a program in Java that solves the traveling salesman problem with a genetic algorithm
I made a Diff tool for Java files
java I tried to break a simple block
I wanted to make (a == 1 && a == 2 && a == 3) true in Java
I wrote a primality test program in Java
I made a Ruby extension library in C
I wrote a prime factorization program in Java
Simple htmlspecialchars in Java
Learn Java with Progate → I will explain because I made a basic game myself
I tried to create a Clova skill in Java
[Personal memo] Make a simple deep copy in Java
I tried to make a login function in Java
I made a Restful server and client in Spring.
What I learned when building a server in Java
I made a Wrapper that calls KNP from Java
Try to solve a restricted FizzBuzz problem in Java
I made a chat app.
Find a subset in Java
2 Implement simple parsing in Java
[Java] Draw a simple pattern
I tried metaprogramming in Java
I made a server side of an online card game ⑤
I made a server side of an online card game ③
[Beginner] Try to make a simple RPG game with Java ①
I made a server side of an online card game ⑥
I just wanted to make a Reactive Property in Java
I made a server side of an online card game ④
Even in Java, I want to output true with a == 1 && a == 2 && a == 3
I tried to convert a string to a LocalDate type in Java
I made a server side of an online card game ②
I tried to make a client of RESAS-API in Java
I made a simple MVC sample system using Spring Boot
I made a Dockerfile to start Glassfish 5 using Oracle Java
A brief explanation of a maze game made in Java for a cousin of an elementary school student
I made a JAVA framework "numatrix" that easily generates unique numbers in a distributed environment & multithreading.
[Rails] I made a simple calendar mini app with customized specifications.
I sent an email in Java
I tried to create a simple map app in Android Studio
Very simple input reception in Java
I tried to illuminate the Christmas tree in a life game
Rock-paper-scissors game for beginners in Java
I can't create a Java class with a specific name in IntelliJ
I made a GUI with Swing
[Note] What I learned in half a year from inexperienced (Java)
[Note] What I learned in half a year from inexperienced (Java) (1)
I recently made a js app in the rumored Dart language