Find the maximum and minimum values out of the 5 numbers entered in Java (correction ver)

The other day I posted a program to find the maximum and minimum values from 5 numbers, but since I received advice in the comments, I would like to repost the corrected one. @ LouisS0616 Thank you for your comment ... !!

By the way, I will put the link of the previous article here. https://qiita.com/GARA41679531/items/c81b57a56bfcb28f84a0

What changed

In the previous code, the variables min and max were initialized with 0, so when all the input numbers were positive or negative, either the maximum value or the minimum value was output as 0. To improve that, I assigned the first number entered to the variables min and max, and rewrote the code to check the magnitude relationship from there!

Code description

package max_min;
import java.util.*;
import java.math.*;

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(i==1) {
				min=a;
				max=a;
			}else {
				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.");
		
	}

}

The code above is the actual rewritten code.

Specifically, what I changed is that I am making a program that inputs numbers 5 times using the for syntax, but in that, I used the if syntax to input the first number as the variable min. I am trying to assign it to max.

Execution result

image.png I tried to enter all negative numbers this time, but I was able to display it properly without the maximum value becoming 0 !!

Summary

I realized once again that the improvements are hidden even in the program that I think is perfect! I would appreciate it if you could let me know if there are any improvements in the code I wrote this time! Thank you for reading to the end !!

Recommended Posts

Find the maximum and minimum values out of the 5 numbers entered in Java (correction ver)
Find the maximum and minimum of the five numbers you entered in Java
Find out the list of fonts available in AWS Lambda + Java
[Java] Program example to get the maximum and minimum values from an array
[Java] Get the dates of the past Monday and Sunday in order
Find out the maximum heap size when you specify the -Xmx or -XX: MaxRAM option in Java
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
How to find out the Java version of a compiled class file
How to find the total number of pages when paging in Java
Get the result of POST in Java
The story of writing Java in Emacs
Discrimination of Enums in Java 7 and above
I received the data of the journey (diary application) in Java and visualized it # 001
Sample source code for finding the least common multiple of multiple values in Java
Sample code to get the values of major SQL types in Java + MySQL 8.0
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
The story of making ordinary Othello in Java
About the idea of anonymous classes in Java
The story of learning Java in the first programming
Measure the size of a folder in Java
[Java] The confusing part of String and StringBuilder
I compared the characteristics of Java and .NET
Basics of threads and Callable in Java [Beginner]
Distinguish between positive and negative numbers in Java
I want to find the MD5 checksum of a file in Java and get the result as a string in hexadecimal notation.
Import files of the same hierarchy in Java
[Android 9.0 Pie Java] Implement setOnTouchListener in the margin of RecyclerView and close the soft keyboard
Get the value from the array and find out what number it is included in
Find the greatest common divisor and least common multiple of any number of integers in Ruby
[Java] Cut out a part of the character string with Matcher and regular expression
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] Get the file in the jar regardless of the environment
Java classes and instances to understand in the figure
I summarized the types and basics of Java exceptions
Set the maximum number of characters in UITextField in RxSwift
Reverse Enum constants from strings and values in Java
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
[Java] [Kotlin] Generically call valueOf and values of Enum
Summarize the additional elements of the Optional class in Java 9
I tried to find out what changed in Java 9
I want to find out which version of java the jar file I have is available
Shout Java at the heart of technology-Themes and elemental technologies that Java engineers should pursue in 2017-
Sample code to get the values of major SQL types in Java + Oracle Database 12c