Java_day_03_运算符、流程控制、循环、方法初识

1.运算符

1.1赋值运算符

package _01_Opreator;
/**
 * 赋值运算符
 * 
 * 基本的 = : 把右边的赋值给左边
 * 
 * += : 左右相加,赋值给左边
 * -= :左减去右,赋值给左边
 * 还有*=;/=;%=;作用同上
 * 
 * 左结合:同等优先级 先算左边
 * 右结合:同等优先级 先算右边
 * +=这些是右结合
 * @author 16200
 *2021年12月30日上午9:14:22
 */
public class Operator_01 {

	public static void main(String[] args) {
		byte i = 2;
		//自动强制类型转换
		i+=3;
		//这种需要强制转换,i+3结果为int
		i = (byte) (i + 3);
		//i++等价于i = i + 1;
		i++;
//		 * 左结合:同等优先级 先算左边
//		 * 右结合:同等优先级 先算右边
//		 * +=这些是右结合
		int a = 10 ;
		a*= a+=1;//左结合为101,右结合110
						//初始化还是从左到右,不过先执行右边的 
//						10*=10+=1---->10*=11----->110
		
		System.out.println(a);
	}

}

1.2字符串连接符

package _01_Opreator;

/**
 * 字符串连接符
 * 

 * 	
 * @author 16200
 *2021年12月30日上午9:29:48
 */

public class Operator_02 {

	public static void main(String[] args) {
		String string ="大大大大";
//		 * 用+把多个数据拼接成一个字符串
		int a = 1;
		int b = 2;
		//如果两端为数字则为加和运算
		System.out.println(a+b);
		//如果两端有一个字符串,则为字符串拼接符,用来把他们连接起来
		System.out.println("a+b"+a+b);
		//如果还要运算则加括号
		System.out.println("a+b="+(a+b));
				
	}

}

1.3 三目运算符

package _01_Opreator;
/**
 * 三目运算符
 * 
 * 语法:布尔型表达式?真:假
 * 
 * 若布尔型表达式为true,执行真 否则 执行 假
 * 
 * @author 16200
 *2021年12月30日上午9:39:42
 */
public class Operator_03 {
	public static void main(String[] args){
		int i = 2 < 2 ? 1 : 0;
		//为false,i==0;
		System.out.println(i);
	}
}

2 流程控制

2.1循序结构

package _02_Flow;
/**
 * 流程控制
 * 
 * 循序结构
 * 
 * 分支结构:通过指定的判断条件,选择性执行不同分支
 * 		单分支* 		双分支 * 		多分支
 * 		if……else……
 * 		swicth
 * 
 * 循环结构
 * 
 * @author 16200
 *2021年12月30日上午10:09:36
 */
public class Flow_01 {
	public static void main (String[] args){
			int a = 10 ;
			int b = 11 ;
			//单分支,if(布尔表达式){为真时执行的代码}
			if (a>b){
				a = b;
			}
			//双分支,if(布尔表达式){为真时执行}else{为假时执行}
			if (a>b){
				a = b;
			}else{
				b = a;
			}
			//多分支,if(){}else if(){}……
			//如果以else{}结尾,则必有一条语句被执行
			//如果以else if (){}结尾,则不一定执行
			if (a>b){
				a = b;
			}else if(a<b){
				b = a;
			}else {
				System.out.println(a);
			}
			//只有一条语句时,{}可省略(不建议)
			if (a>b)
				a = b;
			
	}
}

2.2 IF

package _02_Flow;

import java.util.Scanner;

/**
 * 要求: 
 * 	给定人的年龄1~100
 * 		判断处于什么阶段
 * 		[1-5]幼儿
 * 		[6-18]少年
 *		[19-35]青年
 *		[36-55]中年
 *		[56-100]老年
 * 
 * @author 16200
 *2021年12月30日上午10:31:31
 */
public class if_01 {

