循环结构
while 循环
while 循环是最基本的循环,结构为:
while (布尔表达式){
//循环内容
}
- 只要布尔表达式为true,循环就会一直执行下去。
- 我们大多数情况是会让循环停下来的,我们需要一个让表达式失效的方式来结束循环。
- 少数情况需要循环一直执行,比如服务器的请求响应监听等。
- 循环条件一直为true,就会 造成无限循环【死循环】,我们正常的业务编程中,应该避免死循环,会影响程序性能或者造成循环卡死崩溃。
public class WhileDemo02 {
public static void main(String[] args) {
//死循环 要避免,否则可能会使程序崩溃或者卡死
while (true){
//等待客户端连接
//定时检查
//。。。
}
}
}
输出1-100
public class WhileDemo01 {
public static void main(String[] args) {
//输出1-100
int i = 0;
int sum = 0;
while (i<100){
i++;
sum = sum + i;
System.out.println(i);
}
System.out.println(sum);
}
}
计算从1加到100的和
public class WhileDemo03 {
public static void main(String[] args) {
//计算从1+到100
//高斯的故事
int i = 0;
int sum = 0;
while (i<=100){
sum = sum +i;
i++;
}
System.out.println(sum);
}
}
用while输出1-1000以内能被5整除的数,且每行输出数字最多三个
public class ForDemo05 {
public static void main(String[] args) {
//用while循环输出1-1000之间能被5整数的数,并且每一行输出三个
int i = 0;
while (i<=1000){
i++;
if (i%5==0){
System.out.print(i+"\t");
}
if (i%(5*3)==0){
System.out.print("\n");
}
}
}
}
do…while 循环
对于while语句而言,如果 不满足条件,则不能进入循环,但有时我们需要即使不满足条件,也至少执行一次。
do…while循环和while循环相似,不同的是,do…while循环至少会执行一次。
do{
//代码语句
}while (布尔表达式)
- while和do…while的区别
- while先判断后执行,do…while是先执行后判断
- do…while总是保证循环体至少会被执行一次,这是它们的主要差别。
public class DoWhileDemo02 {
public static void main(String[] args) {
//while和do...while的区别
int a = 0;
while (a<0){
System.out.println(a);
a++;
} //不会输出结果
System.out.println("=====================");
do {
System.out.println(a);
a++;
}while (a<0); //输出0
}
}
for 循环
for 循环可以使某些结构变得更加简单。
for 循环语句是支持迭代的一种通用结构,是最有效,最灵活的循环结构。
for循环执行的次数是在循环前就已经确定的,格式如下:
for (初始化;布尔表达式;更新){
//代码语句
}
public class ForDemo01 {
public static void main(String[] args) {
int a = 1; //初始化条件
while (a<=100){ //条件判断
System.out.println(a); //循环体
a+=2; //迭代
}
System.out.println("while循环结束");
for (int i = 1;i<=100;i++){
System.out.println(i);
}
System.out.println("for循环结束");
for (int x = 0; x < 100; x++) {
System.out.println(x);
}
/*
关于for循环有一下几点说明
最先执行初始化步骤,可以声明一种变量,但可初始化一个或多个循环控制变量,也可以是空语句
然后检测布尔表达式的值,如果为true,循环体被执行,如果为false,循环体被终止,开始执行循环体后面的语句
执行一次循环后,更新循环控制变量(迭代因子控制循环变量的增减),再次检测布尔表达式,循环执行上面的过程
*/
//以下是死循环
/*
for (; ; ) {
}
*/
}
}
计算1-100之家的奇数和偶数的和
public class ForDemo02 {
public static void main(String[] args) {
int oddSum = 0;
int evenSum = 0;
for (int i = 0; i <= 100; i++) {
if (i%2!=0){ //奇数
oddSum+=i; //oddSum = oddSum = i;
}else { //偶数
evenSum+=i;
}
}
System.out.println("奇数的和:"+ oddSum);
System.out.println("偶数的和:" + evenSum);
}
}
用for循环输出1-1000以内的能被5整除的数,且每行输出三个数字
public class ForDemo04 {
public static void main(String[] args) {
//用while或for循环输出1-1000之间能被5整数的数,并且每一行输出三个
for (int i = 0; i <= 1000; i++) {
if (i%5==0){
System.out.print(i+"\t");
}
if (i%(5*3)==0){ //换行
System.out.println();
//System.out.print("\n");
}
}
//System.out.println(); 输出完会换行
//System.out.print(); 输出完不会换行
}
}
打印九九乘法表
public class Demo05 {
/*1*1=1
1*2=2 2*2=4
1*3=3 2*3=6 3*3=9
1*4=4 2*4=8 3*4=12 4*4=16
1*5=5 2*5=10 3*5=15 4*5=20 5*5=25
1*6=6 2*6=12 3*6=18 4*6=24 5*6=30 6*6=36
1*7=7 2*7=14 3*7=21 4*7=28 5*7=35 6*7=42 7*7=49
1*8=8 2*8=16 3*8=24 4*8=32 5*8=40 6*8=48 7*8=56 8*8=64
1*9=9 2*9=18 3*9=27 4*9=36 5*9=45 6*9=54 7*9=63 8*9=72 9*9=81
*/
public static void main(String[] args) {
// 1.我们下面打印出第一列
// 2.我们把固定的1把循环包起来
// 3.去掉重复项目 i <= g
// 4.调整样式
for (int g = 1; g <= 9; g++) {
for (int i = 1; i <= g; i++) {
System.out.print(g+"*"+i+"="+(i*i)+"\t");
}
System.out.println();
}
}
}
示例2:while打印
public class Demo03 {
public static void main(String[] args) {
//九九乘法表
int b = 0;
while (b<9){
b++;
int a = 0; //这时,不能把变量a和变量b一起声明,如果把a声明到前面,在走while b的时候b的值不清零,越来越大,这样就得不到九九乘法表
while (a<b){
a++;
System.out.print(a+"*"+b+"="+(b*a)+"\t");
if (a==b){
System.out.println();
}
}
}
}
}
示例3:另外一种for打印
public class Demo04 {
public static void main(String[] args) {
//打印九九乘法表
for (int a = 1; a <= 9; a++) {
for (int b = 1; b<=a; b++){
System.out.print(b+"*"+a+"="+(b*a)+"\t");
if (a==b){
System.out.println();
}
/* 上面结果和下面输出结果是一样的,写成上面的代码原因是没有理解透for循环
for (int g = 1; g <= 9; g++) {
for (int i = 1; i <= g; i++) {
System.out.print(g+"*"+i+"="+(i*i)+"\t");
}
System.out.println();
}
*/
}
}
}
}
增强型 for 循环
Java5中引入了一种主要用于数组的增强型for循环
for (声明语句:表达式){
//代码句子
}
- 声明语句:声明新的局部变量,该变量的类型必须与数组元素的类型匹配,其作用域限定在循环语句块,其值与此时数组元素的值相等。
- 表达式:表达式是要访问的数组名,或者返回值为数组的方法。
public class Demo07 {
public static void main(String[] args) {
int [] numbers = {10,20,30,40,50,60,70,80}; //定义了一个数组
//遍历数组元素
for (int i = 0; i<8; i++){
System.out.print(numbers[i]+"\t");
}
System.out.println(); //输出空行分割
for (int x:numbers){
System.out.print(x+"\t");
}
}
}