Find the maximum and minimum of the five numbers you entered in Java

This is the first post. There may be some parts of the text or code that are difficult to read or missing, but I hope you can see them with a warm eye.

Preface

Today, I would like to create a program in Java that "finds the maximum and minimum values from the five numbers I entered." As for the flow of explanation, I would like to explain in concrete terms after showing a picture of the code actually written. By the way, the development environment uses Eclipse.

Actually make a program

Code description

package max_min;
import java.util.*;

public class Max_Min{

   public static void main(String[] args){
      //TODO auto-generated method stub
      Scanner scanner=new Scanner(System.in);
      double max=0;
      double min=0;
      int i;
      double a;

      System.out.println("Find the maximum and minimum values from the five numbers.");
      
      for(i=1;i<6;i++){
         System.out.print(i+"Second number:");
         a=scanner.nextDouble();

         if(a<=min){
             min=a;
         }else if(a>=max){
             max=a;
         }
      }
         System.out.println("The maximum value is"+max+"is.");
         System.out.println("The minimum value is"+min+"is.");
   }
}

This is the code I actually wrote. I will explain in detail.

First, the project name is "Max_Min", the package is "max_min", and the class is "Max_Min".

Then, in order to read the number actually typed, "import java.util. *;" Is written on the second line, and the number entered is read using the "Scanner" of the external library.

In addition, the variables max and min, which represent the maximum and minimum values, are set with 0 as the initial value, and the magnitude relationship is checked one by one in the order of the entered numbers, and numbers smaller than the current minimum value are entered. If so, the operation to assign to the variable min if a number larger than the current maximum value is entered is described in the if syntax.

Run

image.png

The execution result is as above. The variables max and min are defined as double, so the end is written in decimal form!

Summary

The program that finds the maximum and minimum values may seem easy at first glance, but when I actually made it, there were some difficult parts! But it's not impossible, and it's a code that seems to have a lot of basics, so I thought it might be a good idea for beginners to think that it's the first step in practical training!

Afterword

Thank you to everyone who read through to the end !!! I wrote it at the beginning, but I think it was difficult to read because it was the first post. If you have any reflections or improvements regarding how to write this article or how to write the code, I would appreciate it if you could comment ... !!

Also, I will post an article as soon as another program is created, so please do that too! Thank you very much!!

Recommended Posts

Find the maximum and minimum of the five numbers you entered in Java
Find the maximum and minimum values out of the 5 numbers entered in Java (correction ver)
A quick explanation of the five types of static in Java
A program (Java) that outputs the sum of odd and even numbers in an array
[Java] Get the dates of the past Monday and Sunday in order
Find out the list of fonts available in AWS Lambda + Java
Find out the maximum heap size when you specify the -Xmx or -XX: MaxRAM option in Java
Get the result of POST in Java
The story of writing Java in Emacs
Discrimination of Enums in Java 7 and above
The story of forgetting to close a file in Java and failing
This and that of the implementation of date judgment within the period in Java
How to change the maximum and maximum number of POST data in Spark
Regarding the transient modifier and serialization in Java
The story of low-level string comparison in Java
[Java] Handling of JavaBeans in the method chain
I received the data of the journey (diary application) in Java and visualized it # 001
The story of making ordinary Othello in Java
About the idea of anonymous classes in Java
Be careful if you find SHIFT-JIS in Java
The story of learning Java in the first programming
[Java] Program example to get the maximum and minimum values from an array
I compared the characteristics of Java and .NET
Feel the passage of time even in Java
Basics of threads and Callable in Java [Beginner]
Distinguish between positive and negative numbers in Java
Import files of the same hierarchy in Java
I want to find the MD5 checksum of a file in Java and get the result as a string in hexadecimal notation.
[Android 9.0 Pie Java] Implement setOnTouchListener in the margin of RecyclerView and close the soft keyboard
Find the greatest common divisor and least common multiple of any number of integers in Ruby
Error logging and exception handling that you can't often see in the Java area
[Ruby] Difference between get and post
Get the result of POST in Java
Get the URL of the HTTP redirect destination in Java
Write ABNF in Java and pass the email address
Please note the division (division) of java kotlin Int and Int
[For beginners] DI ~ The basics of DI and DI in Spring ~
The comparison of enums is ==, and equals is good [Java]
Organizing the current state of Java and considering the future
Java language from the perspective of Kotlin and C #
[Java] How to get the maximum value of HashMap
[Java] Get the file in the jar regardless of the environment
I summarized the types and basics of Java exceptions
Set the maximum number of characters in UITextField in RxSwift
Change the storage quality of JPEG images in Java
Find the approximate value of log (1 + x) in Swift
Use of Abstract Class and Interface properly in Java
Summarize the additional elements of the Optional class in Java 9
Shout Java at the heart of technology-Themes and elemental technologies that Java engineers should pursue in 2017-
Was done in the base year of the Java calendar week
[For beginners] Explanation of classes, instances, and statics in Java
[Note] Java Output of the sum of odd and even elements
I didn't understand the behavior of Java Scanner and .nextLine ().
Find the address class and address type from the IP address with Java
JSON in Java and Jackson Part 1 Return JSON from the server
I tried to summarize the basics of kotlin and java
Difference between Java and JavaScript (how to find the average)
Command to check the number and status of Java threads
Java beginners briefly summarized the behavior of Array and ArrayList
Count the number of digits after the decimal point in Java
Correct the character code in Java and read from the URL
Think about the differences between functions and methods (in Java)