gpt4 book ai didi

java - 我已经能够编写一个代码来打印算术级数,但我需要有关如何在二维数组中打印它的帮助

转载 作者:行者123 更新时间:2023-12-01 04:57:18 26 4
gpt4 key购买 nike

我被要求编写一个代码来打印二维数组中的算术维度。我已经编写了打印算术级数的代码,但我不知道如何在二维数组中打印它。这是我的代码,任何帮助将不胜感激

package task3;

import java.util.Scanner;

public class ArithmeticProgression {
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);

double firstTerm = 0;
double numberOfTerms = 0;
double nthTerm = 0;
double commonDifference = 0;
double sum= 0;
double term = 0;

System.out.print("Enter the value of a (First Term) : ");
firstTerm = input.nextDouble();

System.out.print("Enter the value of d (Common Difference) : ");
commonDifference = input.nextDouble();

System.out.print("Enter the value of n (Number of terms) : ");
numberOfTerms = input.nextDouble();

nthTerm = firstTerm + (numberOfTerms - 1) * commonDifference;

sum = numberOfTerms * (2 * firstTerm + (numberOfTerms - 1) * commonDifference)/2;

System.out.println("");
System.out.println("The Arithmetic Progression is as follows :");

for(int i = 0; i < numberOfTerms; i++){
term = firstTerm + i * commonDifference;
System.out.print(term+" + ");
}

System.out.println("...");
System.out.println("The nthTerm of the series : " + nthTerm);
System.out.println("The Sum of n terms of series : " + sum);
}
}

最佳答案

如果不指定二维数组中行和列之间的关系,这种级数更适合一维数组。在这种情况下,您可以使用:

double[] terms = new double[(int)numberOfTerms];
for (int i = 0; i < numberOfTerms; i++) {
terms[i] = firstTerm + i * commonDifference;
}

System.out.println(Arrays.toString(terms));

关于java - 我已经能够编写一个代码来打印算术级数,但我需要有关如何在二维数组中打印它的帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13891912/

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