public class Demo05ShiCao{
public static void main(String[] args){
int sum1 = 0;
for(int i = 1; i <= 100; i++){
if(i % 2 == 0){
sum1 +=i;
}
}
System.out.println("结果是:" + sum1);
System.out.println("==============================================");
int sum2 = 0;
int c = 1;
while(c <= 100){
c++;
if(c % 2 == 0){
sum2 += c;
}
}
System.out.println("结果是:"+sum2);
System.out.println("==============================================");
int sum3 = 0;
int d = 100;
do{
if(d % 2 == 0){
int f;
sum2 += f;
}
d++;
}while(d <= 100);
System.out.println("结果是:"+sum3);
}
拿走不谢
关键代码:
int sum = 0;
for(int i=1;i<=100;i++){
if(i%2==0){
sum += i;
举例代码如下:
public class Sum {
public static void main(String[] args){
int sum = 0;
for(int i=1;i<=100;i++){
if(i%2==0){
sum += i;
}
}
System.out.println("1-100的偶数之和是:" + sum);
}
}
class AAA{
public static void main(String [] args)
{
int sum = 0;
for(int a =1;a<=100;a++){
if(a%2 == 0){
sum = sum + a;
}
}
System.out.println(sum);
}
}
public static void main(String[] args){
int sum = 0;
for(int i=1;i<=100;i++){
if(i%2==0){
sum += i;
}
}
System.out.println("1-100的偶数之和是:" + sum);
}
}
public static void main(String [] args)
{
int sum = 0;
for(int a =1;a<=100;a++){
if(a%2 == 0){
sum = sum + a;
}
}
System.out.println(sum);
}
}
2011-09-19
广告 您可能关注的内容 |