java cyclic structure of the cyclic structure 02_while

while loop and for comparison a similar loop, while loop will execute the code block cyclic specified condition is true.

[Syntax]

while (循环条件表达式) {
	执行语句;
}

Execution: If the "loop conditional expression" established "loop" is executed, or out of the loop!

"Loop" is finished, the judge will immediately go "cycle conditional expression" is established.

Requirements: All integers between output [1, 100]!

int i = 1; // 循环初始化表达式
while(i <= 100) { // 循环条件表达式
	System.out.println(i);
	i++; // 循环后的操作表达式
}

Note: In the while loop, "after the cyclic operation expression" can only be placed in the "loop", the "cycle of operation after the expression" on the "loop" of the foremost and rearmost fundamentally different!

Demand: Demand daffodils number between 100-999. On each of the digital bits of the 3 daffodils power equals itself (eg: 5 + 1 ^ 3 ^ 3 ^ 3 + 3 = 153).

// a)通过for循环获得[100, 999]之间所有的整数,假设循环变量为i。
int i = 100;
while(i <= 999) {
	// b)在循环体中,获得i的个位数(bit1)、十位数(bit2)和百位数(bit3)。
	int bit1 = i % 10;
	int bit2 = i / 10 % 10;
	int bit3 = i / 100;
	// c)要求满足:bit1*bit1*bit1 + bit2*bit2*bit2 + bit3*bit3*bit3 == i
	if(bit1*bit1*bit1 + bit2*bit2*bit2 + bit3*bit3*bit3 == i) {
		System.out.println(i);
	}
	i++;
}

ps: For the latest free documentation and instructional videos, please add QQ group (627,407,545) receive.

Published 55 original articles · won praise 0 · Views 780

Guess you like

Origin blog.csdn.net/zhoujunfeng121/article/details/104639513