Skip to content

Instantly share code, notes, and snippets.

@fanker
Created October 7, 2012 03:51
Show Gist options
  • Save fanker/3847034 to your computer and use it in GitHub Desktop.
Save fanker/3847034 to your computer and use it in GitHub Desktop.
计算100内所有偶数的和
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