Java (Notes on the fifth day of entry)

                         **for 语句**(循环语句)

Case 1: Find the sum of even numbers between 1-100:

Insert picture description here

Case 2: Narcissus

 在控制台上输出所有的水仙花数;

Insert picture description here

 /*for 循环语句

Case 3:
Statistics

Requirement: Statistics: how many "" daffodils are in total, and output the number on the console

Analysis: 1: Define the variable count, which is used to save the number of "Daffodils", the initial value is 0, define the variable count, which is used to save the number of "Daffodils", the initial value is 0
int cout=0
2: in judgment During the process of daffodil count, the output will not be output if the condition is met, change to modify the value of coun, make count+1

  if(a*a*a+b*b*b+c*c*c==原数)
  {count++;
	  
	  
  }
  3:打印输出结果
  System.out.println("水仙花的数目为"+count+"个";)

while loop statement
Basic format:
while (conditional judgment statement) { loop body statement; } complete format:


初始化语句;
while(条件判断语句){
	循环体语句;
	条件控制语句;
	
}

执行流程:
1:执行初始化语句;
2:执行条件判断语句,看其结果是ture 还是false;
3:执行循环体语句
4:执行条件控制语句
5:回到2继续




案例4需求:连需打印五次“HelloWorld”

Insert picture description here

do while loop body statement

Basic format:
do{ loop body statement;

}while (conditional judgment statement);

Full format:

Initialization statement
do { loop body statement; conditional control statement;

}while (conditional judgment statement);

The difference between the three loops: The method to end the infinite loop: ctrl+cInsert picture description here

Case 5:
Demand: Try to output odd and even numbers from 1-5 respectively.

Jump control statement:
1# continue is used in the loop, based on conditional control, skips the execution of a certain loop body content (when true), and continues to the next execution
2# break is used in the loop, based on conditional control, terminates the loop The execution of the body content, that is, the end of the current entire loop

Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_51599540/article/details/109320397