Study Java Try using Scanner or Map

If you started attending a vocational training school and learned HTML & CSS, Java with Progate etc., you can use Qiita completely ... so I tried to describe the exercises of "** Introduction to Java that is refreshing, 2nd edition **" that I purchased for studying Java in a different way from the original answer.


Practice 2-3 Create the following program.

① "Welcome to the fortune-telling hall" is displayed on the screen. ② The message "Please enter your name" is displayed on the screen. (3) Accepts one line of character input from the keyboard and stores it in the String type variable name. ④ "Please enter your age" is displayed on the screen. ⑤ Accepts one line of character input from the keyboard and stores it in the String type variable ageString. (6) Convert the contents of the variable ageString to an int type and assign it to the int type variable age. (7) Generate a random number from 0 to 3 and assign it to the int type variable fortune. ⑧ Increase the value of fortune by 1 with the increment operator to make it a random number from 1 to 4. ⑨ "The result of fortune-telling has come out!" Is displayed on the screen. ⑩ The screen will display "(age) year old (name), your luck number is (random number)". At that time, the variable age is displayed in (age), the variable name is displayed in (name), and the number created in ⑧ is displayed in (random number). "1: Daikichi 2: Nakayoshi 3: Kichi 4: Bad" is displayed on the screen.

import java.util.*;

public class practice {

	public static void main(String[] args) {
		Scanner stdInput = new Scanner(System.in);
		Random random = new Random();
		Map<Integer, String> map = new HashMap<Integer, String>();
		
		map.put(0, "Daikichi");
		map.put(1, "Nakayoshi");
		map.put(2, "Kichi");
		map.put(3, "Bad");
			
		
		System.out.println("Welcome to the fortune-telling hall!");
		System.out.print("Please input your name:");
		String name = stdInput.nextLine();
		System.out.print("Please enter your age:");
		String ageString = stdInput.nextLine();
		
		int age = Integer.parseInt(ageString);
		int fortune = random.nextInt(4);
		
		System.out.println("\n Fortune-telling results have come out!");
		System.out.println(age + "of age" + name + "San, your fortune" + map.get(fortune) + "is");
	}
}

Execution result

result



Welcome to the fortune-telling hall!
Please enter your name: Hatademeo
Please enter your age: 40

The result of fortune-telling came out!
40-year-old Hatademeo, your fortune is Nakayoshi

Book used

Introduction to Java that is refreshingly understood 2nd edition (refreshing series)

Summary

I was told to do various things, but this is the result of ignoring it. .. .. I wanted to use a Python dictionary, but the first thing I looked up was ** Map **, so I used that. It's quite annoying for java to declare data types in various places. .. .. It should be easy to understand

Please do not hesitate to point out any points you notice.

Recommended Posts

Study Java Try using Scanner or Map
Try using RocksDB in Java
Try scraping using java [Notes]
Try using Redis with Java (jar)
[Java] Try to implement using generics
Try using IBM Java method tracing
Try using Hyperledger Iroha's Java SDK
[Java] Where did you try using java?
JAVA (Map)
Try using Java framework Nablarch [Web application]
Try using the Stream API in Java
Map without using an array in java
Try using JSON format API in Java
Try using JobScheduler's REST-API --Java RestClient implementation--
Acquisition of input contents using Scanner (Java)
Try using the Wii remote with Java
[In-house study session] Java basics-execution without using IDE- (2017/07/06)
Try using Firebase Cloud Functions on Android (Java)
Try using JobScheduler's REST-API --Java RestClient Test class-
Try using libGDX
Let's study Java
java Scanner class
Try using Maven
Try using powermock-mockito2-2.0.2
Try using GraalVM
Try Java 8 Stream
Try using jmockit 1.48
Try using sql-migrate
Try using Sourcetrail (win version) in Java code
Try using GCP's Cloud Vision API in Java
Try using Sourcetrail (macOS version) in Java code
Try similar search of Image Search using Java SDK [Search]
Try accessing the dataset from Java using JZOS
Try using SwiftLint
Try using Log4j 2.0
[Java] Map comparison
[Java] Study notes
Java 8 study (repeatable)
Java study memorandum
Try communication using gRPC on Android + Java server
Try using the COTOHA API parsing in Java
Roughly try Java 9
Study Java Silver 1
Don't forget setCalendar or setTimeZone when using Java DateFormat
Try implementing the Eratosthenes sieve using the Java standard library
Try Image Search's similar search using Java SDK [Registration]
Create two or more line graphs using MPAndroidChart [Java]
How to convert A to a and a to A using AND and OR in Java
Try image classification using TensorFlow Lite on Android (JAVA)
Try global hooking in Java using the JNativeHook library
Try to build a Java development environment using Docker
Sorting using java comparator
Try using JobScheduler's REST-API
Try using java.lang.Math methods
Try using PowerMock's WhiteBox
[Java ~ Method ~] Study memo (5)
Try using Talend Part 2
Scraping practice using Java ②
Enum reverse map Java
Java study # 1 (typical type)
[Java ~ Array ~] Study memo 4