Java 如何在数组中搜索并从方法返回?

Java 如何在数组中搜索并从方法返回?,java,arrays,function,Java,Arrays,Function,我必须写一个程序,用1到100之间的随机数填充数组。用户将输入数组的大小。把数组填满。然后用户将输入他们要查找的数字,程序将返回该数字在数组中的位置,如果该数字不在数组中,则返回-1。继续执行此操作,直到使用输入-1 这就是我到目前为止所做的: Scanner kb = new Scanner (System.in); System.out.println("Please enter row size"); int row = kb.nextInt(); Syst

我必须写一个程序,用1到100之间的随机数填充数组。用户将输入数组的大小。把数组填满。然后用户将输入他们要查找的数字,程序将返回该数字在数组中的位置,如果该数字不在数组中,则返回-1。继续执行此操作,直到使用输入-1

这就是我到目前为止所做的:

    Scanner kb = new Scanner (System.in);
    System.out.println("Please enter row size");
    int row = kb.nextInt();
    System.out.println("Please enter column size");
    int column = kb.nextInt();

    int [][] Array = new int [row][column];

    System.out.println();
    for ( int r = 0; r < Array.length; r++)
    {
        for (int c = 0; c < Array[r].length ; c++)
        {
            Array [r][c] = (int) (Math.random()*99+1);
            System.out.print(Array[r][c]+ " ");
        }
        System.out.println();
        System.out.println();
    }

    System.out.println("Please input a number to look for or enter \"-1\" to stop ");
    int find = kb.nextInt();

    findValue (Array, find);

}
    public static int findValue (int [][] Array, int find)
    {
        if (find !=-1)
            {
                for ( int x = 0; x < Array.length; x++)
                {
                    for (int y = 0; y < Array[x].length ; y++)
                    {
                        if (Array[x][y] == find)
                        {       
                            System.out.println("The number is located in the "+ (x+1) + " row, and the " + (y+1) + " column");
                        }

                    }
                }

            }

        }   
    }
}
Scanner kb=新的扫描仪(System.in);
System.out.println(“请输入行大小”);
int row=kb.nextInt();
System.out.println(“请输入列大小”);
int column=kb.nextInt();
int[][]数组=新的int[行][列];
System.out.println();
for(int r=0;r

但是,如果用户请求的数字不在数组中,我无法确定如何返回“-1”。。此外,我不知道如何不断地询问“请输入一个数字…”,并在用户输入“-1”时停止。注意:
返回的工作方式是,您的函数在任何调用它的地方都“给出”一个值。例如,在我的代码中,函数返回一个整数数组,您可以使用

int[] positionInArray = findValue(Array, find);
第1部分:返回-1

在数组中找到值后,只需返回

if (Array[x][y] == find){
    return {x,y}
}
但是,由于您使用的是2D数组(为什么?它没有在提示中指定)。因此,您必须将函数的返回值更改为
int[]
,以便可以同时返回两个坐标

public static int[] findValue (int [][] Array, int find)
然后,如果到达
循环的
末尾,则意味着未找到匹配项,因此在末尾添加一个return语句(您的IDE应该对此表示不满:您声明了一个返回类型
int
,但从未返回任何内容):


第二部分:继续问

使用
do while
循环

do {
    find = kb.nextInt();
    if (find != -1){
        int[] position = findValue(Array, find);
        //print position, see above
    }
}while (find != -1);
while (find != -1){
    //get input, find value, etc
}
或者一个简单的
while
循环

do {
    find = kb.nextInt();
    if (find != -1){
        int[] position = findValue(Array, find);
        //print position, see above
    }
}while (find != -1);
while (find != -1){
    //get input, find value, etc
}
但是,如果用户请求的数字不在数组中,我无法确定如何返回“-1”

使用
for
循环检查数字是否与数组中的任何内容匹配,如下所示:

for(int i = 0; i < array[].length; i++){
    for(int j = 0; j < array[i][].length; j++){
        if(in != array[i][j]){
            // return -1
        }
    }
}

(再次假设您的整数是
中的

您的代码几乎正确,只是缺少一些细节

public static void main(String[] args) {

    Scanner kb = new Scanner(System.in);
    System.out.println("Please enter row size");
    int row = kb.nextInt();
    System.out.println("Please enter column size");
    int column = kb.nextInt();

    int[][] Array = new int[row][column];

    System.out.println();
    for (int r = 0; r < Array.length; r++) {
        for (int c = 0; c < Array[r].length; c++) {
            Array[r][c] = (int) (Math.random() * 99 + 1);
            System.out.print(Array[r][c] + " ");
        }
        System.out.println();
        System.out.println();
    }

    int find;
    do {
        System.out.println("Please input a number to look for or enter \"-1\" to stop ");
        find = kb.nextInt();

        if (find != -1) {

            int[] coordinates = findValue(Array, find);

            if (coordinates[0] == -1 && coordinates[1] == -1) {
                System.out.println("The number could not be found");                    
            } else {
                System.out.println("The number is located in the " + (coordinates[0]) + " row, and the " + (coordinates[1]) + " column");
            }
        }

    } while (find != -1);
}

public static int[] findValue(int[][] Array, int find) {

    int[] coordinates = new int[2];

    coordinates[0] = -1;
    coordinates[1] = -1;

    for (int x = 0; x < Array.length; x++) {
        for (int y = 0; y < Array[x].length; y++) {
            if (Array[x][y] == find) {

                coordinates[0] = x + 1;
                coordinates[1] = y + 1;

                break;
            }
        }
    }

    return coordinates;
}
publicstaticvoidmain(字符串[]args){
扫描仪kb=新扫描仪(System.in);
System.out.println(“请输入行大小”);
int row=kb.nextInt();
System.out.println(“请输入列大小”);
int column=kb.nextInt();
int[][]数组=新的int[行][列];
System.out.println();
for(int r=0;r
啊,你提前2分钟回答了嗨,如果我的问题很愚蠢,很抱歉,我大约在2个月前开始编程。返回“-1”意味着什么?当您找到它时,返回的是
-1
,而不是当您没有找到它时。至于2分钟前的回答:我知道它的感觉:PHi,你能给我解释一下写“return 1”是什么意思吗?正如你所说,当找不到数字时,你想返回-1,方法“findValue”必须返回一个整数。如果成功,我选择返回1。但是,您可以将1替换为任何其他数字,因为它没有被使用。您甚至可以将该方法更改为“publicstaticvoid..”而不返回任何内容。我只是试着按照你的要求,这真的取决于你:)哦,好的!但是“-1”到哪里去了?我不明白如何使用它。谢谢你的回答,我不知道你想用它做什么。如果您的想法是打印它,您可以将消息“找不到号码”替换为“-1”。如果您不需要它做任何事情,只需删除语句并使方法return void.OP返回整数在数组中的位置。在这里,找到时返回1
public static void main(String[] args) {

    Scanner kb = new Scanner(System.in);
    System.out.println("Please enter row size");
    int row = kb.nextInt();
    System.out.println("Please enter column size");
    int column = kb.nextInt();

    int[][] Array = new int[row][column];

    System.out.println();
    for (int r = 0; r < Array.length; r++) {
        for (int c = 0; c < Array[r].length; c++) {
            Array[r][c] = (int) (Math.random() * 99 + 1);
            System.out.print(Array[r][c] + " ");
        }
        System.out.println();
        System.out.println();
    }

    int find;
    do {
        System.out.println("Please input a number to look for or enter \"-1\" to stop ");
        find = kb.nextInt();

        if (find != -1) {

            int[] coordinates = findValue(Array, find);

            if (coordinates[0] == -1 && coordinates[1] == -1) {
                System.out.println("The number could not be found");                    
            } else {
                System.out.println("The number is located in the " + (coordinates[0]) + " row, and the " + (coordinates[1]) + " column");
            }
        }

    } while (find != -1);
}

public static int[] findValue(int[][] Array, int find) {

    int[] coordinates = new int[2];

    coordinates[0] = -1;
    coordinates[1] = -1;

    for (int x = 0; x < Array.length; x++) {
        for (int y = 0; y < Array[x].length; y++) {
            if (Array[x][y] == find) {

                coordinates[0] = x + 1;
                coordinates[1] = y + 1;

                break;
            }
        }
    }

    return coordinates;
}