我想打印一个大小为n的数组,其中每个元素为pow(i,i),i,范围从1到n。也就是说,如果我输入n = 4,则应该返回一个数组A = {1,4,27,256}。这是因为power(1,1)= 1,power(2,2)= 4,power(3,3)= 27和power(4,4)= 256。
但是,当我尝试运行以下代码时,它没有给出任何输出。
import java.io.*;
import java.util.*;
import java.lang.Math;
public class Main
{
public static void main(String[] args)
{
Main s = new Main();
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
double[] A = new double[n];
int j ;
for(j = 0; j <= n; j++)
{
A[j] = Math.pow(j+1, j+1);
//System.out.println(A[j]); --> 1
}
System.out.print(A);
System.out.println(A); //-->2
for (int i=0; i<A.length; i++)
{
System.out.print(A[i]+" "); // --> 3
}
}
}
当我尝试删除等式1的带引号的引号时,它正在向我输出值。但是方程式2或方程式3都没有帮助我打印阵列。
最佳答案
尝试这个 :-
public static void main(String[] args) {
Main s = new Main();
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
double[] A = new double[n];
int j ;
for(j = 0; j <= n-1; j++)
{
A[j] = Math.pow(j+1, j+1);
//System.out.println(A[j]); --> 1
}
System.out.print(A);
System.out.println(A); //-->2
for (int i=0; i<A.length-1; i++)
{
System.out.print(A[i]+" "); // --> 3
}
}
关于java - 数组未在Java中打印,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60681447/