How do I get the max and min values from a set of numbers entered?
Emily Wong
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); 10 8 Answers
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)
2You 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.
6It is better
public class Main { public static void main(String[] args) { System.out.print("Enter numbers: "); Scanner input = new Scanner(System.in); double max = Double.MIN_VALUE; double min = Double.MAX_VALUE; while (true) { if ( !input.hasNextDouble()) break; Double num = input.nextDouble(); min = Math.min(min, num); max = Math.max(max, num); } System.out.println("Max is: " + max); System.out.println("Min is: " + min); }
} //for excluding zero
public class SmallestInt { public static void main(String[] args) { Scanner input= new Scanner(System.in); System.out.println("enter number"); int val=input.nextInt(); int min=val; //String notNull; while(input.hasNextInt()==true) { val=input.nextInt(); if(val<min) min=val; } System.out.println("min is: "+min); }
} This is what I did and it works try and play around with it. It calculates total,avarage,minimum and maximum.
public static void main(String[] args) { int[] score= {56,90,89,99,59,67}; double avg; int sum=0; int maxValue=0; int minValue=100; for(int i=0;i<6;i++){ sum=sum+score[i]; if(score[i]<minValue){ minValue=score[i]; } if(score[i]>maxValue){ maxValue=score[i]; } } avg=sum/6.0;
System.out.print("Max: "+maxValue+"," +" Min: "+minValue+","+" Avarage: "+avg+","+" Sum: "+sum);} } here you need to skip int 0 like following:
val = s.nextInt(); if ((val < min) && (val!=0)) { min = val; } System.out.print("Enter a Value: ");
val = s.nextInt();This line is placed in last.The whole code is as follows:-
public static void main(String[] args){ 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) { if (val < min) { min = val; } if (val > max) { max = val; } System.out.print("Enter a Value: "); val = s.nextInt(); } System.out.println("Min: " + min); System.out.println("Max: " + max);
} I tried to optimize solution by handling user input exceptions.
public class Solution { private static Integer TERMINATION_VALUE = 0; public static void main(String[] args) { Integer value = null; Integer minimum = Integer.MAX_VALUE; Integer maximum = Integer.MIN_VALUE; Scanner scanner = new Scanner(System.in); while (value != TERMINATION_VALUE) { Boolean inputValid = Boolean.TRUE; try { System.out.print("Enter a value: "); value = scanner.nextInt(); } catch (InputMismatchException e) { System.out.println("Value must be greater or equal to " + Integer.MIN_VALUE + " and less or equals to " + Integer.MAX_VALUE ); inputValid = Boolean.FALSE; scanner.next(); } if(Boolean.TRUE.equals(inputValid)){ minimum = Math.min(minimum, value); maximum = Math.max(maximum, value); } } if(TERMINATION_VALUE.equals(minimum) || TERMINATION_VALUE.equals(maximum)){ System.out.println("There is not any valid input."); } else{ System.out.println("Minimum: " + minimum); System.out.println("Maximum: " + maximum); } scanner.close(); }
}