Created
October 7, 2012 03:51
-
-
Save fanker/3847034 to your computer and use it in GitHub Desktop.
计算100内所有偶数的和
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Total { | |
public static void main (String[] args) { | |
int i = 0; | |
int total = 0; | |
while (i <= 100) { | |
int num = i%2;//数字i对2进行取余 | |
i++;//对数字i进行自加 | |
if (num != 0) {//判断余数是否为0 | |
total += i;//当余数不为0的时候,说明i是偶数,加到和值total上 | |
} | |
} | |
System.out.println("100以内所有的偶数和为:" + total ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment