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.
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.
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.
The execution result is as above. The variables max and min are defined as double, so the end is written in decimal form!
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!
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