一、for循环代码
/*
* Copyright (c) 2020, 2023, webrx.cn All rights reserved.
*
*/
package com.it;
/**
* <p>Project: chat - Ex03</p>
* <p>Powered by webrx On 2023-07-06 20:42:46</p>
* <p>描述:<p>
*
* @author 简单遗忘 [814736551@qq.com]
* @version 1.0
* @since 17
*/
public class Ex03 {
public static void main(String[] args) {
// 用for循环和while循环分别计算1-100相加的和并输出
// int i=1;
// int sum=0;
// while (i<=100){
// sum+=i;
// i++;
// }
// System.out.println(sum);
// }
int sum1=0;
for(int j=1;j<=100;j++) {
sum1+=j;
}
System.out.println(sum1);
}
}
二、while循环代码
/*
* Copyright (c) 2020, 2023, webrx.cn All rights reserved.
*
*/
package com.it;
/**
* <p>Project: chat - Ex03</p>
* <p>Powered by webrx On 2023-07-06 20:42:46</p>
* <p>描述:<p>
*
* @author 简单遗忘 [814736551@qq.com]
* @version 1.0
* @since 17
*/
public class Ex03 {
public static void main(String[] args) {
// 用for循环和while循环分别计算1-100相加的和并输出
int i=1;
int sum=0;
while (i<=100){
sum+=i;
i++;
}
System.out.println(sum);
}
// int sum1=0;
// for(int j=1;j<=100;j++) {
// sum1+=j;
//
//
// }
// System.out.println(sum1);
// }
}
三、运行截图展示