在二维数组中输入实数,搜索等于并输出它们

时间:2013-06-21 14:43:48

标签: java

我是Java编程的新手,我想要一些帮助。

这是我的问题:我们输入随机实数并希望将它们记录在矩阵中(例如[100] [100]的数组),如果之前输入了这样的数字,我们想要输入的数字如果是这样的话,我们会输出它们,然后输出下一个。只有在之前连续输入数字时。

这是我的代码,但很可能是不正确的

import java.util.Scanner;

class AddAMatrix {
    public static void main(String args[]) {
        int m, n, c, i;
        Scanner in = new Scanner(System.in);
        //input the size of the matrix
        System.out.println("Enter the number of rows and columns of matrix");
        m = in.nextInt();
        n  = in.nextInt();

        int array[][] = new int[m][n];

        System.out.println("Enter number");
        //we input random numbers and want to record them in the matrix, with that numbers we input we want to fing if there are 
        //such a numbers entered before successively and if that is so , we output them and the next one at the sceen . only if the 
        //numbers are successively entered before.
        for (c = in.nextin(); c < m; c++)
            if (array[c][].equals(c))
                System.out.println("number is repeated" + c);
            else System.out.println("enter another number");
        for (d = in.nextin(); d < n ;d++ )
            array[c][d] = in.nextInt();
        if (array[c][].equals(c))
            System.out.println("number is repeated" + c);
        else System.out.println("enter another number");

        if (array[c][d].equals(c, d));
        System.out.println("next number of entered matrix is" + array[c][d]);                  
    }
}

非常感谢。这是有效的,但它显示了输入两次的最后一个数字。我的任务是我们输入大量的数字,例如300或400个数字,而不是我们输入一个例如23,我们取这个数字并在大厅矩阵中四处找到并且我们输出它(23)和之前的数字它是按顺序输入的,只是矩阵的下一个。例如:2,5,7,9,23,32,13,15,19,39,36,......... 3,4, 9,23 输出9 ,23,32这就是这里的问题。我希望你能给我指导,我应该工作。提前谢谢。!!!

1 个答案:

答案 0 :(得分:0)

试试这段代码:

public static void main(String[] args) {
    int m, n, c;
    Scanner in= new Scanner(System.in);
    //input the size of the matrix
    System.out.println("Enter the number of rows and columns of matrix");
    m= in.nextInt();
    n= in.nextInt();

    int array[][]= new int[m][n];

    for (c= 0; c < m; c++) {
        for (int d= 0; d < n; d++) {
            array[c][d]= in.nextInt();
        }
    }

            System.out.println("Enter a number to search for:");
            int queryNum= in.nextInt();

            //search for it
    for (c= 0; c < m; c++) {
        for (int d= 0; d < n; d++) {
            if (array[c][d] == queryNum) {
                if (d == 0) {
                    if (c - 1 >= 0) {
                        System.out.println(array[c-1][n-1]);
                    }
                } else {
                    System.out.println(array[c][d+1]);
                }
                System.out.println(array[c][d]);
                if (d + 1 >= n) {
                    if (c+1 < n) {
                        System.out.println(array[c+1][0]);
                    }
                } else {
                    System.out.println(array[c][d+1]);
                }
            }
        }
    }


    in.close();
}

几位评论:

  • 在for循环中,从0索引迭代到数组的长度:
    • for (c= 0; c < m; c++) {for (int d= 0; d < n; d++) {
  • 您必须像我在最后一行中那样关闭您正在使用的流(Scanner in