gpt4 book ai didi

java - 数学和数组

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:16:02 25 4
gpt4 key购买 nike

我是 Java 新手,仍在学习中。我费了好大劲想弄清楚如何创建这个程序。我已经尝试了多种方法,现在花了大约 4 个小时试图让它工作,但它仍然不会印出我需要的东西。我需要一个 10x5 数组,前 25 位数字是索引变量的平方,最后 25 位数字是索引乘以 3。它输出的是 5 个数字,重复 10 次以上。但这就像它没有在读取“下一个索引变量”。我得到:0.0, 1.0, 4.0, 9.0, 16.0, 0.0, 1.0, 2.0, 4.0, etc.. 这是我目前所拥有的(请不要打分,我正在尝试很难学这个!):

public class snhu4 {
public static void main(String args[]) {
double alpha[][] = new double [10][5];

for (int col=0; col<=9;col++) {
for (int row=0; row<=4;row++) {
alpha[col][row]= Math.pow(row,2);

System.out.println(alpha[col][row]);
}
}
}
}

最佳答案

我相信您正在寻找这样的东西:

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

for (int i = 0; i < 10; i++) {
for (int j = 0; j < 5; j++) {
int index = (5 * i + j);
if (index < 25) {
alpha[i][j] = (index * index);
System.out.println("index(" + index + ")^2 =" + alpha[i][j]);
} else {
alpha[i][j] = (3 * index);
System.out.println("3*index(" + index + ") = " + alpha[i][j]);
}
}
}
}

关于java - 数学和数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20294855/

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