写一个Java程序,使用for语句将数组int[] arr={2,5,7,8,9,0}中所有元素都乘以2,并将结果存储到一个新的数组中,然后输出新数组中的所有元素
时间: 2023-09-18 07:12:17 浏览: 48
好的,以下是Java程序的代码实现:
```java
public class Main {
public static void main(String[] args) {
int[] arr = {2, 5, 7, 8, 9, 0};
int[] newArr = new int[arr.length];
for (int i = 0; i < arr.length; i++) {
newArr[i] = arr[i] * 2;
}
for (int i = 0; i < newArr.length; i++) {
System.out.print(newArr[i] + " ");
}
}
}
```
程序运行结果为:
```
4 10 14 16 18 0
```
其中,第一个for循环将原数组中的每个元素都乘以2,并存储到新数组中;第二个for循环遍历新数组并输出每个元素的值。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)