Java基础之do while循环与for循环

do while

语句格式:

do

{

    执行语句;

}while(条件表达式);

do while特点是条件无论是否满足,

循环体至少被执行一次。

 

 

publicstaticvoid main(String[] args) {

       int x = 0, y = 0;

       do {

           System.out.println(x);

           x++;

       } while (x < 0);

       // do while do会先执行一次,不管是否满足循环条件。

       while (y < 0) {

           System.out.println(y);

           y++;

       }

    }

     

while:先判断条件,只有条件满足才执行循环体。    

do while:先执行循环体,再判断条件,条件满足,再继续执行循环体。

简单一句话:do while:无论条件是否满足,循环体至少执行一次。

注意一个细节do while 后面的分号;

案例:改写猜数字游戏

publicstaticvoid main(String[] args) {

       // 记录用户输入的数字

       int guess = -1;

       // 记录用户输入次数

       int count = 0;

       // 生成1-100之间随机数

       int num = (int) (int)(Math.random()*100)+1;

Scanner sc = new Scanner(System.in);

 

       // 循环猜数字

       do {

           System.out.println("请输入1-100之间的数字");

           guess = sc.nextInt();

           if (guess > num) {

 

              System.out.println("哥们,太大了");

           } elseif (guess < num) {

 

              System.out.println("哥们,太小了");

           } else {

 

              System.out.println("恭喜,中啦");

           }

           count++;

 

       } while (num != guess);

       System.out.println("你猜测的数字是:" + num + "猜测了" + count + "");

    }


例:计算器

     系统自动生成2个随机数用于参与运算。

     系统生成0-4之间的随机数,表示加减乘除取模运算。

     使用switch 进行匹配

class Couter {

    publicstaticvoid main(String[] args) throws InterruptedException {

         // 生成随机数Math.random()生成0-1值,不包含01

         //乘以10得到010之间的数(double类型),不包含010

         //强转为int,并加1得到110之间的数,包含110

        int x = (int)(Math.random()*10)+1;   

        int y = (int)(Math.random()*10)+1;         

        System.out.println(x);

       System.out.println(y);

       // 创建0-4随机数 0 1 2 3 4 各表示加减乘除取模

       int z = (int) (int)(Math.random()*5);

       System.out.println(z);

 

       switch (z) {

       case 0:

           System.out.println(x + "+" + y + "=?");

           System.out.println("哥们快猜。。。。");

           Thread.sleep(2000);

           System.out.println(x + "+" + y + "=" + (x + y));

           break;

       case 1:

           System.out.println(x + "-" + y + "=?");

           System.out.println("哥们快猜。。。。");

           Thread.sleep(2000);

           System.out.println(x + "-" + y + "=" + (x - y));

           break;

       case 2:

           System.out.println(x + "*" + y + "=?");

           System.out.println("哥们快猜。。。。");

           Thread.sleep(2000);

           System.out.println(x + "*" + y + "=" + (x * y));

           break;

       case 3:

           System.out.println(x + "/" + y + "=?");

           System.out.println("哥们快猜。。。。");

           Thread.sleep(2000);

           System.out.println(x + "/" + y + "=" + (x / y));

           break;

       case 4:

           System.out.println(x + "%" + y + "=?");

           System.out.println("哥们快猜。。。。");

           Thread.sleep(2000);

           System.out.println(x + "%" + y + "=" + (x % y));

           break;

       }

    }

}

计算器2:上述中只能计算一次。可以使用whileu循环来不停计算。

程序生成了3个随机数,前两个数参与运算,第三个数用于匹配运算符。要注意除数为0的情况。

int x = (int)(Math.random()*10)+1;    

Math.random() 生成0-1之间的数字,double类型

Math.random()*10 就是0-9之间的数,是double类型

(int)(Math.random()*10)double类型强转成int类型,去掉小数点,便于计算。

(int)(Math.random()*10)+1,生成了110之间随机数。

 

int z = (int) (int)(Math.random()*5);

生成0-4之间的数字,可以用0表示加,1表示减,2表示乘,3表示除,4表示取模

为了减慢程序,使用了Thread.sleep(2000); 让程序等待一会。

 




for循环

1.格式:

for(初始化表达式;循环条件表达式;循环后的操作表达式){

       执行语句;

}

 

2.定义需求: 想要打印5次helloworld

publicstaticvoid main(String[] args) {

       for (int x = 0; x < 5; x++) {

           System.out.println("hello java");

       }

    }

3.for的执行流程

