gpt4 book ai didi

Java 在循环中选择变量

转载 作者:太空宇宙 更新时间:2023-11-04 07:07:45 25 4
gpt4 key购买 nike

我想知道是否有更有效的方法来在循环中的变量之间进行选择。我所拥有的以下代码可以工作,但如果可能的话,我希望有更好的方法。

Map<Character, Character> axes = new HashMap<Character, Character>();

(...)

for (int w = 0; w < image.getWidth(); w++) {
for (int h = 0; h < image.getHeight(); h++) {
for (int d = 0; d < depth; d++) {
int x = axes.get('x') == 'w' ? w : (axes.get('x') == 'h' ? h : d);
int y = axes.get('y') == 'w' ? w : (axes.get('y') == 'h' ? h : d);
int z = axes.get('z') == 'w' ? w : (axes.get('z') == 'h' ? h : d);

(...)

}
}
}

在上面的内容中,我需要将图像的某些坐标分配给具有深度的 3D 坐标,但它使用的边会根据其面向的方向而变化。有没有更快的方法来执行代码而不必进行单独的循环?

最佳答案

您可以尝试如下操作:

int[] coords = new int[3];
int width = image.getWidth();
int height = image.getHeight();
for (coords[0] = 0; coords[0] < width; ++coords[0])
{
for (coords[1] = 0; coords[1] < height; ++coords[1])
{
for (coords[2] = 0; coords[2] < depth; ++coords[2])
{
int x = coords[x_idx];
int y = coords[y_idx];
int z = coords[z_idx];

}
}
}

关于Java 在循环中选择变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21081753/

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