1.for loop syntax
[Syntax]
for(初始化表达式; 循环条件表达式; 循环后的操作表达式) {
执行语句;
}
Key: Mastering the execution order for the cycle. The initialization expression is executed first, and only performed once during the cycle.
2.for cycle Precautions
-
"Cycle initialization expression" was first performed, and never performed only once!
-
Variables declared in a "loop initialization expression," we call "loop variable."
"Loop variable" is a local variable used in a loop can not be used outside of the loop.
-
What Will the following code output result? ? ?
// 演示一
int i = 1; // 出生
for(; i <= 100; i++) {
System.out.println(i);
}
System.out.println("循环之外:" + i); // 输出:101
// 演示二
for(int i = 10, j = 2; j < 7; j += 3, i -= 2) {
System.out.println("i:" + i + " j:" + j);
}
// 第一趟输出:"i:10 j:2"
// 第二趟输出:"i:8 j:5"
-
"Cycle initialization expression" and "post-cycle operation expression" there is no limit, you only need to conform to the syntax!
"Cycling conditional expression" requires the returned results must be a boolean, otherwise compilation errors!
3.for cycle exercise
1, the output of 0 (included) to a number between 100 (inclusive) in a decreasing manner;
for(int i = 100; i >= 0; i--) {
System.out.println(i);
}
2, the output of odd numbers between 1 and 100;
/**
* 步骤分析
* a)通过for循环,获得[1, 100]之间的所有整数,假设循环变量为i。
* b)在循环体中,判断i是否为奇数。
* 当i%2!=0时,证明i是一个奇数!
*/
for(int i = 1; i <= 100; i++) {
if(i % 2 != 0) {
System.out.println("奇数:" + i);
}
}
3, enter a positive integer n, calculating 1 + 2 + 3 + ... + n and;
/**
* 步骤分析:
* a)通过Scanner获得一个正整数,假设使用变量n来保存。
* b)通过for循环,获得[1, n]之间所有的正整数,假设循环变量为i。
* c)在循环前面定义一个变量(sum),用于保存1+2+3+…+n的和。
*/
// 1.通过Scanner获得一个正整数
Scanner input = new Scanner(System.in);
System.out.print("请输入一个正整数:");
int n = input.nextInt();
// 2.通过for循环,获得[1, n]之间所有的正整数
int sum = 0; // sum出生
for(int i = 1; i <= n; i++) {
// 3.计算1+2+3+…+n的和
sum += i;
}
System.out.println("sum:" + sum);
4, enter a positive integer n, calculating 1-2 + 3-4 + 5-6 + ... - (n-1) + n and;
/**
* 步骤分析:
* a)通过Scanner获得一个正整数,假设使用变量n来保存。
* b)通过for循环,获得[1, n]之间所有的正整数,假设循环变量为i。
* c)在循环前面定义一个变量(sum),用于保存1-2+3-4+5-6+…-(n-1)+n的和;
* 当i为奇数时,执行累加操作;当i为偶数时,执行累减操作。
*/
// 1.通过Scanner获得一个正整数
Scanner input = new Scanner(System.in);
System.out.print("请输入一个正整数:");
int n = input.nextInt();
// 2.通过for循环,获得[1, n]之间所有的正整数
int sum = 0;
for(int i = 1; i <= n; i++) {
// 3.计算1-2+3-4+5-6+…-(n-1)+n的和
if(i % 2 == 0) { // 偶数
sum -= i;
}
else { // 奇数
sum += i;
}
}
System.out.println(sum);
5, the output is between 1 and 1000 can both be divisible by 5 the number divisible by 3, and each output line 5.
/**
* 步骤分析:
* a)通过for循环获得[1, 1000]之间所有的整数,假设循环变量为i。
* b)在循环体中,判断i是否既能被5整除又能被3整除,满足该条件则输出i的值。
* 输出i的值时,不要做换行操作。 System.out.print(i);
* c)把符合条件的数每行输出五个!
* 1)定义一个变量(count),用于保存符合条件数的个数。
* 2)当i既能被5整除又能被3整除,则符合条件的数+1。
* 3)判断count的值为5的倍数,则执行换行操作!
*/
// 1.通过for循环获得[1, 1000]之间所有的整数
// a)定义一个变量,用于保存符合条件数的个数。
int count = 0;
for(int i = 1; i <= 1000; i++) {
// 2.判断i是否既能被5整除又能被3整除
if(i % 5 == 0 && i % 3 == 0) {
// 满足以上条件,则输出i的值
// System.out.print(i + '\t'); // 错误
System.out.print(i + "\t"); // 正确
// b)当i既能被5整除又能被3整除,则符合条件的数+1。
count++;
// c)判断count的值为5的倍数,则执行换行操作!
if(count % 5 == 0) {
System.out.println();
}
}
}
6, daffodils Numbers by between 100 to 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。
* b)在循环体中,获得i的个位数(bit1)、十位数(bit2)和百位数(bit3)。
* c)要求满足:bit1*bit1*bit1 + bit2*bit2*bit2 + bit3*bit3*bit3 == i
*/
// 1.通过for循环获得[100, 999]之间所有的整数
for(int i = 100; i < 1000; i++) { // i = 153
// 2.获得i的个位数(bit1)、十位数(bit2)和百位数(bit3)
int bit1 = i % 10;
int bit2 = i % 100 / 10;
int bit3 = i / 100;
// 3.要求满足: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);
}
}
7, programming to find an integer number of four in abcd satisfy the following relationship, (ab + cd) * (ab + cd) = abcd (e.g.: (20 + 25) * (20 + 25) = 2025).
/**
* 步骤分析:
* a)通过for循环获得[1000, 9999]之间所有的整数,假设循环变量为i。
* b)在循环体中,获得i的千位百位(ab)和十位个数(cd)。
* 例如:i的值为2025,那么i%100就得到了25(cd),i/100就得到20(ab)。
* c)要求满足:(ab+cd)*(ab+cd) == i
*/
// 1.通过for循环获得[1000, 9999]之间所有的整数
for(int i = 1000; i < 10000; i++) { // i = 2025
// 2.获得i的千位百位(ab)和十位个数(cd)。
int ab = i / 100;
int cd = i % 100;
// 3.要求满足:(ab+cd)*(ab+cd) == i
if((ab+cd)*(ab+cd) == i) {
System.out.println(i);
}
}
ps: For the latest free documentation and instructional videos, please add QQ group (627,407,545) receive.