	public static void main(String[] args) {
		@SuppressWarnings("resource")
		Scanner scanner = new Scanner (System.in);
		System.out.println("请输入年龄,并按回车确认:");
		int age = scanner.nextInt();
		if (age<=0|age>100){
			System.out.println("输入错误");	
		}else if (age<=5){
			System.out.println("此人处于幼儿时期");	
		}else if (age<=18){
			System.out.println("此人处于少年时期");	
		}else if (age<=35){
			System.out.println("此人处于青年时期");	
		}else if (age<=55){
			System.out.println("此人处于中年时期");	
		}else {
			System.out.println("此人处于老年时期");	
		}
	}

}

2.3 SWITCH

​
package _03_Switch;
/**
 * switch:Java1.7之前 只能传入int值
 * 				但是从1.7开始 可以传入字符串
 * 语法:
 * 		Switch(值){
 * 			case 值1:
 * 				执行语句;
 * 				break;
 * 			case 值2:
 * 				执行语句;
 * 				break;
 * 			default:
 * 				执行语句;
 * 				break;
 * 		}
 * 
 * 每一个分支都需要加break来结束分支,不然会出现case穿透现象
 * 
 * @author 16200
 *2021年12月30日上午11:22:12
 */
public class Switch_01 {

	public static void main(String[] args) {
		//if可以判断相等和范围,Switch只能判断相等
	
			char c = 'A';
			switch(c){
			case 'A':
				System.out.println("A");
				break;
			case 'b':
				System.out.println("b");
				break;
			default:
				System.out.println("c");
				break;
			}
			//不加break,case穿透,无法终止
			char q = 'A';
			switch(q){
			case 'A':
				System.out.println("A");
				
			case 'b':
				System.out.println("b");
				
			default:
				System.out.println("c");
			}
	}
}


​

2.3.1 case穿透

package _03_Switch;

public class Switch_02 {

	public static void main(String[] args) {
		char c = 'A';
		//利用case穿透来达到多重判断的效果
		switch(c){
		case 'A':
		case 'b':
			System.out.println("b");
			break;
		default:
			System.out.println("c");
			break;
		}
	}

}

2.3.2例题

package _03_Switch;

import java.util.Scanner;
/**
 * 要求:
 * 		给定学生成绩[0-100],注意 成绩可以为小数
 * 		[90-100] 优秀
 * 		[70-90] 良好
 * 		[60-70] 及格
 * 		[0-60] 不及格
 * 
 * 使用 Switch完成,Switch只能传入int和字符串
 * 
 * @author 16200
 *2021年12月30日上午11:58:22
 */

public class Switch_03 {
	public static void main (String[] args){
		@SuppressWarnings("resource")
		Scanner scanner = new Scanner (System.in);
		System.out.println("请输入成绩,并按回车确认:");
		double score = scanner.nextDouble();
		int score1 =(int) score / 10;
		switch (score1){
			case 10:case 9:
				System.out.println("成绩优秀");	break;
			case 8:case 7:
				System.out.println("成绩良好");	break;
			case 6:
				System.out.println("成绩及格");	break;
			case 0 :case 1 :case 2 :case 3 :case 4 :case 5 :
				System.out.println("成绩不及格");	break;
			default:
				System.out.println("输入无效");	break;
		}
		
	}
}

2.4 for

package _04_For;

/**
 * for循环是 计数循环,在某个范围内重复执行
 * 
 * 语法结构:
 * 		for (表达式1;布尔表达式2;表达式3;){
 * 			循环体,就是需要重复执行的代码
 * 		}
 * 循环三要素 :起始值,终止条件,步长
 * 
 * 表达式1:初始化操作,只执行一次
 * 
 * 表达式2:布尔型,终止条件,控制循环终止
 * 
 * 表达式3:步长,每次循环都执行
 * 
 * 执行过程:
 * 		先执行表达式1然后执行表达式2,若表达式2位TRUE,则执行循环体,
 * 		然后执行表达式2,然后再次执行表达式2,若是TRUE,则执行循环体,
 * 		然后执行表达式3,直到表达式2为FALSE
 * 
 * @author 16200
 *2021年12月30日下午2:07:13
 */
