- #1
Hiche
- 84
- 0
Homework Statement
Write a program that reads a number N from the command line and asks the user to enter N integers. The program should compute their minimum and maximum, and prints them out.
Homework Equations
..
The Attempt at a Solution
Code:
public class MinMax
{
public static void main(String[] args)
{
int N = Integer.parseInt(args[0]);
int userInput = 0;
int maximum = 0;
int minimum = ?;
for (int i = 1; i <= N; i++)
{
StdOut.print("Enter integer " + i + ": ");
userInput = StdIn.readInt();
if (userInput > maximum)
maximum = Math.max(maximum, userInput);
if (userInput < minimum)
minimum = Math.min(minimum, userInput);
}
StdOut.println(maximum + " " + minimum);
}
}
The question mark is where I need help. If I input a large number, it works. But is it the only way? We are limited to using the standard input command StdIn. This is an easy question I know, but I was just looking for an alternative.