【问题标题】:How to search in Arrays and return from a method?如何在数组中搜索并从方法返回?
【发布时间】:2016-01-10 03:01:09
【问题描述】:

我必须编写一个程序,用 1 到 100 之间的随机数填充数组。用户将输入数组的大小。把数组填满。然后用户将输入他们要查找的数字,程序将返回该数字在数组中的位置,如果它不在数组中,则返回 -1。继续这样做,直到 use 进入 -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");
                        }

                    }
                }

            }

        }   
    }
}

但是,如果用户要求的数字不在数组中,我不知道如何返回“-1”。另外,我不知道如何继续询问“请输入一个数字.. ." 并在用户输入“-1”时停止。

【问题讨论】:

    标签: java arrays function


    【解决方案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;
    }
    

    【讨论】:

    • 你好,你能解释一下你写的“return 1”是什么意思吗?
    • 正如你所说,当找不到数字时要返回-1,方法“findValue”必须返回一个整数。如果成功,我选择返回 1。您可以将 1 替换为任何其他数字,因为它没有被使用。您甚至可以将方法更改为“public static void ..”并且不返回任何内容。我只是尝试遵循您的要求,这完全取决于您:)
    • 哦,好的!但是那个“-1”去哪里了?我不明白如何使用它。谢谢你的回答
    • 我不知道你想用它做什么。如果您的想法是打印它,您可以将消息“找不到号码”替换为“-1”。如果您不需要它来做任何事情,只需删除语句并使方法返回 void。
    • OP 要求返回整数在数组中的位置。在这里,找到时返回 1。您必须返回一个带有值的整数数组,并在未找到时返回一个带有 -1 的数组。
    【解决方案2】:

    但是,如果用户要求的数字不在数组中,我不知道如何返回“-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
            }
        }
    }
    

    (当然假设你的整数是in

    另外,我不知道如何继续询问“请输入一个数字...”并在用户输入“-1”时停止。非常感谢您的帮助

    使用do/while 循环,这样你就可以继续做你的事情你的整数不等于-1。

    do {
        // do your stuff here, while i isn't -1
    } while (in != -1);
    

    (再次假设您的整数是in

    【讨论】:

    • 您在找到-1 时返回,而不是在未找到时返回。至于2分钟前的回答:我知道是什么感觉:P
    【解决方案3】:

    注意:return 的工作方式是您的函数“赋予”您调用它的任何位置的值。例如,在我的代码中,该函数返回一个整数数组,您可以使用

    将它分配给一个变量
    int[] positionInArray = findValue(Array, find);
    

    第 1 部分:返回 -1

    在数组中找到值后,你要做的就是return

    if (Array[x][y] == find){
        return {x,y}
    }
    

    但是,由于您使用的是 2D 数组(为什么?它没有在提示中指定。)您必须将函数的返回值更改为 int[],以便您可以返回两个坐标。

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

    然后,如果您到达 for 循环的末尾,则意味着未找到匹配项,因此请在末尾添加一个 return 语句(您的 IDE 应该抱怨这一点:您声明了 @ 的返回类型987654328@但从不返回任何东西):

    for ( int x = 0; x < Array.length; x++){
        for (int y = 0; y < Array[x].length ; y++){
            //...
        }
    }
    return {-1}; //match not found
    

    然后,你必须实际打印它:

    int[] position = findValue(Array, find);
    System.out.println("Found at " + (position[0] + 1) + "," + (position[1] + 1));
    

    第 2 部分:不断询问

    使用do-while 循环

    do {
        find = kb.nextInt();
        if (find != -1){
            int[] position = findValue(Array, find);
            //print position, see above
        }
    }while (find != -1);
    

    或者一个简单的while 循环

    while (find != -1){
        //get input, find value, etc
    }
    

    【讨论】:

    • 啊,你提前 2 分钟回答了
    • 嗨,对不起,如果我的问题很愚蠢,我大约 2 个月前开始编程。返回“-1”是什么意思?
    猜你喜欢
    • 1970-01-01
    • 2023-04-09
    • 1970-01-01
    • 2018-10-10
    • 2018-05-09
    • 2016-01-14
    • 2015-08-27
    • 2015-01-29
    • 2016-06-03
    相关资源
    最近更新 更多