public class For_01 {

	public static void main(String[] args) {
		//循环内的i只能在循环内使用
		for (int i = 0;i <6; i++){
			System.out.println(i);
		}
		//终止条件为true,为死循环
//		for (int i = 0;ture; i++){
//			System.out.println(i);
//		}
		//如要在循坏外使用则需要在循坏外定义
		int i =1;
		for (i = 0;i <6; i++){
			System.out.println(i);
		}
		System.out.println(i);
	}

}

2.4.1  嵌套循环

package _04_For;

/**
 * 嵌套循环 
 * 		双层嵌套可模拟二维表,三层可模拟三维体
 * 		可以把外层循环看成行数,内层循环看成列数
 * 
 * 
 * @author 16200
 *2021年12月30日下午2:22:22
 */
public class For_02 {

	public static void main(String[] args) {
		int d = 0;

				for(int a =0; a < 5; a++){			
					for(int x =0; x< 5; x++){
						d++;
						//println没有ln就不换行
						System.out.print(d+" ");
					}System.out.println();
				}

	}

}

2.4.2 九九乘法表

package _04_For;

/**
 * 九九乘法表
 * 
 * @author 16200
 *2021年12月30日下午2:34:28
 */
public class For_03 {
	public static void main(String[] args) {
		for(int i =1; i < 10; i++){	
			//列小于等于行,造成左下三角
			for(int j =1; j<= i; j++){
				System.out.print(j+"x"+i+"="+(i*j) + " ");
			}System.out.println();
	}
	}
}

2.4.5 1~100累加加和

package _04_For;
/**
 * 1~100累加加和
 * @author 16200
 *2021年12月30日下午2:38:50
 */
public class For_04 {
	public static void main(String[] args) {
		//偶数奇数和i+=2;或取余筛选
		int sum = 0;
		for(int x =0; x<=100; x++){
			sum+=x;
		}
		System.out.println(sum);
	}
}

2.4.6  构建三角

package _04_For;

/**
 * 三角测试:如何构建上三角?
 * 第一行10列
 * 2行9列
 * 可以构建左上三角
 * 右上三角需要输出空格用于固定结构
 * @author 16200
 *2021年12月30日下午2:34:28
 */
public class For_05 {
	public static void main(String[] args) {
第一次循环,第一行
//		//第二次循环,第二行
//		for(int i =10; i >0; i--){	
//			//需要循环输出10个数
//				//需要循环输出9个数
//			for(int j =1; j <= i; j++){
//				System.out.print(0+ "    ");
//			}System.out.println();
//		}//左上三角构建完成
		
	//第一次循环,第一行
			//第二次循环,第二行
			for(int i =10; i >0; i--){	
				//需要循环输出0个空格,10个数,
					//需要循环输出1个空格,9个数
				for(int k=0 ;k<10-i;k++){
					System.out.print(" "+ "   ");
				}//这种每行都输出了10个空格,不同步
				//如果反向倒叙循环一个左下三角是否可行
				//第一行输出0个空格
				// 能否使空格数随行数增加
//					System.out.print(" "+ "    ");//此处加空格使所有行均加空格,不可行
				
				for(int j =1; j <= i; j++){
					System.out.print( 0+"  ");
				}System.out.println();
			}
//			//并行循环是否一起输出   否
//			for(int i =1; i < 10; i++){	
//				//列小于等于行,造成左下三角
//				for(int j =1; j<= i; j++){
//					System.out.print(j+"x"+i+"="+(i*j) + " ");
//				}System.out.println();
//	}
}}
package _04_For;
/**
 * 经查询网络获得解法:
	行计数不变,从1到10
	列计数分空格和数字,和设想一致
			
 * @author 16200
 *2021年12月30日下午4:31:53
 */
