我的逻辑和推理有问题,使用 while 循环,并返回正数 n 的总和和输入总和 n 的平方。请查看我的代码并尽可能提供帮助,谢谢。
练习是: /* 编写一个简短的 Java 方法,它接受一个整数 n 并返回所有小于或等于 n 的正整数的平方和。* */
public class ch1dot7
{
public static void main (String[]args)
{
Scanner input = new Scanner(System.in);
int n, m = 0, sum = 0;
System.out.print("Please enter a value for n: ");
n = input.nextInt();
System.out.println("n is currently: "+n);
if (n <= 0)
{
System.out.print("Please enter a value that is higher than 0 (integer)");
n = input.nextInt();
}
while (sum > n)
{
System.out.print("Please enter a value for m (enter a value greater than n to exit): ");
m = input.nextInt();
if (m < n)
{
sum += m*m;
System.out.println("sum of the squares is: " +sum);
}
sum += m*m;
}
}//end main
}//end class