gpt4 book ai didi

Java:打印数组元素

转载 作者:行者123 更新时间:2023-12-01 09:56:50 25 4
gpt4 key购买 nike

我需要以每行 10 的增量打印数组元素...

import java.util.*;

public class Array {

public static void main(String args[]) {
double alpha[] = new double[50];

//Initialize the first 25 elements of the array (int i=0; i<25; i++)//
for(int i = 0; i < 25; i++) {
alpha[i]= i * i;
}

//Initialize the last 25 elements of the array (i=25; i<50; i++)//
for(int i = 25; i < 50; i++) {
alpha[i]= 3 * i;
}

//Print the element of the array
System.out.println ( "The values are: " );

for (int i = 0; i < 50; i++) {
System.out.println ( alpha[i] );
}

//Print method to display the element of the array
void print(double m_array[]) {
for(int i =1; i <= m_array.length; i++) {
System.out.print(m_array[i-1] +" ");
if(i%10==0)
System.out.print("\n");
}
}
}
}

我假设这个 if 语句是我的问题...如果这正是我所缺少的,我尝试了 printf 但不起作用...

最佳答案

public class Array {

public static void main(String args[]) {
double alpha[] = new double[50];

// Initialize the first 25 elements of the array (int i=0; i<25; i++)
for (int i = 0; i < 25; i++) {
alpha[i] = i * i;
}

// Initialize the last 25 elements of the array (i=25; i<50; i++)
for (int i = 25; i < 50; i++) {
alpha[i] = 3 * i;
}

System.out.println("The values are: ");

// Print the element of the array
print(alpha);

}

// Print method to display the element of the array
private static void print(double m_array[]) {
for (int i = 1; i <= m_array.length; i++) {
System.out.print(m_array[i-1] + " ");
if (i % 10 == 0) {
System.out.println();
}
}
}
}

输出:

The values are:

0.0 1.0 4.0 9.0 16.0 25.0 36.0 49.0 64.0 81.0

100.0 121.0 144.0 169.0 196.0 225.0 256.0 289.0 324.0 361.0

400.0 441.0 484.0 529.0 576.0 75.0 78.0 81.0 84.0 87.0

90.0 93.0 96.0 99.0 102.0 105.0 108.0 111.0 114.0 117.0

120.0 123.0 126.0 129.0 132.0 135.0 138.0 141.0 144.0 147.0

关于Java:打印数组元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37151701/

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