My code does not give errors, however it is not displaying the minimum and maximum values. The code is:
Scanner input = new Scanner(System.in);
int array[] = new int[10];
System.out.println("Enter the numbers now.");
for (int i = 0; i < array.length; i++) {
int next = input.nextInt();
// sentineil that will stop loop when 999 is entered
if (next == 999) {
break;
}
array[i] = next;
// get biggest number
getMaxValue(array);
// get smallest number
getMinValue(array);
}
System.out.println("These are the numbers you have entered.");
printArray(array);
// getting the maximum value
public static int getMaxValue(int[] array) {
int maxValue = array[0];
for (int i = 1; i < array.length; i++) {
if (array[i] > maxValue) {
maxValue = array[i];
}
}
return maxValue;
}
// getting the miniumum value
public static int getMinValue(int[] array) {
int minValue = array[0];
for (int i = 1; i < array.length; i++) {
if (array[i] < minValue) {
minValue = array[i];
}
}
return minValue;
}
//this method prints the elements in an array......
//if this case is true, then that's enough to prove to you that the user input has //been stored in an array!!!!!!!
public static void printArray(int arr[]) {
int n = arr.length;
for (int i = 0; i < n; i++) {
System.out.print(arr[i] + " ");
}
}
Do I need a system.out.println() to display it, or should the return work?
I have updated your same code please compare code with your's original code :
Here you haven't print the max and min values. Print the max and min values in the getMaxVal and getMin val methods or after the call. This is the output.
You are calling the methods but not using the returned values.
Imho one of the simplest Solutions is: -
You are doing two mistakes here.
1. calling
getMaxValue(),getMinValue()
methods before array initialization completes.2.Not storing return value returned by the
getMaxValue(),getMinValue()
methods.So try this code