Java Minimum and Maximum values in Array

前端 未结 13 789
没有蜡笔的小新
没有蜡笔的小新 2020-12-01 06:32

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[]          


        
相关标签:
13条回答
  • 2020-12-01 07:12

    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);
    
    0 讨论(0)
  • 2020-12-01 07:12

    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
    
      ...  
    
    0 讨论(0)
  • 2020-12-01 07:14

    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);
    
    0 讨论(0)
  • 2020-12-01 07:17
    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)); 
    
    0 讨论(0)
  • 2020-12-01 07:21

    //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);
    
     }
    }
    
    0 讨论(0)
  • 2020-12-01 07:22

    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] + " ");
        }
    }
    }
    
    0 讨论(0)
提交回复
热议问题