P. Icker P. Icker - 8 months ago 66
Java Question

minimum of integers

package variousprograms;
import java.util.*;
public class InputStats
public static void main(String[] args)
Scanner input = new Scanner(System.in);
int a;
int b;
int c;
int d;
int e;

System.out.println("First Integer ");
a = input.nextInt();
System.out.println("Second Integer ");
b = input.nextInt();
System.out.println("Third Integer ");
c = input.nextInt();
System.out.println("Fourth Integer ");
d = input.nextInt();
System.out.println("Fifth Integer ");
e = input.nextInt();

System.out.println("Maximum is " + Math.max(Math.max(Math.max(Math.max(a,b), c), d), e));
System.out.println("Minimum is " + Math.min(Math.min(Math.min(Math.min(a,b), c), d), e));
System.out.println("Mean is " + (a + b + c + d + e)/5.0);

I created a code to find the minimum, maximum, and mean of a set of five integers using five variables for each integer. The problem is that I am supposed to use four variables instead of five, and I cannot use control statements such as if or loop. How should I change the code I already made? Please help! I would greatly appreciate it!


You could do it with variables for the current input, min, max, and total. Just keep re-using the same variable for input, and update the other three variables before you get the next input from the user.

To keep track of the maximum value without if statements, you'll have to do something like:

max = Math.max(max, input);

And something similar for the minimum value.