Java while与do while
while循环-简单示例:
package com.LittleWu.Struct;
public class WhileDemo03 {
public static void main(String[] args) {
// 计算1+2+3+.....+100
// while先判断再执行
int i = 0;
int sum = 0;
while(i < 100){
i++;
sum+=i;
System.out.println(i + "+");
}
System.out.println("=" + sum);
}
}
while循环注意事项:
package com.LittleWu.Struct;
public class WhileDemo02 {
public static void main(String[] args) {
while(true){ // 循环为死循环 尽量避免
// 等待客户端连接
// 定时检查
// 。。。。。。。等
}
}
}
do while循环使用实例
package com.LittleWu.Struct;
public class DoWhileDemo01 {
public static void main(String[] args) {
// 计算1+2+3+.....+100
// do while限制性再判断 至少要执行一次
int i = 0;
int sum = 0;
do {
sum = sum + i;
i++;
}while (i <= 100);
System.out.println("=" + sum);
}
}
do while循环与while循环对比
package com.LittleWu.Struct;
/***
* ░░░░░░░░░░░░░░░░░░░░░░░░▄░░
* ░░░░░░░░░▐█░░░░░░░░░░░▄▀▒▌░
* ░░░░░░░░▐▀▒█░░░░░░░░▄▀▒▒▒▐
* ░░░░░░░▐▄▀▒▒▀▀▀▀▄▄▄▀▒▒▒▒▒▐
* ░░░░░▄▄▀▒░▒▒▒▒▒▒▒▒▒█▒▒▄█▒▐
* ░░░▄▀▒▒▒░░░▒▒▒░░░▒▒▒▀██▀▒▌
* ░░▐▒▒▒▄▄▒▒▒▒░░░▒▒▒▒▒▒▒▀▄▒▒
* ░░▌░░▌█▀▒▒▒▒▒▄▀█▄▒▒▒▒▒▒▒█▒▐
* ░▐░░░▒▒▒▒▒▒▒▒▌██▀▒▒░░░▒▒▒▀▄
* ░▌░▒▄██▄▒▒▒▒▒▒▒▒▒░░░░░░▒▒▒▒
* ▀▒▀▐▄█▄█▌▄░▀▒▒░░░░░░░░░░▒▒▒
* 单身狗就这样默默地看着你,一句话也不说。
*/
public class DoWhileDemo02 {
public static void main(String[] args) {
int a = 0;
while (a<0){
System.out.println(a);
a++;
}
System.out.println("=============================================");
do {
System.out.println(a);
a++;
}while (a<0);
}
}
本文地址:https://blog.csdn.net/qq_51555533/article/details/109277566
您可能感兴趣的文章:
- 创建Gradle工程出现Could not install Gradle distribution from 'https://services.gradle.org/distributions/gradleXX'.问题解决
- spring boot 错误:Check your ViewResolver setup
- java实现判断一个整数是几位数,并按照逆序输出
- Please, configure Web Facet first!idea报这错的解决办法!!
- SpringBoot 2.0 报错: Failed to configure a DataSource: 'url' attribute is not specified and no embe
- [异常处理1]Error occurred during initialization of boot layer
- 解决Invalid character found in the request target. The valid characters are defined in RFC 7230 and RFC 问题
如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!
网友评论