In: Computer Science
(Intro/Basic) JAVA
Write a small program that gets some numbers as command-line arguments and finds the maximum of those numbers. You can assume that the user will provide some command-line arguments when running the program (so you don’t have to worry about what to do if the user doesn’t provide any arguments -- that won’t happen). Also, you can assume that all the command line arguments will be floating-point numbers, i.e., numbers with a decimal point, like 2.95.
Source code of the program and its working are given below.Comments are also given along with the code for better understanding.Screen shot of the code and output are also attached.If find any difficulty, feel free to ask in comment section. Please do upvote the answer.Thank you.
Working of the code
Source code
public class Main { public static void main(String[] args) { //declaring a double variable max //initializing max with the first value passed(args[0]) double max = Double.parseDouble(args[0]); //for loop iterate through each value passed by command line for (int i = 1; i < args.length; i++) { //parsing string value into double double number = Double.parseDouble(args[i]); //checking if number is greater than max if (number > max) //if yes set number as new max max = number; } //printing result System.out.println("Maximum value among entered numbers: " + max); } }
Screen shot of the code
Screen shot of the output