gpt4 book ai didi

java - 切换大于/小于

转载 作者:行者123 更新时间:2023-12-01 12:55:40 33 4
gpt4 key购买 nike

我正在压缩一些代码,现在我有 4 种方法,它们几乎都做同样的事情,除了 for 循环的建模有点不同。我正在传递 int 的 up , down , right ,和left作为这个紧凑方法的参数,这与我以前的 4 个方法一致。

通过为其中一个参数传递 1,为其余参数传递 0,我可以在单个循环中执行不同的操作,只是在切换 < 或 > 符号时遇到问题。

这是我正在尝试使用的代码行:

for (int i = (right*3)+(up*3); i <= (left*3)+(down*3); i= i + (left) + (down) - (up) - (right)) {

一切正常,除了我需要切换 i <= (left...部分为i >= (left...如果right == 1 ,Java中有没有办法在for循环中做到这一点?

如果这是不可能的,我可以制作 2 个不同的 for 循环,我只是不喜欢重复几乎相同的代码。

谢谢!

编辑:这里有 4 种方法:

public boolean moveRight() {
boolean didMove = false;
for (int a = 0; a <= 3; a++) {
for (int i = 3; i >= 0; i--) {
for (int j = 0; j < 4; j++) {
if (i != 3) {
if (valueArray[i + 1][j] == 0 && valueArray[i][j] != 0) {
valueArray[i + 1][j] = valueArray[i][j];
valueArray[i][j] = 0;
didMove = true;
button[i][j].setSize(80, 80);
} else if (valueArray[i + 1][j] == valueArray[i][j] && valueArray[i][j] != 0) {
valueArray[i + 1][j] = valueArray[i][j] * 2;
Score += valueArray[i][j] * 2;
valueArray[i][j] = 0;
didMove = true;
button[i][j].setSize(80, 80);
}
}
}
}
}
return didMove;
}

public boolean moveLeft() {
boolean didMove = false;
for (int a = 0; a <= 3; a++) {
for (int i = 0; i <= 3; i++) {
for (int j = 0; j < 4; j++) {
if (i != 0) {
if (valueArray[i - 1][j] == 0 && valueArray[i][j] != 0) {
valueArray[i - 1][j] = valueArray[i][j];
valueArray[i][j] = 0;
didMove = true;
button[i][j].setSize(80, 80);
} else if (valueArray[i - 1][j] == valueArray[i][j] && valueArray[i][j] != 0) {
valueArray[i - 1][j] = valueArray[i][j] * 2;
Score += valueArray[i][j] * 2;
valueArray[i][j] = 0;
didMove = true;
button[i][j].setSize(80, 80);
}
}
}
}
}
return didMove;
}

public boolean moveDown() {
boolean didMove = false;
for (int a = 0; a <= 3; a++) {
for (int i = 0; i < 4; i++) {
for (int j = 3; j >= 0; j--) {
if (j != 3) {
if (valueArray[i][j + 1] == 0 && valueArray[i][j] != 0) {
valueArray[i][j + 1] = valueArray[i][j];
valueArray[i][j] = 0;
didMove = true;
button[i][j].setSize(80, 80);
} else if (valueArray[i][j + 1] == valueArray[i][j] && valueArray[i][j] != 0) {
valueArray[i][j + 1] = valueArray[i][j] * 2;
Score += valueArray[i][j] * 2;
valueArray[i][j] = 0;
didMove = true;
button[i][j].setSize(80, 80);
}
}
}
}
}
return didMove;
}

public boolean moveUp() {
boolean didMove = false;
for (int a = 0; a <= 3; a++) {
for (int i = 0; i < 4; i++) {
for (int j = 0; j <= 3; j++) {
if (j != 0) {
if (valueArray[i][j - 1] == 0 && valueArray[i][j] != 0) {
valueArray[i][j - 1] = valueArray[i][j];
valueArray[i][j] = 0;
didMove = true;
button[i][j].setSize(80, 80);
} else if (valueArray[i][j - 1] == valueArray[i][j] && valueArray[i][j] != 0) {
valueArray[i][j - 1] = valueArray[i][j] * 2;
Score += valueArray[i][j] * 2;
valueArray[i][j] = 0;
didMove = true;
button[i][j].setSize(80, 80);
}
}
}
}
}
return didMove;
}

最佳答案

数学;将比较两边都乘以 -1,变为 <=进入>= .

int sw = Math.abs(Math.signum(right - 1)) * 2 - 1; // -1 when right == 1 else 1

sw*i <= sw*((left*3)+(down*3))

这是否是更优化的还是加密编程......

关于java - 切换大于/小于,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23922542/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com