public class For_06 {

	public static void main(String[] args) {

//        for(int i=1;i<=10;i++){
//        	//空格计数从最大值入手,计数从比行数小1开始,
//        	//第一行m为9 ,判断m是否大于10-行数,false,没有空格
//        	//第二行m初始依旧为9,而10-行数变为8,输出一个空格
//        	//那么是否可以反过来?   _可行,已在For_05 中实现
//            for(int m=9;m>10-i;m--){
//                System.out.print(" "+"  ");
//            }
//            for(int j=10;j>=i;j--){
//                System.out.print("0"+" ");
//            }
//            System.out.print("\n");
//        }
		
		
//        for(int i=1;i<=10;i++){
//        	//右下三角应同理
//           //仅仅需要将10双方互换
//            for(int j=9;j>=i;j--){
//                System.out.print(1+" ");
//            }
//            for(int m=10;m>10-i;m--){
//                System.out.print(0+" ");
//            }
//            System.out.print("\n");
//        }
		//网络上右下有更优解
		//原理为坐标值大小比较
		//如此左上三角也可用此法更优解出
		System.out.printf("右边直角三角形\n");
		for(int i = 1;i<=9;i++){
		    for(int j = 9;j>0;j--){
		        if(j<=i){
		             System.out.print("0");
		        }
		        else{
		             System.out.print("1");
		        }
		  }
		  System.out.print("\n");
		}
		
		//等腰同理由1/2坐标值实现
        System.out.print("等腰三角形\n");
        for(int i = 1;i<=5;i++){//确定高
        	//第一行由高底等边得出需要的1数目
            for(int m=0;m<5-i;m++){
                System.out.print("1");
            }
            for(int j = 1;j<=i*2-1;j++){//由等边对称得出每行0数目
                System.out.print("0");
            }
            System.out.println();
        }
        //倒等腰与之相反
        System.out.print("倒等腰三角形\n");
        for( int i = 1;i<=5;i++){
            for(int m=4;m>5-i;m--){
                 System.out.print("1");
             }
             for(int j = 1;j<=(5-i)*2+1;j++){
                 System.out.print("0");
              }
              System.out.println();
         }
        //菱形就只是上等腰加下等腰堆砌,其中已个三角形需要减掉一行
        for(int i = 1;i<=5;i++){//确定高
        	//第一行由高底等边得出需要的1数目
            for(int m=0;m<5-i;m++){
                System.out.print("1");
            }
            for(int j = 1;j<=i*2-1;j++){//由等边对称得出每行0数目
                System.out.print("0");
            }
            System.out.println();
        }
        for( int i = 1;i<=4;i++){//此处减掉倒三角一行,变成4行的等腰
            for(int m=4;m>4-i;m--){//且要使第一行就有一个1
                 System.out.print("1");
             }
             for(int j = 1;j<=(4-i)*2+1;j++){
                 System.out.print("0");
              }
              System.out.println();
         }
	}

}

2.5 while

package _05_While;
/**
 * while :真假循环,条件为真,就执行,反之不执行
 *  
 * 语法: 
 * 		while(布尔型){
 * 			循环体;
 * 		}
 * @author 16200
 *2021年12月30日下午3:05:37
 */
public class While_01 {

	public static void main(String[] args) {
		int i = 0 ;
		//计数循环用if,不知循环次数用while
		while (i < 100){
			System.out.println(i);
			i++;
		}

		
	}

}

2.5.1 do...while...

package _05_While;
/**
 * do while 可以保证循环至少执行一次
 * 
 * for和while的执行次数是0~N次 而 dowhile 是 1~N次
 * 
 * @author 16200
 *2021年12月30日下午3:17:09
 */
public class While_02 {

	public static void main(String[] args) {
		int i =1000;
		do {
			System.out.println(i);
			i++;
		}while(i < 100);
	}

}

