Below is what I have so far:
I don't know how to exclude 0 as a min number though. The assignment asks for 0 to be the exit number so I need to have the lowest number other than 0 appear in the min string. Any ideas?
int min, max;
Scanner s = new Scanner(System.in);
System.out.print("Enter a Value: ");
int val = s.nextInt();
min = max = val;
while (val != 0) {
System.out.print("Enter a Value: ");
val = s.nextInt();
if (val < min) {
min = val;
}
if (val > max) {
max = val;
}
};
System.out.println("Min: " + min);
System.out.println("Max: " + max);
Here's a possible solution:
public class NumInput {
public static void main(String [] args) {
int min = Integer.MAX_VALUE;
int max = Integer.MIN_VALUE;
Scanner s = new Scanner(System.in);
while (true) {
System.out.print("Enter a Value: ");
int val = s.nextInt();
if (val == 0) {
break;
}
if (val < min) {
min = val;
}
if (val > max) {
max = val;
}
}
System.out.println("min: " + min);
System.out.println("max: " + max);
}
}
(not sure about using int or double thought)
You just need to keep track of a max value like this:
int maxValue = 0;
Then as you iterate through the numbers, keep setting the maxValue to the next value if it is greater than the maxValue:
if (value > maxValue) {
maxValue = value;
}
Repeat in the opposite direction for minValue.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With