求大佬用JAVA编程计算1^1+2^2+3^3+4^4+5^5+6^6?

2024-11-25 12:54:38
推荐回答(1个)
回答1:

public static void main(String[] args)
{
Count(6);
}

public static int Count(int x)
{
int sum = 0;
for (int i = 1; i <= x; i++)
{
if (i == x)
{
System.out.print(i + "^" + i + " = ");
}
else
{
System.out.print(i + "^" + i + " + ");
}
sum += Math.pow(i,i);
}
System.out.println(sum);
return sum;
}