我一直在做这个项目,项目运行良好,但它显示"令人困惑的缩进"和"将返回值分配给新变量"警告,我不知道它是关于什么的。这是带有警告的行:
System.out.printf("The sum of the squares: %.0f", tsquee);
以下是完整的项目。谢谢!
double n = 0;
while (n < 25)
{
n = n + 1;
if (n > 25)
break;
System.out.printf("%3.0f%15.2f%16.2f%17.2f%15.2fn", n, Math.cbrt(n), Math.sqrt(n), Math.pow(n, 3), Math.pow(n, 2));
}
{
double tsquee = 0.0, tsqueer = 0.0;
int csq = 0, ccube = 0;
for (n = 0; n <= 25; n++)
tsquee += Math.pow(n, 2); tsqueer += Math.sqrt(n);
for (n = 0; n <= 25; n++)
if (Math.pow(n, 2) > 250)
{
csq++;
}
else if (Math.pow(n, 3) > 2000)
{
ccube++;
}
System.out.printf("The sum of the squares: %.0fn", tsquee);
System.out.printf("The sum of the square roots: %.2fn", tsqueer);
System.out.println("The number of squares greater than 250: " + csq);
System.out.println("The number of cubes greater than 2000: " + ccube);
}
}
凯
"令人困惑的缩进"警告可能是因为不需要在 while 循环之后开始并转到末尾的大括号集。此外,您的 for 循环语法不正确。我想你想要的是这个:
for (n = 0; n <= 25; n++)
{
if (Math.pow(n, 2) > 250)
{
csq++;
}
else if (Math.pow(n, 3) > 2000)
{
ccube++;
}
}
但我不确定。您需要将要循环的代码括在大括号内,就像使用 while 循环一样。
此外,由于您的两个 for 循环具有相同的边界和条件,您可以将它们组合为一个 for 循环:
for (n = 0; n <= 25; n++)
{
tsquee += Math.pow(n, 2);
tsqueer += Math.sqrt(n); // I would also put these on separate lines
if (Math.pow(n, 2) > 250)
{
csq++;
}
else if (Math.pow(n, 3) > 2000)
{
ccube++;
}
}