【问题标题】:Java: Searching an unsorted integer array for the first occurrence of a user inputted integerJava:在未排序的整数数组中搜索用户输入整数的第一次出现
【发布时间】:2016-12-10 04:44:14
【问题描述】:

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

  1. 与(我相信)方法“arrayIndex”和“position”如何相互作用。我这样说是因为当我为两个字段输入相同的确切数字时,即使它们应该处理完全不同的任务,它们也会返回相同的确切值。

  2. 使用我的“位置”方法,它应该返回数组中第一次出现的搜索值的位置,它似乎返回了不正确的值,在检查后我仍然无法弄清楚原因。如果用户搜索一个不存在的值位置,它应该返回一个“-1”。

我的完整代码:

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;
    }
}

【问题讨论】:

  • 您似乎根本没有使用position 方法。
  • @4castle 对不起,你是什么意思?
  • 您的代码从不调用position 方法。

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


【解决方案1】:

在您的 main 方法中,当您想获得 i 作为计数时,您实际上获得的是 arrayIndex() 而不是 position()。 只需将int i = arrayIndex(tmax); 更改为int i = position(tmax); 并去掉int match() 作为position() 的参数

【讨论】:

  • @Aramza 数组索引为 0,因此您看到的输出是正确的。如果您希望数组为 1 索引,只需将 1 添加到您返回的索引。
  • 数组从 0 而不是 1 开始,因为它们的工作方式。只需在 position() 的返回值上加 1
【解决方案2】:

这里是您的问题的解决方案。您两次调用 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;
}

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-13
    • 1970-01-01
    • 1970-01-01
    • 2016-08-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多