Java定义数组遍历数组后被三整除
时间: 2023-10-24 08:05:39 浏览: 56
java---输出前五个被3整除的数
好的,下面是Java版的定义数组遍历数组后被三整除的方法:
```java
public class Main {
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
System.out.print("Elements in the array that can be divided by 3 are: ");
for (int i = 0; i < arr.length; i++) {
if (arr[i] % 3 == 0) {
System.out.print(arr[i] + " ");
}
}
}
}
```
输出结果为:
```
Elements in the array that can be divided by 3 are: 3 6 9
```
这段代码遍历了整个数组,对于数组中的每个元素,判断它是否能被 3 整除,如果能被整除,就打印出来。
阅读全文