2.6 break

package _06_Break_Continue;

public class Break_01 {

	public static void main(String[] args) {
		for (int i =0; i<10; i++){
			if(i == 3){
				//终止循环,只能用于内层循环,不能泳衣嵌套循环
				break;
			}
		}
	}

}
package _06_Break_Continue;

public class Break_02 {
	public static void main(String[] args) {
		int d = 0;
		//对外层循环设置名字
		outerfor:for(int a =0; a < 5; a++){			
			for(int x =0; x< 5; x++){
				d++;
				System.out.print(d+" ");
				if(x == 3){
					//终止指定循环
					break outerfor;
				}
			}System.out.println();
		}
	}
}

2.7 continue

package _06_Break_Continue;
/**
 * continue :跳过当前次循环,继续下次
 * 
 * 
 * @author 16200
 *2021年12月30日下午3:27:32
 */
public class Continue_01 {

	public static void main(String[] args) {
		for (int i =0; i<10; i++){
			if(i == 3){
				continue;
			}
			System.out.println(i);
	}
	}}

3 方法

package _07_Method;
/**
 *方法就是很多条语句的集合,把这些代码放到方法中,然后就可以多次使用这个方法
 *
 *方法目的:代码复用,使得程序更加简短清晰,提高开发效率
 * @author 16200
 *2021年12月30日下午3:38:10
 */
//function 函数 Method 方法
//提高代码重用
public class Method_01 {

	public static void main(String[] args) {
		m1();
		m1();
	}
	public static void m1(){
		int sum = 0;
		for(int i =1;i<=100;i++){
			sum+=i;
		}
		System.out.println(sum);
	}
}

3.1 声明,分类,调用

package _07_Method;
/**
 * 声明:
 * 		修饰符列表 返回值类型 方法名 (参数列表){方法体}
 * 
 * 		修饰符列表:可以有,可以没有,可以有多个
 * 			权限修饰:public protected pricate 不写 四选一
 * 			其他修饰:static (静态) ,synchronize
 * 					abstract,final 二选一.....
 * 
 * 		返回值类型 : 11种数据类型中的任意一种 , 如果不需要返回值,写viod
		
		方法名: 方法名字,望文知义,驼峰命名法
 			
 		参数列表:做一件事,需要的必备条件,可以作为入参
 					可以有多个,用,逗号隔开 比如 int a , int b ,int c ....
 					
 					形参:在方法声明时,定义的参数列表
 					实参:方法调用时,实际传入的数据
 					
 		方法体: 需要执行的代码
 			注意:return
 				如果有返回值类型的方法中,方法必须有return语句
 					比如
 					 public static int m1(){
 						return 1;//这里的1 只要是int值就可,因为返回值为int类型 
 					}
 				如果没有返回值 那么 可以写return,也可不写,就算写return也返回不了数据,只能终止运行
 				public static void m1(){
 						return ;//只能终止方法运行
 					}
 					
 	方法分类:
 		1 静态方法:使用static修饰的方法,是静态方法
 		2 成员方法: 没有static 修饰的方法是成员方法
 		3 构造方法 :创建对象使用
 	调用: 
 		静态方法:类名.静态方法名(参数),同类中可以省略//类名用于定位
 		成员放法:对象引用.成员方法名(参数)
 	
 	方法不调用不执行,调用才执行,并把结果返回到调用处
 	
 	编写方法 只考虑功能的实现,最终方法被用来做什么,与声明无关
 		
 * @author 16200
 *2021年12月30日下午3:41:00
 */
public class Method_02 {

	 public static int m2(int a,int b){
			return a+b;//这里的1 只要是int值就可,因为返回值为int类型 
		}
	public static void main(String[] args) {
		Method_01.m1();//调用不同类的方法
		int i = 1 ;
		int j = 2 ; 
		System.out.println(m2(i,j));//调用同类方法
		
	}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值