Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/329.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/3/arrays/12.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_Arrays_Modulus - Fatal编程技术网

Java 数组索引的模运算

Java 数组索引的模运算,java,arrays,modulus,Java,Arrays,Modulus,我有一个关于使用按钮切换一些图像的教程,下面是代码 public class MainActivity extends AppCompatActivity { private static ImageView andro; private static Button buttonswitch; int current_image_index = 0; int[] images = {R.mipmap.andro_img,R.mipmap.apple_image,R.mipmap.ic_laun

我有一个关于使用按钮切换一些图像的教程,下面是代码

public class MainActivity extends AppCompatActivity {
private static ImageView andro;
private static Button buttonswitch;

int current_image_index = 0;
int[] images = {R.mipmap.andro_img,R.mipmap.apple_image,R.mipmap.ic_launcher,R.mipmap.ic_launcher_round};
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    buttonClick();
}
public void buttonClick() {
    andro = (ImageView) findViewById(R.id.imageView);
    buttonswitch = (Button) findViewById(R.id.button);
    buttonswitch.setOnClickListener(
            new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    current_image_index++;
                    current_image_index = current_image_index % images.length;
                    andro.setImageResource(images[current_image_index]);
                }
            }
    );
}
}
这一部分我真的很困惑:

 @Override
            public void onClick(View view) {
                current_image_index++;
                current_image_index = current_image_index % images.length;
                andro.setImageResource(images[current_image_index]);
我的理解是,一旦我点击按钮,那么int current_image_索引将增加1。然后用images.length对当前图像索引进行模化,当前图像索引的剩余部分除以image.length。例如,我第一次将当前图像索引设置为0,然后单击后,它将设置为1,然后将当前图像索引设置为%image.length=0。然后andro.setImageResource(images[0])


这将重复一次又一次,因为当前图像索引保持为0。那么,既然当前图像索引%image.length总是给出一个0的结果,那么单击后,图片如何能够不断地变化呢。

当前图像索引%images.length
作为一个模块工作

所以我想我们都同意a
1/2=0r1

在每种编程语言中,模运算意味着简单地取除法的剩余部分,并将其作为运算结果返回

因此
1‰2=1
而不是零

…因为当前_image_索引%image.length将始终给出0的结果

不太正确。

模运算符(
%
)计算两个操作数的值。这是一种重复的减法。事实上,使用
a%b
你会问自己:

如果我重复从
a
中减去
b
,直到该操作不再可行,剩下多少

让我们用
8%3
测试它(因此
a=8
b=3

  • 我能从8中减去3吗?是的,结果是5
  • 我能从5中减去3吗?是的,结果是2
  • 我能从2中减去3吗没有,所以我们的最终结果是2

从逻辑上讲,带有结果的操作
a%b
总是导致
0,您可以说“它将是1,然后是当前图像索引%image.length=0”。这是错误的<代码>图像。长度
为4,因此
当前图像索引
0给出0,1给出1,2给出2,3给出3,然后4给出0,这样你就得到了你的换行符。它增加了图像计数器。而不是使用条件来检查你是否通过了,一旦你通过了结束,模值会自动重置为零。基本数学-
a%b
的答案总是
我想你不明白模在做什么。例如,模5的1是1,而不是0。@KenY-N为什么1给出1?二等于二?等等模不是除法的余数吗?