0

我是 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 这是抓住这里。我希望你能给我指明我应该工作的方向。先感谢您 。!!!

4

1 回答 1

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),就像我在最后一行中所做的那样
于 2013-06-21T14:49:52.280 回答