Java Minimum and Maximum values in Array

前端 未结 13 790
没有蜡笔的小新
没有蜡笔的小新 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:38

    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

       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
      int maxValue = getMaxValue(array);
      System.out.println(maxValue );
    
      // get smallest number
      int minValue = getMinValue(array);
      System.out.println(minValue);
    
    0 讨论(0)
提交回复
热议问题