城管希课堂之循环


 do-while循环

 不管判断语句是否成立 都会先执行循环体1次

 do{

      执行体

 }while(判断语句)



while 的死循环

让判断条件 恒成立

需求 打印5次 结束循环

int num = 1;

while(true){

添加一个出口 来停止你的循环

if (num==5) {

结束循环

break 后面的语句是永久不会执行的

所以后面不要写代码 报错

break;// 跳出循环语句

}

System.out.println(num);

num++;

}



 for 循环

  for (声明循环增量初值;判断条件;增量加加){

 		循环体

 }

 

 循环的执行顺序

 for(int i = 0;i<10;i++){

 	sout("hahah");

 }




for循环的嵌套

  嵌套循环的执行顺序

  for (int i = 0; i < 5; i++) {

    for (int j = 0; j < 5; j++) {

	System.out.println("内循环");

	}

    System.out.println("外循环");

    }

System.out.println("循环外");


*

**

***

****

*****

尖向上的三角形

修改 内循环 判断条件



尖向下的三角形

修改 内循环 初始条件


外层循环控制:一共有多少行

内存循环控制:每一行有多少个元素
break 和 continue 在for循环中的作用
continue的作用
结束本次循环 继续执行下一次循环
在嵌套for循环中的一个作用
break 跳出当前的内循环 并没有跳出整个循环 
只能影响到 自己这个层循环
这种break方式直接跳出 名为"w"的循环;
w:for (int i = 0; i < 10; i++) {
	for (int j = 0; j < 10; j++) {
		if (j == 3) {
			break w;
		}
		System.out.print(j);
	}
	System.out.println();
}

猜数字游戏

int num=(int)(Math.random()*(100-0+1)+0);
		
		Scanner scanner = new Scanner(System.in); // 创建系统提供的类的对像
		while (true) {
			System.out.println("请猜一个数0-100");
			String str1 = scanner.nextLine(); // 接收键盘输入的字符串
			int n = Integer.parseInt(str1); //将字符串转换成int类型
			if (n == num) {
				System.out.println("猜中了");
				break;
			} else if (n < num) {
				System.out.println("猜小了");
			} else {
				System.out.println("猜大了");
			}
		}


System.out.println("请输入天气");

		// 创建系统提供的类的对象

		Scanner scanner = new Scanner(System.in);

		// 接收键盘输入的字符串

		String score = scanner.nextLine();
if(score.equals("晴天")){
			System.out.println("去爬山");
		}else  if(score.equals("下雨")){
			System.out.println("去逛街");
			//直接使用之前创建出来的 Scanner对象
			//在此接收用户输入
			System.out.println("请输入小雨还是暴雨");
			String score2 = scanner.nextLine();
			if(score2.equals("小雨")){
				System.out.println("打伞");
			}else{
				System.out.println("穿雨衣");
			}
		}else{
			System.out.println("滚犊子");
		}
字符串的匹配 结果为boolean类型;

设置一个变量来接收最大值;

//  随机[15,200]的整数 10个 并找出最大值
		int max = 0;
		for (int i = 0; i < 10; i++) {
			int num = (int)(Math.random()*(200-15+1)+15);
			System.out.println(num);
			if (num>max) {
				max=num;
			}
		}
		System.out.println("最大值为:"+max);
	}



函数

函数定义:封装特定功能 代码块

函数的好处:减少了重复代码 提高了开发的效率

函数的写法:

参数列表

关键字 返回值类型 函数名(参数类型1 参数名1,参数类型2 参数名2,....){

函数体(执行的语句)

  return 返回值;

}

 没有返回值 使用 void 

 

 函数的书写位置: 

 1.必须在类中

 2.不能在函数中定义函数

 

 函数重载:实现了相同的功能 但是内部实现不同 

 就可以使用函数的重载

 是不是函数的重载 只跟 参数有关

 (并且只跟参数的类型 参数的顺序 参数个数 有关)

 跟函数的返回值类型 和 函数的实现 无关 

 

 return 后面可以不跟返回值

 作用:直接结束这个函数



 需求: 定义函数
  1.计算2个数的最大值
  2.计算3个数的最大值
  3.计算4个数的最大值
  4.计算5个数的最大值
 
  当要编写一个函数的时候
  先看有没有系统或自己的函数可以去使用
public static void main(String[] args) {
		int c=max(1, 10);
		System.out.println(c);
	}
	public static int max(int x,int y){
		int max = x>y?x:y;
		return max;
	}
	public static int max(int x,int y,int z){
		int max = max(x,y);
		return max>z?max:z;
	}
	public static int max(int x,int y,int z,int a){
		int max = max(x,y,z);
		return max>a?max:a;
	}
	public static int max(int x,int y,int z,int a,int b){
		int max = max(x,y,z,a);
		return max>b?max:b;
	}

 递归函数

 函数的实现部分 调用了跟自己相同名字的函数(自己调自己)


public static int fun(int n) {
		//注意:递归函数都需要一个出口
		if(n==1){
			return 1;
		}
		//4*3*2*1 传进来的数 自减	
		return n*fun(n - 1);
	}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值