Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/362.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 是否有必要缩短if-else语句而不是绕圈 我有一只猫跑过屏幕,停下来在屏幕中间划痕两次。我当前的代码看起来像 private void scratch(){ for (int i = xPos; i < getWidth(); i+=0) { xPos = i; // swap images if (currentImage == nekoPics[0]) currentImage = nekoPics[2]; else if (currentImage == nekoPics[2]) currentImage = nekoPics[4]; else if (currentImage == nekoPics[4]) currentImage = nekoPics[5]; else if (currentImage == nekoPics[5]) currentImage = nekoPics[4]; else if (currentImage == nekoPics[4]) currentImage = nekoPics[5]; else currentImage = nekoPics[0] private void scratch(){ 对于(int i=xPos;i_Java_If Statement_Statements - Fatal编程技术网

Java 是否有必要缩短if-else语句而不是绕圈 我有一只猫跑过屏幕,停下来在屏幕中间划痕两次。我当前的代码看起来像 private void scratch(){ for (int i = xPos; i < getWidth(); i+=0) { xPos = i; // swap images if (currentImage == nekoPics[0]) currentImage = nekoPics[2]; else if (currentImage == nekoPics[2]) currentImage = nekoPics[4]; else if (currentImage == nekoPics[4]) currentImage = nekoPics[5]; else if (currentImage == nekoPics[5]) currentImage = nekoPics[4]; else if (currentImage == nekoPics[4]) currentImage = nekoPics[5]; else currentImage = nekoPics[0] private void scratch(){ 对于(int i=xPos;i

Java 是否有必要缩短if-else语句而不是绕圈 我有一只猫跑过屏幕,停下来在屏幕中间划痕两次。我当前的代码看起来像 private void scratch(){ for (int i = xPos; i < getWidth(); i+=0) { xPos = i; // swap images if (currentImage == nekoPics[0]) currentImage = nekoPics[2]; else if (currentImage == nekoPics[2]) currentImage = nekoPics[4]; else if (currentImage == nekoPics[4]) currentImage = nekoPics[5]; else if (currentImage == nekoPics[5]) currentImage = nekoPics[4]; else if (currentImage == nekoPics[4]) currentImage = nekoPics[5]; else currentImage = nekoPics[0] private void scratch(){ 对于(int i=xPos;i,java,if-statement,statements,Java,If Statement,Statements,有没有一种更简单的方法来做if-else语句,而不是像这样把它们围成一个大圆圈 提前谢谢 (注:我想你可以用某种计数器来做这件事,但我不太确定该怎么做,非常感谢任何帮助)你可以保留当前图像的索引,并在每次迭代时增加它,例如: currentImage = nekoPics[currentIndex%6]; currentIndex++; 或 这需要根据动画的顺序对nekoPics中的图像进行排序。如果你阻止那只猫出现在你的屏幕前,可能会更容易编码 不过,说真的,你可以通过制作一个对象来定义你的

有没有一种更简单的方法来做if-else语句,而不是像这样把它们围成一个大圆圈

提前谢谢
(注:我想你可以用某种计数器来做这件事,但我不太确定该怎么做,非常感谢任何帮助)

你可以保留当前图像的索引,并在每次迭代时增加它,例如:

currentImage = nekoPics[currentIndex%6];
currentIndex++;


这需要根据动画的顺序对nekoPics中的图像进行排序。

如果你阻止那只猫出现在你的屏幕前,可能会更容易编码


不过,说真的,你可以通过制作一个对象来定义你的图片序列来解决这个问题。

除了其他地方建议的地图之外,你还可以使用一个数组;你需要跟踪当前图像的索引:

int[5] nextImageList
  = { 2, ?, 4, 5, 4 }

next = nextImageList[currentImageIndex];
currentImage = nekoPics[next];
currentImageIndex = next;

初始化currentImage和currentImageIndex后不需要“如果”。我不确定1是否在任何地方都是有效的索引,如果不是,任何东西都可以放入数组中的1插槽。

我将发布一个类似于的答案,使用数组。我认为这是最简单的解决方案

然而,他的回答在数组维度上有一个小小的错误。为了完整性,我发布了这篇文章,但应该归功于他

// Elsewhere, in your initialization:
int currentImageIndex = 0; // Assuming [0] is your first image.
int[] nextImageList = { 2, -1, 4, -1, 5, 4 };
// Given the current index, this array will direct you
// to the next image index. Those -1 are unknown (to us).
// Set them to the values you need.

private void scratch() {
    for (int i = xPos; i < getWidth(); ) {
        xPos = i;

        // Swap images.
        currentImageIndex = nextImageList[currentImageIndex];
        currentImage = nekoPics[currentImageIndex];

        // What else you were doing here.
    }
}
//在初始化过程中的其他地方:
int currentImageIndex=0;//假设[0]是您的第一个图像。
int[]nextImageList={2,-1,4,-1,5,4};
//给定当前索引,此数组将指导您
//到下一个图像索引。那些-1是未知的(对我们来说)。
//将它们设置为所需的值。
私人文件{
对于(int i=xPos;i
你可以使用
开关盒
。i+=0是什么意思???第5个
if
将始终计算为false,因为你在第3个
if
中检查了它。试试字典,而不是if/else简单地访问dict中的位置。这样做了吗,解决了吗?这是一种方法,我如何让它停止运行并让o有一种方法开始运行,这样猫在抓痕后会再次运行到屏幕的末尾?基本上,它使用公共的void moveIn开始运行,然后开始抓痕,然后我有一个moveOut方法也可以让它运行到末尾,但我无法在完成抓痕后启动这可能是初学者最简单的答案s、 我建议进行编辑,因为数组由(至少)6个插槽组成,而不是5个插槽。第三个插槽也不知道。谢谢你帮我解决了这个问题,干杯
// Elsewhere, in your initialization:
int currentImageIndex = 0; // Assuming [0] is your first image.
int[] nextImageList = { 2, -1, 4, -1, 5, 4 };
// Given the current index, this array will direct you
// to the next image index. Those -1 are unknown (to us).
// Set them to the values you need.

private void scratch() {
    for (int i = xPos; i < getWidth(); ) {
        xPos = i;

        // Swap images.
        currentImageIndex = nextImageList[currentImageIndex];
        currentImage = nekoPics[currentImageIndex];

        // What else you were doing here.
    }
}