- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这是创建幻方的方法的代码。 n
是正方形的长度。它必须看起来像:
static int[][] magicSquare(int n) {
int[][] square=new int[n][n];
我不明白这个k=(k+1)%n;
尤其是,为什么它是%n
?这不是每次循环都会将 k
变为 1
吗?
for (int i=0; i<n; i++){
in k=i;
for (int j=0; j<n; j++){
square[i][j]=k+1;
k=(k+1)%n;
1 2 3 4
2 3 4 1
3 4 1 2
4 1 2 3
最佳答案
Java中的%用于模除。每当应用该运算符时,右侧操作数都会从左侧操作数中减去尽可能多的次数,剩下的就是输出。您可以通过将左侧操作数除以右侧操作数并将余数作为整数来轻松检查它。在 a%b
的情况下,它会像 a - (a/b)*b
。
以下是一些示例:
10 % 4 = 2 // 2*4 = 8 + 2 = 10
10 % 5 = 0 // 2*5 = 10 + 0 = 10
0 % 4 = 0 // result here is 0 thus 0*4 = 0 + 0 = 0
// if you try to extract 4 from 0, you will not succeed and what's left will be returned (which was originally 0 and it's still 0)...
就您而言:
k = (k + 1) % n;
确保 k 的值永远不会超过 4,因此如果它可以被 4 整除,那么它将被除,剩下的将被写在那里。在 k 恰好为 4 的情况下,您会将 0 的值写入 k 中,但由于您总是添加 k + 1
,因此它会写入 1 的值。
对于初学者,我建议打印您感兴趣的值并观察数据如何迁移。在这里,我添加了一些 println 供您了解。运行代码并自行测试。我确实相信事情会变得更加干净。
public static void main(String[] args) {
int n = 4;
int[][] square = new int[n][n];
System.out.println("--------------");
for (int i = 0; i < n; i++) {
int k = i;
System.out.println("Filling magic cube line " + (i + 1) + ". The k variable will start from " + i + "."); // i initial value is 0 so we add 1 to it just to get the proper line number.
for (int j = 0; j < n; j++) {
System.out.println("Filling array index [" + i + "][" + j + "] = " + (k + 1)); // array indexes start from 0 aways and end at array.length - 1, so in case of n = 4, last index in array is 3.
square[i][j] = k + 1; // add 1 to k so the value will be normalized (no 0 entry and last entry should be equal to n).
k = (k + 1) % n; // reset k if it exceeds n value.
}
System.out.println("--------------");
}
Arrays.stream(square).forEach(innerArray -> {
Arrays.stream(innerArray).forEach(System.out::print);
System.out.println();
});
}
您可以随时尝试并重构代码,如下所示:
public static void main(String[] args) {
int n = 4;
int[][] square = new int[n][n];
System.out.println("--------------");
for (int i = 1; i <= n; i++) {
int k = i;
System.out.println("Filling magic cube line " + i + ". The k variable will start from " + i + ".");
for (int j = 0; j < n; j++) {
System.out.println("Filling array index [" + (i - 1) + "][" + (j - 1) + "] = " + k); // array indexes start from 0 aways and end at array.length - 1, so in case of n = 4, last index in array is 3. Subtract both i and j with 1 to get the proper array indexes.
square[i - 1][j - 1] = k;
k = (k + 1) % n; // reset k if it exceeds n value.
}
System.out.println("--------------");
}
Arrays.stream(square).forEach(innerArray -> {
Arrays.stream(innerArray).forEach(System.out::print);
System.out.println();
});
}
请记住,数组的索引从 0 开始,以长度 - 1 结束。在 4 的情况下,第一个索引是 0,最后一个索引是 3。这是两个实现的差异,尝试看看索引和值取决于控制变量i
和j
。
https://www.diffchecker.com/x5lIWi4A
在第一种情况下,i 和 j 都从 0 开始,一直增长,直到它们的值都小于 n,而在第二个例子中,它们从 1 开始,一直增长,直到它们等于 n。我希望现在情况变得更清楚了。干杯
关于java - 魔方代码循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48268946/
1 需求实现 绘制魔方 中基于OpenGL ES 实现了魔方的绘制,实现较复杂,本文基于 Unity3D 实现了 2 ~ 10 阶魔方的整体旋转和局部旋转。 本文完整代码资源
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 1
这是一道算法题。我似乎无法找到一种方法来比较 rubix 立方体中 2 个立方体的相对位置。 我已经在我的程序中对所有 20 个立方体进行了编号。我正在使用他们的坐标系,但现在我想在相对位置对两个立方
如标题所示,我从 Eclipse 和 cmd 命令运行我的 Magic Square 程序时得到了不同的结果。 eclipse 没有意义。 (eclipse中“Wrong number...”这句话的
这与我问过的另一个问题有关,但现在我已经走得更远了。我正在尝试创建一个 Magic Square基于文本文件输入的程序。该程序将读取第一个方 block ,然后为其余方 block 打印出乱码。如果有
我是一名优秀的程序员,十分优秀!