简体   繁体   English

用户在数组中输入 10 个数字我将如何编写代码来查找该数字并输出它在数组中的位置?

[英]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?

Very new to arrays in Java but the problem I was given to solve is this: Java 中的数组非常新,但我要解决的问题是:

"In your main method, prompt the user to enter ten numbers and store them in an array. Write a method called find that returns the position of the first occurrence of a particular number in the array. If the number is not in the list, your method should return ‐1. From main, prompt the user to enter a number, call the find method, and display the result to the user from main. If the method returned ‐1, inform the user that the number was not in the list. Do not interact with the user from within the findmethod. The signature of your method must be: public static int find(int[] arr, int thingToFind)" “在你的 main 方法中,提示用户输入十个数字并将它们存储在一个数组中。编写一个名为 find 的方法,该方法返回特定数字在数组中第一次出现的位置。如果该数字不在列表中,你的方法应该返回-1。从main,提示用户输入一个数字,调用find方法,并将结果从main显示给用户。如果方法返回-1,通知用户该数字不在列表。不要在 findmethod 中与用户交互。你的方法的签名必须是:public static int find(int[] arr, int thingToFind)”

Output example:输出示例:

 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.

I know I need a loop to be in the find method but not sure which one, I'm thinking a for loop but not sure how to implement it.我知道我需要在 find 方法中使用一个循环,但不确定是哪个循环,我正在考虑使用 for 循环但不确定如何实现它。 Here is my code as of now, yes I know I'm missing a lot, and it's just the start of it.这是我现在的代码,是的,我知道我错过了很多,而这只是它的开始。 Not sure where to go from here.不知道从这里去哪里。

   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){

     }

   }

This will return the index of thingToFind, or -1 if thingToFind is not found.这将返回 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;
}
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")

} } } }

You have a way to go yet.你还有一段路要走。 You know that you have ten numbers so you can write你知道你有十个数字所以你可以写

int arr[] = new int[10];

before you read in any number.在您阅读任何数字之前。 Also, you will need a loop to to read in your numbers.此外,您将需要一个循环来读取您的数字。 You could just write你可以写

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

and so on, but you can do better than that, right?等等,但你可以做得更好,对吧?

You will need a for loop as you said, because you can use the index (normally int i = 0 ) to acces the elements of the array which you are iterating over.正如您所说,您将需要一个 for 循环,因为您可以使用索引(通常为int i = 0 )来访问您正在迭代的数组元素。 In each loop, check if the element at i is the number you are looking for.在每个循环中,检查 i 处的元素是否是您要查找的数字。 If yes, return i.如果是,则返回 i。 After the loop, return -1, since you will only reach this part if you didn't find the number.循环后,返回-1,因为只有在没有找到数字的情况下才会到达这部分。

Some things you may need:您可能需要的一些东西:

  • arr.length is the size of your array arr.length是数组的大小
  • arr[i] is the element at the position i arr[i]是位置 i 的元素

I think your method should look something like this我认为你的方法应该是这样的

  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;

    }

I prefer avoiding one line function!我更喜欢避免单行功能!

Returns the index of the value within the array, returns -1 if not found or null array input[1]返回数组中值的索引,如果未找到则返回 -1 或 null 数组 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- [1] http://commons.apache.org/proper/commons-lang/javadocs/api-release/org/apache/commons/lang3/ArrayUtils.html#indexOf-int:A-int-

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我如何从不同的数组中找到与数组的最大数量相关的索引? - How would I find what Index the largest number of an array correlates to from a different array? 我如何找到此数组索引的位置以及最小值? - How would i find the position of this array index and also the minimum? 我如何找到数组中的第二大数字? - How would I find the next largest number in an Array? 如何显示数组中元素的位置? - How would I display the position of an element in an array? 使用代码查找 10 个随机整数数组的平均值时遇到问题 - Having trouble with code to find average of an array of 10 random whole numbers 如何确保用户仅输入0到10之间的数字? - How do I make sure that the user enters a number between 0 and 10 only? 我将如何遍历数组以查看用户输入变量(数字)是否等于数组中的一个元素并用“@”替换该元素 - how would i loop through an array to see if the user input variable(number) is equal to a element in the array and replace that element with a “@” 我如何从使用此2D数组的用户那里获得列号和行号? - How would I get the column number and line number from a user using this 2D array? 查找数组中最大的连续数字和 output 数字以及有多少个 - Find largest consecutive numbers in array and output numbers and how many there is Java-如果用户输入无效的输入,如何返回异常? - Java - How would I return an exception if the user enters a invalid input?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM