英文:
Sum Programme adding square of numbers
问题
以下是翻译好的代码部分:
public class Summing {
public static void main(String args[]) {
int sum = 0;
int n = 1;
int square;
for (int i = 1; i <= 100; i++) {
if (i % 13 == 0) {
sum = sum + i;
}
}
System.out.print("所有可被13整除的数字之和:" + sum);
while (n <= 100) {
square = n * n;
sum = sum + square;
n++;
}
System.out.print("所有数字平方之和:" + sum);
}
}
英文:
college assignment here and need to do the following:
- The sum of all numbers divisible by 13 between 1 and 100 inclusive
- The sum of all numbers squared between 1 and 100 inclusive
The output i get when adding the squared numbers is 338714 but correct answer is 338350 - can someone advise where exactly i am going wrong
public class Summing {
public static void main (String args []){
int sum = 0;
int n = 1;
int square;
for (int i = 0; i < 100; i++){
if (i%13==0)
{
sum = sum + i;
}
}
System.out.print("The sum of all all the numbers is divisible by 13" = sum);
while (n<=100){
square (n*n);
sum = sum + square;
n++;
}
System.out.print("The sum of all the numbers squares is " + sum );
}
}
答案1
得分: 0
338714 - 338350 = 314 这恰好是你的第一个任务的总和 ![]()
你需要重置"sum"变量,因为当进入第二个循环时,它的值是314,这是差值。
英文:
338714 - 338350 = 314 which is exactly the sum of your first mission ![]()
You hav to reset "sum" variable, because when getting into the second loop its value is 314 which is the difference.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论