【问题标题】:user enters 10 numbers into an array how would I write code to find that number and output what position it is in the array?用户在数组中输入 10 个数字我将如何编写代码来查找该数字并输出它在数组中的位置?
【发布时间】:2016-02-28 01:17:29
【问题描述】:

对 Java 中的数组非常陌生,但我要解决的问题是:

"在你的main方法中,提示用户输入十个数字并存储 他们在一个数组中。编写一个名为 find 的方法,返回位置 数组中特定数字的第一次出现。如果 number 不在列表中,您的方法应该返回 -1。从主要, 提示用户输入数字,调用 find 方法,并显示 从 main 给用户的结果。如果方法返回-1,通知 该号码不在列表中的用户。不要与 find 方法中的用户。您的方法的签名必须是: public static int find(int[] arr, int thingToFind)"

输出示例:

 Enter ten numbers: 18 14 82 17 2 14 6 2 18 4
 What number would you like me to find? 2
 2 first occurs in the 5th place in the list.

 Enter ten numbers: 4 19 0 41 ­2 ­7 7 14 41 100
 What number would you like me to find? 99
 99 is not in the list.

我知道我需要一个循环才能在 find 方法中,但不确定是哪一个,我正在考虑一个 for 循环,但不知道如何实现它。这是我现在的代码,是的,我知道我错过了很多,这只是它的开始。不知道从这里去哪里。

   public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    System.out.println("Enter 10 numbers ");
    int n = in.nextInt();
    int arr[] = new int[n];
  }

   public static int find(int[] arr, int thingToFind){

     }

   }

【问题讨论】:

    标签: java


    【解决方案1】:

    这将返回 thingToFind 的索引,如果未找到 thingToFind,则返回 -1。

     public static int find(int[] arr, int thingToFind){
      int i;
      for(i = 0; i < arr.length(); i++){
        if(arr[i] == thingToFind){
          return i;
        }
      }
      return -1;
    }
    

    【讨论】:

      【解决方案2】:
      void find(int[]arr, int tofind){
         for (int i: arr){
              if (arr[i]==toFind){
                 System.out.println(tofind+" first occurs at "+i+"th place in the array")
              return
         System.out.println("not found")
      

      } }

      【讨论】:

        【解决方案3】:

        你还有一段路要走。你知道你有十个数字所以你可以写

        int arr[] = new int[10];
        

        在您阅读任何数字之前。 此外,您将需要一个循环来读取您的数字。你可以写

        arr[0] = in.nextInt();
        arr[1] = in.nextInt();
        arr[2] = in.nextInt();
        

        等等,但你可以做得更好,对吧?

        【讨论】:

          【解决方案4】:

          我认为你的方法应该是这样的

            public static int find(int[] arr, int thingToFind){
          
          
                  for(int i = 0;i<arr.length;i++){
                      if(arr[i]==thingToFind){
                          return i+1;
                      }
                  }
                  return -1;
          
              }
          

          【讨论】:

            【解决方案5】:

            正如您所说,您将需要一个 for 循环,因为您可以使用索引(通常为 int i = 0)来访问您正在迭代的数组的元素。在每个循环中,检查 i 处的元素是否是您要查找的数字。如果是,则返回 i。循环结束后,返回-1,因为如果你没有找到数字,你只会到达这部分。

            你可能需要的一些东西:

            • arr.length 是数组的大小
            • arr[i] 是 i 位置的元素

            【讨论】:

              【解决方案6】:

              我更喜欢避免使用单行函数!

              返回值在数组中的索引,如果没有找到则返回 -1 或者为空数组 input[1]

              public static int find(int[] arr, int tofind) {
                   return ArrayUtils.indexOf(arr, tofind);
              }
              

              [1]http://commons.apache.org/proper/commons-lang/javadocs/api-release/org/apache/commons/lang3/ArrayUtils.html#indexOf-int:A-int-

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 2021-01-19
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2018-11-29
                • 1970-01-01
                相关资源
                最近更新 更多