for 知道要进行循环,读到x=0 的时候,在内存中开辟了空间,定义变量x 赋值为0。接着进行条件判断 x<5,为真,这个时候对满足条件后执行了循环体的内容System.out.println("hellojava");当循环体执行完毕之后,执行x < 5;的表达式即 x++ 。x自增后变为了1 ,再次进行判断 x<5 (int x=0 只执行一次),如果为真就再次运行System.out.println("hellojava");如果为假,for循环结束。

 

2、for 和while的区别

publicstaticvoid main(String[] args) {

       for (int x = 0; x < 5; x++) {

           System.out.println("hello java");

       }

       System.out.println(x);

       //x cannot be resolved to a variable

 

       int y = 0;

       while (y < 5) {

           System.out.println("hello world");

           y++;

       }

       System.out.println(y);

}

 

4. 错误

解释 x 为什么会找不到,注意了变量的作用域,也就是变量的作用范围。x 只在 for 循环的大括号内有效,出了这个区域,就无效了.在内存中就消失了。x消失后,仍要访问它,肯定会报错的。

y 就不一样了,y 是定义在while 外的。while循环完毕仍有效  while的初始化 动作在外边,循环结束后y 仍然存在。

当定义的y 只作为循环增量存在的话的,循环完毕后y就没有用了,但是y还是占着一块内存。所以,如果定义的变量只作为循环增量存在的话,就用for 循环可以节约内存。

其实for 和while 是可以互换的。

最后总结

1、for里面的两个表达式运行的顺序,初始化表达式只读一次,判断循环条件,为真就执行循环体,然后再执行循环后的操作表达式,接着继续判断循环条件,重复找个过程,直到条件不满足为止。

2、while与for可以互换,区别在于for为了循环而定义的变量在for循环结束时就在内存中释放。而while循环使用的变量在循环结束后还可以继续使用。

3、最简单无限循环格式:while(true) , for(;;),无限循环存在的原因是并不知道循环多少次,而是根据某些条件,来控制循环。推荐使用while(true)

    

while(true){

          

}

for(;;){

          

       }

for(;true;){

          

       }


 for 循环例:

1.   获取1-10的和,并打印。

2.   1-100之间 7的倍数的个数,并打印。

publicstaticvoid main(String[] args) {

       // 获取110的和1+2+3+4+5+6+7+8+9+10

       int sum = 0;

       for (int x = 1; x <= 10; x++) {

           System.out.println((sum + x) + "=" + sum + "+" + x);

           sum = sum + x;

       }

       System.out.println(sum);// 55

    }

 

 

publicstaticvoid main(String[] args) {

       // 1-100之间 7的倍数的个数,并打印。

       int count = 0;

       for (int x = 0; x <= 100; x++) {

           if (x % 7 == 0) {

              System.out.println(x);

              count++;

           }

       }

       System.out.println(count);

}

 

累加思想:通过变量记录住循环操作后的结果;通过循环的形式.进行累加的动作。

计数器思想:通过一个变量记录住数据的状态变化,也是通过循环完成。

 

循环常见错误:

多加分号:在for括号后和循环体之间加分号是常见错误。

错误:

程序编译运行都可以通过,只是不是我们想要的结果。

for(int i=0;i<100;i++);{

           System.out.println("hello ");

}

正确:

for(int i=0;i<100;i++){

           System.out.println("hello ");

}

 

错误;是一个死循环

int i=0;

while(i<100);{

    System.out.println("hello");

       i++;

}

正确:

int i=0;

while(i<100){

    System.out.println("hello");

       i++;

}


语句的嵌套应用

什么是嵌套形式,其实就是语句中还有语句。

想要打印出矩形:

publicstaticvoid main(String[] args) {

       for (int x = 0; x < 5; x++) {

           System.out.println("*");

       }

 }

publicstaticvoid main(String[] args) {

       for (int x = 0; x < 5; x++) {

           System.out.print("*");

       }

    }

这里用“*”表示矩形的边。

publicstaticvoid main(String[] args) {

       for (int x = 0; x < 5; x++) {

           for(int y=0;y<6;y++){

              System.out.print("*");

           }

           System.out.println();

       }

}

forfor 嵌套for循环例2

打印此种格式的图案

    *****  

    ****

    ***

    **

    *

publicstaticvoid main(String[] args) {

       for (int x = 5; x > 0; x--) {

           for(int y=x;y>0;y--){

              System.out.print("*");

           }

           System.out.println("");

       }

    }



  • 20
    点赞
  • 66
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值