1.循环结构的四要素
① 初始化条件
② 循环条件 —>是boolean类型
③ 循环体
④ 迭代条件
说明:通常情况下,循环结束都是因为②中循环条件返回false了。
package com.ht.workbag;
//2到10的偶数之和
public class Count {
public static void main(String[] args) {
// TODO Auto-generated method stub
int sum = 0;
//方法一:for(条件){循环体}
// for( int i=2;i<=10;i++)
// {
// if(i%2==0)
// {
// sum+=i;
// }
// }
//方法二:while(条件){循环体}
int i = 2;
while (i<11)
{
sum+=i;
i=i+2;
}
//方法三: do{循环体}...while(条件);
// int i = 2;
// do {
// sum+=i;
// i=i+2;
// }while (i<11);
System.out.println("2到10的偶数之和为:"+sum);
}
}
2.三种循环结构:
2.1 for循环结构
for(①;②;④){
③
}
执行过程:① - ② - ③ - ④ - ② - ③ - ④ - … - ②
class ForTest {
public static void main(String[] args) {
/*
System.out.println("Hello World!");
System.out.println("Hello World!");
System.out.println("Hello World!");
System.out.println("Hello World!");
System.out.println("Hello World!");
*/
for(int i = 1;i <= 5;i++){//i:1,2,3,4,5
System.out.println("Hello World!");
}
//i:在for循环内有效。出了for循环就失效了。
//System.out.println(i);
//练习:
int num = 1;
for(System.out.print('a');num <= 3;System.out.print('c'),num++){
System.out.print('b');
}
//输出结果:abcbcbc
System.out.println();
//例题:遍历100以内的偶数,输出所有偶数的和,输出偶数的个数
int sum = 0;//记录所有偶数的和
int count = 0;//记录偶数的个数
for(int i = 1;i <= 100;i++){
if(i % 2 == 0){
System.out.println(i);
sum += i;
count++;
}
//System.out.println("总和为:" + sum);
}
System.out.println("总和为:" + sum);
System.out.println("个数为:" + count);
}
}
/*
编写程序从1循环到150,并在每行打印一个值,
另外在每个3的倍数行上打印出“foo”,
在每个5的倍数行上打印“biz”,
在每个7的倍数行上打印输出“baz”。
*/
class ForTest1 {
public static void main(String[] args) {
for(int i = 1;i <= 150;i++){
System.out.print(i + " ");
if(i % 3 == 0){
System.out.print("foo ");
}
if(i % 5 == 0){
System.out.print("biz ");
}
if(i % 7 == 0){
System.out.print("baz ");
}
//换行
System.out.println();
}
}
}
class IfExer {
public static void main(String[] args) {
int x = 4;
int y = 1;
if (x > 2)
if (y > 2)
System.out.println(x + y);
//System.out.println("atguigu");
else //就近原则
System.out.println("x is " + x);
//课后练习3:测算狗的年龄
int dogAge = 6;
if(dogAge >= 0 && dogAge <= 2){
System.out.println("相当于人的年龄:" + dogAge * 10.5);
}else if( dogAge > 2){
System.out.println("相当于人的年龄:" + (2 * 10.5 + (dogAge - 2) * 4));
}else{
System.out.println("狗狗还没出生呢!");
}
//课后练习4:如何获取一个随机数:10 - 99
int value = (int)(Math.random() * 90 + 10);// [0.0,1.0) --> [0.0,90.0) --->[10.0, 100.0) -->[10,99]
System.out.println(value);
//公式:[a,b] : (int)(Math.random() * (b - a + 1) )+ a
}
}
2.2 while循环结构
①
while(②){
③;
④;
}
执行过程:① - ② - ③ - ④ - ② - ③ - ④ - … - ②
说明:
写while循环千万小心不要丢了迭代条件。一旦丢了,就可能导致死循环!
for和while循环总结:
-
开发中,基本上我们都会从for、while中进行选择,实现循环结构。
-
for循环和while循环是可以相互转换的!
区别:for循环和while循环的初始化条件部分的作用范围不同。 -
算法:有限性
class WhileTest{ public static void main(String[] args) { //遍历100以内的所有偶数 int i = 1; while(i <= 100){ if(i % 2 == 0){ System.out.println(i); } i++; } //出了while循环以后,仍可以调用。 System.out.println(i);//101 } }
-
我们写程序,要避免出现死循环。
2.3 do-while循环结构
①
do{
③;
④;
}while(②);
执行过程:① - ③ - ④ - ② - ③ - ④ - … - ②
说明:
1.do-while循环至少会执行一次循环体!
2.开发中,使用for和while更多一些。较少使用do-while
class DoWhileTest {
public static void main(String[] args) {
//遍历100以内的偶数,并计算所有偶数的和及偶数的个数
int num = 1;
int sum = 0;//记录总和
int count = 0;//记录个数
do{
if(num % 2 == 0){
System.out.println(num);
sum += num;
count++;
}
num++;
}while(num <= 100);
System.out.println("总和为:" + sum);
System.out.println("个数为:" + count);
//*************体会do-while至少执行一次循环体***************
int number1 = 10;
while(number1 > 10){
System.out.println("hello:while");
number1--;
}
int number2 = 10;
do{
System.out.println("hello:do-while");
number2--;
}while(number2 > 10);
}
}
3.“无限循环”结构: while(true) 或 for(;😉
总结:如何结束一个循环结构?
方式一:当循环条件是false时
方式二:在循环体中,执行break
class ForWhileTest {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int positiveNumber = 0;//记录正数的个数
int negativeNumber = 0;//记录负数的个数
for(;;){//while(true){
int number = scan.nextInt();
//判断number的正负情况
if(number > 0){
positiveNumber++;
}else if(number < 0){
negativeNumber++;
}else{
//一旦执行break,跳出循环
break;
}
}
System.out.println("输入的正数个数为:" + positiveNumber);
System.out.println("输入的负数个数为:" + negativeNumber);
}
}
4.嵌套循环
1.嵌套循环:将一个循环结构A声明在另一个循环结构B的循环体中,就构成了嵌套循环
内层循环:循环结构A
外层循环:循环结构B
2.说明:
① 内层循环结构遍历一遍,只相当于外层循环循环体执行了一次
② 假设外层循环需要执行m次,内层循环需要执行n次。此时内层循环的循环体一共执行了m * n次
③ 外层循环控制行数,内层循环控制列数
【典型练习】
//练习一:
/*
******
******
******
******
*/
for(int j = 1;j <= 4;j++ ){
for(int i = 1;i <= 6;i++){
System.out.print('*');
}
System.out.println();
}
//练习二:
/* i(行号) j(*的个数)
* 1 1
** 2 2
*** 3 3
**** 4 4
***** 5 5
*/
for(int i = 1;i <= 5;i++){//控制行数
for(int j = 1;j <= i;j++){//控制列数
System.out.print("*");
}
System.out.println();
}
/* i(行号) j(*的个数) 规律:i + j = 5 换句话说:j = 5 - i;
**** 1 4
*** 2 3
** 3 2
* 4 1
*/
for(int i = 1;i <= 4;i++){
for(int j = 1;j <= 5 - i;j++){
System.out.print("*");
}
System.out.println();
}
/*
嵌套循环的应用1:
九九乘法表
1 * 1 = 1
2 * 1 = 2 2 * 2 = 4
。。。
9 * 1 = 9 。。。 9 * 9 = 81
*/
class NineNineTable {
public static void main(String[] args) {
for(int i = 1;i <= 9;i++){
for(int j = 1;j <= i;j++){
System.out.print(i + " * " + j + " = " + (i * j) + " ");
}
System.out.println();
}
}
}
/*
100以内的所有质数的输出。
质数:素数,只能被1和它本身整除的自然数。-->从2开始,到这个数-1结束为止,都不能被这个数本身整除。
最小的质数是:2
*/
class PrimeNumberTest {
public static void main(String[] args) {
boolean isFlag = true;//标识i是否被j除尽,一旦除尽,修改其值
for(int i = 2;i <= 100;i++){//遍历100以内的自然数
for(int j = 2;j < i;j++){//j:被i去除
if(i % j == 0){ //i被j除尽
isFlag = false;
}
}
//
if(isFlag == true){
System.out.println(i);
}
//重置isFlag
isFlag = true;
}
}
}
补充:衡量一个功能代码的优劣:
1.正确性
2.可读性
3.健壮性
4.高效率与低存储:时间复杂度 、空间复杂度 (衡量算法的好坏)
如何理解流程控制的练习:
流程控制结构的使用 + 算法逻辑