【问题标题】:Java: Searching through unsorted integer array and returning how many numbers are greaterJava:搜索未排序的整数数组并返回更大的数字
【发布时间】:2016-12-10 03:14:03
【问题描述】:

我一直在尝试创建一种方法来搜索未排序的整数数组,但我遇到了一些我无法解决的问题。我想允许程序的用户输入一个整数值并让程序返回大于搜索的整数值的数字数量。我似乎遇到的问题是处理 main 方法中的变量,使用扫描仪进行搜索,以及如何修复我的循环以进行此运行。

非常感谢您的帮助!

我的代码:

import java.util.Scanner;

public class Prog9ArrayMethods {

    public static void main(String[] args) {
        // Daily high temperatures for Portland Maine Jan 1 - Dec 31 2015
        int[] tmax = {32, 38, 34, 35, 41, 17, 25, 17, 29, 24, 26, 33, 31, 24, 
                      29, 38, 20, 49, 49, 36, 31, 38, 35, 32, 37, 20, 17, 26, 
                      30, 32, 22, 26, 12, 20, 35, 34, 19, 28, 22, 15, 30, 23, 
                      20, 17, 16, 19, 21, 21, 32, 33, 19, 34, 35, 31, 19, 34, 
                      21, 27, 27, 30, 36, 32, 46, 39, 23, 38, 40, 44, 47, 56, 
                      41, 39, 38, 36, 45, 44, 28, 32, 34, 36, 35, 34, 39, 42, 
                      49, 49, 41, 41, 40, 48, 45, 46, 66, 49, 48, 41, 47, 42, 
                      35, 43, 54, 68, 66, 70, 65, 55, 67, 55, 57, 48, 63, 60, 
                      53, 54, 55, 56, 58, 63, 57, 60, 55, 54, 62, 76, 75, 72, 
                      84, 58, 59, 83, 68, 82, 64, 68, 70, 63, 74, 61, 65, 67, 
                      69, 67, 65, 83, 84, 91, 79, 80, 77, 84, 73, 51, 50, 61, 
                      60, 58, 73, 67, 65, 68, 81, 86, 80, 85, 78, 61, 61, 75, 
                      72, 80, 69, 72, 72, 67, 82, 78, 67, 70, 59, 69, 75, 68, 
                      78, 80, 71, 82, 82, 76, 84, 72, 84, 87, 90, 78, 76, 82, 
                      76, 74, 70, 81, 84, 70, 82, 78, 76, 67, 67, 77, 83, 88, 
                      86, 86, 86, 81, 81, 80, 82, 80, 76, 80, 77, 77, 67, 80, 
                      77, 80, 85, 85, 89, 86, 83, 75, 73, 78, 70, 79, 75, 80, 
                      79, 77, 75, 81, 86, 80, 84, 86, 72, 78, 82, 92, 89, 86, 
                      78, 73, 74, 62, 73, 83, 85, 82, 83, 75, 72, 69, 65, 74, 
                      74, 63, 63, 67, 74, 75, 69, 62, 55, 58, 58, 61, 69, 67, 
                      63, 59, 56, 68, 70, 62, 68, 57, 61, 57, 46, 48, 66, 58, 
                      65, 54, 47, 62, 54, 52, 59, 73, 58, 51, 58, 64, 64, 64, 
                      68, 69, 65, 53, 58, 53, 47, 53, 60, 46, 53, 54, 47, 47, 
                      53, 59, 46, 42, 42, 42, 41, 51, 61, 57, 41, 32, 38, 44, 
                      45, 47, 51, 51, 57, 39, 45, 53, 48, 57, 47, 48, 56, 42, 
                      50, 46, 40, 38, 47, 49, 47, 51, 62, 51, 43, 34, 23, 28, 
                      44};

        int max = arrayMax(tmax);
        int min = arrayMin(tmax);
        double average = arrayAverage(tmax);
        int search = arrayIndex(search, tmax); // Not exactly sure how to fix this
        System.out.println("Maximum value is: " + max);
        System.out.println("Minimum value is: " + min);
        System.out.println("Average value is: " + average);
        System.out.println("The number of values above the specified value is: " + search);
    }

    // Returns the maximum value in the array
    public static int arrayMax(int[] a) {
        int max = a[0];

        for (int i = 0; i < a.length; i++)
            if (a[i] > max)
                max = a[i];

        return max; 
    }
    // Returns the minimum value in the array
    public static int arrayMin (int[] a) {
        int min = a[0];    

            for (int i = 0; i < a.length; i++)
                if (a[i] < min)
                    min = a[i];
            return min;
    }
    public static double arrayAverage(int[] a) {  
        int sum = 0; // Why does it double the decimal value with "int sum = a[0]"?
        double average;

            for(int i=0; i < a.length; i++){
                sum = sum + a[i];
            }
            average = (double)sum/a.length;
            return average;
    }
    public static int arrayIndex(int userSearch, int[] a) {
        Scanner user_input = new Scanner( System.in );
        System.out.println("Enter a value to search: ");
        userSearch = user_input.next(); // What else could I use?

        for(int i = 0; i < a.length; i++) {
            if(a[i] > userSearch) {
                return a[i]; // Return a value that represents how many numbers are above the search varaible
            }
        }
        return -1;
    }
}

【问题讨论】:

    标签: java arrays loops methods java.util.scanner


    【解决方案1】:

    我必须重写你的 arrayIndex 方法。在该方法中从用户那里获取输入,或者您可以将输入传递给 arrayIndex 方法。

    只计算大于输入的数字,然后返回结果。

    public static int arrayIndex(int[] a) {
    
        Scanner user_input = new Scanner( System.in );
        System.out.println("Enter a value to search: ");
        int userSearch = user_input.nextInt(); 
    
        int numberOfCount = 0;
    
        for(int i = 0; i < a.length; i++) {
            if(a[i] > userSearch) {
                ++numberOfCount;
            }
        }
        return numberOfCount;
    }
    

    【讨论】:

      【解决方案2】:

      这其实很简单。我会解释的。

      public static int arrayIndex(int userSearch, int[] a) {
              Scanner user_input = new Scanner( System.in );
              int count = 0;//this will track your data
              System.out.println("Enter a value to search: ");
              userSearch = user_input.nextInt(); // Use nextInt()
      
              for(int i = 0; i < a.length; i++) {
                  if(a[i] > userSearch) {
                       count++;//increments when the if statement is true
                  }
              }
              return count;//returns your desired data
          }
      

      【讨论】:

        【解决方案3】:

        你可以改变对arrayIndex的调用:

         int search = arrayIndex( tmax); // no need to pass userSearch:
        

        以下是使用扫描仪读取整数的方法:

        Scanner scanner = new Scanner(System.in);
        int userSearch = scanner.nextInt();
        

        但是等等:还有更多。如果你使用 java 8 流,你的代码可以清理很多:

        // Returns the maximum value in the array
        public static int arrayMax(int[] a) {
            return Arrays.stream(a).max().getAsInt();
        }
        // Returns the minimum value in the array
        public static int arrayMin (int[] a) {
            return Arrays.stream(a).min().getAsInt();
        }
        public static double arrayAverage(int[] a) {
            return Arrays.stream(a).average().getAsDouble();
        }
        
        public static long arrayIndex( int[] a) {
        
            System.out.println("Enter a value to search: ");
            Scanner scanner = new Scanner(System.in);
            int userSearch = scanner.nextInt();
        
            return Arrays.stream(a).filter(n -> n > userSearch).count();
        }
        

        看看和享受吧!

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-04-13
          • 2019-06-26
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多