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[]
Imho one of the simplest Solutions is: -
//MIN NUMBER
Collections.sort(listOfNumbers);
listOfNumbers.get(0);
//MAX NUMBER
Collections.sort(listOfNumbers);
Collections.reverse(listOfNumbers);
listOfNumbers.get(0);
You just throw away Min/Max values:
// get biggest number
getMaxValue(array); // <- getMaxValue returns value, which is ignored
// get smallest number
getMinValue(array); // <- getMinValue returns value, which is ignored as well
You can do something like
...
array[i] = next;
System.out.print("Max value = ");
System.out.println(getMaxValue(array)); // <- Print out getMaxValue value
System.out.print("Min value = ");
System.out.println(getMinValue(array)); // <- Print out getMinValue value
...
your maximum, minimum method is right
but you don't print int to console!
and... maybe better location change (maximum, minimum) methods
now (maximum, minimum) methods in the roop. it is need not.. just need one call
i suggest change this code
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;
}
System.out.println("max Value : " + getMaxValue(array));
System.out.println("min Value : " + getMinValue(array));
System.out.println("These are the numbers you have entered.");
printArray(array);
getMaxValue(array);
// get smallest number
getMinValue(array);
You are calling the methods but not using the returned values.
System.out.println(getMaxValue(array));
System.out.println(getMinValue(array));
//To Find Max and Min value in an array without sorting in java
import java.util.Scanner;
import java.util.*;
public class MaxMin_WoutSort {
public static void main(String args[])
{
int n,max=Integer.MIN_VALUE,min=Integer.MAX_VALUE;
System.out.println("Enter the number of elements: ");
Scanner sc = new Scanner(System.in);
int[] arr = new int[sc.nextInt()]; //U can't say static or dynamic.
//UnWrapping object sc to int value;sc.nextInt()
System.out.println("Enter the elements: ");
for(int i=0;i<arr.length;i++) //Loop for entering values in array
{
int next = sc.nextInt();
arr[i] = next;
}
for(int j=0;j<arr.length;j++)
{
if(arr[j]>max) //Maximum Condition
max = arr[j];
else if(arr[j]<min) //Minimum Condition
min = arr[j];
}
System.out.println("Highest Value in array: " +max);
System.out.println("Smallest Value in array: "+min);
}
}
I have updated your same code please compare code with your's original code :
public class Help {
public static void main(String args[]){
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;
}
System.out.println("These are the numbers you have entered.");
printArray(array);
// get biggest number
System.out.println("Maximum: "+getMaxValue(array));
// get smallest number
System.out.println("Minimum: "+getMinValue(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] + " ");
}
}
}