Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在矩阵中的变量周围添加变量_Java - Fatal编程技术网

Java 如何在矩阵中的变量周围添加变量

Java 如何在矩阵中的变量周围添加变量,java,Java,我需要2,2左右的变量之和,然后打印出来 我不知道怎么做。请帮忙! 这是我目前的代码: import java.util.*; import java.io.*; public class MatrixSumming { private int[][] m = {{5,6},{7,8},{3,4}}; //load in the matrix values public int sum( int r, int c ) { return 0; } public String t

我需要2,2左右的变量之和,然后打印出来

我不知道怎么做。请帮忙! 这是我目前的代码:

import java.util.*;
import java.io.*; 

public class MatrixSumming
{
private int[][] m = {{5,6},{7,8},{3,4}};   //load in the matrix values

public int sum( int r, int c )
{
    return 0;
}

public String toString()
{
    return "";
}
}
这是我的跑步者

import java.io.File;
import java.io.IOException;
import java.util.Scanner;
import static java.lang.System.*;

public class MatrixSummingRunner
{
    public static void main( String args[] ) throws IOException
    {
    //Scanner file = new Scanner (new File("matsum.dat"));
    int[][] mat = {{0, 0, 0, 0, 0, 0, 0},
                {0, 1, 2, 3, 4, 5, 0},
                {0, 6, 7, 8, 9, 0, 0},
                {0, 6, 7, 1, 2, 5, 0},
                {0, 6, 7, 8, 9, 0, 0},
                {0, 5, 4, 3, 2, 1, 0},
                {0, 0, 0, 0, 0, 0, 0}};
}
}

我尝试查找,但在矩阵中找不到与此类似的内容。

定期搜索所需的数字,并将索引V值设为“curi”和“curj”

使用以下逻辑 设maxi和maxj是(mxn)矩阵的m和n

sum=0;

对于(int i=curi-1;i-1&&i,此方法将获得所有周围单元格的值并将其相加

private static int neighboursSum(int[][] grid, int r, int c) {

        int sum = 0;

        for (int i = -1; i <= 1; i++) {
            for (int j = -1; j <= 1; j++) {

                // Make sure that we don't sum the Original Value, we only want it's neighbours
                if (i == 0 && j == 0) {
                    continue;

                } else {

                    int newX = r + i;
                    int newY = c + j;

                    // Make sure that the new Coordinates do not point outside the range of the Array
                    if (newX >= 0 && newX <= grid.length && newY >= 0 && newY <= grid.length) {
                        sum += grid[newX][newY];
                    }
                }
            }
        }
        return sum;
    }
私有静态int-neighboursum(int[][]网格,int-r,int-c){
整数和=0;

对于(inti=-1;i您有一个方法
公共int和(intr,intc)
,其中您传递两个int

这些是行和列索引,对应于矩阵中的一个位置。(或者,如果将行1作为第一行,则必须减去1,因为Java中的索引数组为零)

因此,如果你有一个矩阵和值r和c,你将处于位置
矩阵[r][c]
(或者反过来)

想象一下站在一个棋盘上。现在你周围有8个区域,你可以通过一步到达(或者两个,如果你不能在一步中沿对角线移动)。你可能没有一个或多个区域。在计算总和之前,你必须检查这一点

现在,采取步骤意味着在
r
c
中添加或减去1。因此,通过寻址
matrix[r-1][c]
matrix[r][c+1]
等,您可以在所处字段周围获得其他索引。要沿对角线移动,您必须同时更改
r
c
(采取两个步骤。例如,先向左移动一个,然后向上移动一个。您移动到原始字段左上角对角的字段)


然后,您可以使用这些知识,访问当前字段周围的字段并对其进行汇总。

您能更清楚地说明您想要实现的目标吗?就像我希望在矩阵中得到2,2作为解释,然后在所有8个方向上添加2,2周围的所有变量,然后打印出来。如果在一个方向上没有值怎么办?(位置0,0)我应该把它放在哪里?我给了你一个方法,只是想一想。提示:这里你在寻找2,2个位置,因此curi=2和curj=2
private static int neighboursSum(int[][] grid, int r, int c) {

        int sum = 0;

        for (int i = -1; i <= 1; i++) {
            for (int j = -1; j <= 1; j++) {

                // Make sure that we don't sum the Original Value, we only want it's neighbours
                if (i == 0 && j == 0) {
                    continue;

                } else {

                    int newX = r + i;
                    int newY = c + j;

                    // Make sure that the new Coordinates do not point outside the range of the Array
                    if (newX >= 0 && newX <= grid.length && newY >= 0 && newY <= grid.length) {
                        sum += grid[newX][newY];
                    }
                }
            }
        }
        return sum;
    }