了解do-while循环 [英] Understanding do-while loop

查看:77
本文介绍了了解do-while循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过oracle认证的准Java SE7程序员实践考试(这本书),遇到一个问题,即使有解释我也听不懂答案. 这是说明和代码:

I'm doing the oracle certified associate Java SE7 Programmer practice exams (the book) and came across a question, and I don't understand the answer even with the explanation. Here's the explanation and the code:

它将打印3.循环主体执行两次,程序将打印3.

It will print 3. The loop body is executed twice and the program will print 3.

我不了解循环主体如何执行两次,也许我不了解b=!b的含义.有人可以解释一下吗?

I don't understand how the loop body is executed twice, maybe I don't understand what the b=!b means. Can someone explain please?

class TestClass {
    public static void main(String args[]){
        boolean b = false;
        int i = 1;
        do{
            i + + ;
        } while (b = !b);
        System.out.println(i);
    }
}

推荐答案

b = !b 赋值 和错误)

b = !b is an assignment which assigns the inverse of b to itself (effectively flipping between true and false)

在Java中,赋值会返回分配的内容(以便可以使用a=b=1)

in java, an assignment returns what was assigned (so that a=b=1 is possible)

因此,while (b=!b)将翻转b的值,然后检查b的值.

therefore while (b=!b) will flip the value of b, and then check the value of b.

这篇关于了解do-while循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