Java:在第一次出现用户输入的整数时搜索未排序的整数数组

时间:2016-12-10 04:44:15

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

我注意到了两个问题,非常感谢你的帮助!

  1. 用(我相信)方法" arrayIndex"和"位置"正在互相交流。我这样说是因为当我为两个字段输入相同的确切数字时,即使它们应该处理完全不同的任务,它们也会返回相同的确切值。

  2. 以我的"位置"应该返回数组中第一次出现的搜索值的位置的方法似乎返回不正确的值,并且在检查之后我仍然无法找出原因。如果用户搜索不存在的值位置,则应该返回" -1"。

  3. 我的整个代码:

    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 count = arrayIndex(tmax);
            int i = arrayIndex(tmax);
            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: " + count);
            System.out.println("The first occurence of the searched value is: " + i);
        }
    
        // 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;
        }
        // Returns the average value in the array
        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;
        }
        // Returns the number of values greater than the user's indexed values
        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 count = 0;
    
                for(int i = 0; i < a.length; i++) {
                    if(a[i] > userSearch) {
                        ++count;
                    }
                }
                return count;
        }
        public static int position(int [ ] a, int match) {
            Scanner user_input = new Scanner( System.in );
            System.out.println("Enter a value to search: ");
            int userSearch = user_input.nextInt(); 
    
            for (int i = 0; i < a.length; i++)
              {
                   if ( a[i] == userSearch )
                         return i;
              }
             return -1;
        }
    }
    

2 个答案:

答案 0 :(得分:1)

在您的主要方法中,当您希望i成为计数时,您实际上会获得arrayIndex()而不是position()。 只需将int i = arrayIndex(tmax);更改为int i = position(tmax);并删除int match()作为position()的参数

答案 1 :(得分:0)

以下是您的问题的解决方案。您正在调用arrayIndex方法两次而不是调用position方法。我纠正了你的代码。现在它工作正常。

import java.util.Scanner;

公共类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 count = arrayIndex(tmax);
    int i = position(tmax);
    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: " + count);
    System.out.println("The first occurence of the searched value is: " + i);
}

// 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;
}
// Returns the average value in the array
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;
}
// Returns the number of values greater than the user's indexed values
public static int arrayIndex(int[] a) {

    Scanner user_input = new Scanner( System.in );
    System.out.println("Enter a value to search for arrayIndex: ");
    int userSearch = user_input.nextInt(); 

    int count = 0;

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

    for (int i = 0; i < a.length; i++)
      {
           if ( a[i] == userSearch )
                 return i;
      }
     return -1;
}

}