为什么输出末尾没有显示数字 30?
Why is the number 30 not displayed at the end of the output?
enter image description here
package com.company;
public class Main {
public static void main(String[] args) {
// write your code here
int p=0;
for (int i=1; i<11; i++)
{
if (i%2 == 0)
{
System.out.println(p);
p = p + i;
}
}
}
}
在循环之后添加打印。您也可以从 2
开始。并为每次迭代递增 2
(从而消除了对模二测试的需要)。像,
int p = 0;
for (int i = 2; i < 11; i += 2) {
System.out.println(p);
p += i;
}
System.out.println(p);
输出
0
2
6
12
20
30
enter image description here
package com.company;
public class Main {
public static void main(String[] args) {
// write your code here
int p=0;
for (int i=1; i<11; i++)
{
if (i%2 == 0)
{
System.out.println(p);
p = p + i;
}
}
}
}
在循环之后添加打印。您也可以从 2
开始。并为每次迭代递增 2
(从而消除了对模二测试的需要)。像,
int p = 0;
for (int i = 2; i < 11; i += 2) {
System.out.println(p);
p += i;
}
System.out.println(p);
输出
0
2
6
12
20
30