java基础笔记系列_Day03

一、三元运算符

  1. 格式
    关系表达式 ? 表达式1 : 表达式2
    先判断关系表达式,结果为true返回表达式1,否则返回表达式2.
    1:表达1和表达式2可以不是表达式,可以是字符,字符串等,但是数据类型需要相同。
    2:接收类型、表达式1、表达式2数据类型需要一致。例如:字符串需要String接收。

案例:三个和尚

    public class Test03{
    	public static void main(String[] args){
    		//定义三个身高变量
    		int height1 = 150;
    		int height2 = 210;
    		int height3 = 165;
    		//判断三个中最小值
    		int tempheight = height1 < height2 ? height1 : height2;
    		int minweight = tempheight < height3 ? tempheight : height3;
    		//输出最小值
    		System.out.println("最矮身高是"+minweight+"厘米");
    	}
    }

二、数据输入

格式
1.导包,放在类名的上边。 import java.util.Scanner
2.创建对象 Scanner sc = new Scanner(System.in)
3.接受数据 int i = sc.nextInt()

    //导包
    import java.util.Scanner;
    public class Test04{
    	public static void main(String[] args){
    		//创建对象
    		Scanner sc = new Scanner(System.in);
    		System.out.print("输入数据:");
    		//接收数据
    		int i = sc.nextInt();
    		System.out.print("输入的数据是:" + i);
    	}
    }

升级版三个和尚:

    //导包
    import java.util.Scanner;
    //程序入口
    public class Test05{
    	public static void main(String[] args){
    		//创建对象
    		Scanner sc = new Scanner(System.in);	
    		//输入三个和尚的身高
    		System.out.print("第一个和尚的身高是:");
    		int height1 = sc.nextInt();
    		System.out.print("第二个和尚的身高是:");
    		int height2 = sc.nextInt();
    		System.out.print("第三个和尚的身高是:");
    		int height3 = sc.nextInt();		
    		//比较三个和尚的身高并取最小值
    		int tempheight = height1 < height2 ? height1 : height2;
    		int minheight = height3 < tempheight ? height3 : tempheight;
    		System.out.print("三个和尚的最小身高是:"+minheight+"cm");
    	}
    }

三、顺序结构

大多数代码都按照顺序结构执行

四、if结构

1.用法一:if(判断){语句体}
if后判断关系为true,则执行语句体,否则不执行语句体继续顺序执行。

示例:

    public class Test06{
    	public static void main(String[] args){
    		int a = 10;
    		int b = 11;
    		if (a == b)
    		{
    			System.out.println("a和b相等");
    		}
    		System.out.println("结束");
    	}
    }

2.用法二:if(判断){语句体1}else{语句体2}
if后判断关系为true,则执行语句体1,否则执行语句体2,然后继续顺序执行。

案例:判断奇偶

    //导包
    import java.util.Scanner;
    //程序入口
    public class Test07{
    	public static void main(String[] args){
    		//创建对象
    		Scanner sc = new Scanner(System.in);
    		System.out.print("输入一个整数:");
    		//键盘录入接收数据
    		int i = sc.nextInt();
    		//判断奇偶
    		if (i%2==0){
    			System.out.print(i+"是偶数");
    		}else{
    			System.out.print(i+"是奇数");
    		}
    	}
    }

此程序只能判断int类型取值范围内的整数。

案例:润/平年判断
闰年条件:
1.能被4整除,不能被100整除。
2.能被400整除

    //导包
    import java.util.Scanner;
    public class Test08{
    	public static void main(String[] args){
    		//创建对象
    		Scanner sc = new Scanner(System.in);
    		System.out.print("输入年份:");
    		//键盘接收数据
    		long year = sc.nextLong();
    		//判断
    		if((year%4==0&&year%100!=0)||(year%400==0)){
    			System.out.print(year+"是闰年");
    		}else{
    			System.out.print(year+"是平年");
    		}
    	}
    }

3.用法三:if(判断){语句体1} else if(判断){语句体2} else if(判断){语句体3} else if(判断){语句体3}…else{}

顺序执行语句,直到判断为true或者运行最后一个语句体,结束if语句

案例:判断周一到周日

    //导包
    import java.util.Scanner;
    public class Test09{
    	public static void main(String[] args){
    		//创建对象
    		Scanner sc = new Scanner(System.in);
    		System.out.print("请输入一个数(0~7)");
    		int week = sc.nextInt();
    		//判断
    		if (week < 0 || week > 7){
    			System.out.println("输入错误,请重新输入。");
    		}else if (week == 1){
    			System.out.println("周一");
    		}else if (week == 2){
    			System.out.println("周二");
    		}else if (week == 3){
    			System.out.println("周三");
    		}else if (week == 4){
    			System.out.println("周四");
    		}else if (week == 5){
    			System.out.println("周五");
    		}else if (week == 6){
    			System.out.println("周六");
    		}else {
    			System.out.println("周日");
    		}
    	}
    }

数据测试时需要测试三种数据:正确数据,边界数据,错误数据

案例:考试奖励

    //导包
    import java.util.Scanner;
    public class Test10{
    	public static void main(String[] args){
    		//创建对象
    		Scanner sc = new Scanner(System.in);
    		System.out.print("输入小明的分数:");
    		int score = sc.nextInt();
    		//判断奖励
    		if (score<0 || score>100){
    			System.out.print("输入错误,请重新输入");
    		}else if (score<=100 && score>=95){
    			System.out.print("奖励一辆自行车");
    		}else if (score<=94 && score>=90){
    			System.out.print("奖励游乐场玩一次");
    		}else if (score<=89 && score>=85){
    			System.out.print("奖励变形金刚");
    		}else if (score<=84){
    			System.out.print("无");
    		}
    	}
    }

五、switch结构

1.表达式:byte short int char 枚举 String(不能出现long类型)
2.格式

		switch(表达式){
			case 值1:
			break;//跳出当前switch语句
			case 值2:
			break;//如果缺少了break,则发生case穿透,顺序执行下一个case
			case 值3:
			break;
			default:
			break;//default放在最后的时候可以省略这个break
}

3.与if区别:
if可以是判断(大于、小于、不等于),switch只能是等于。

4.switch为什么不允许打断点?
待定

5.case穿透案例:春夏秋冬
case穿透:没有break时,继续执行语句。

    //导包
    import java.util.Scanner;
    public class Test11{
    	public static void main(String[] args){
    		//创建对象
    		Scanner sc = new Scanner(System.in);
    		//输入月份
    		System.out.print("请输入月份");
    		int month = sc.nextInt();
    		switch(month){
    			case 1:
    			case 2:
    			case 12:
    			System.out.print(month + "月是冬季");
    			break;
    			case 3:
    			case 4:
    			case 5:
    			System.out.print(month + "月是春季");
    			break;
    			case 6:
    			case 7:
    			case 8:
    			System.out.print(month + "月是夏季");
    			break;
    			case 9:
    			case 10:
    			case 11:
    			System.out.print(month + "月是秋季");
    			break;
    			default:
    			System.out.print("您输入的月份有误,请重新输入");
    		}
    	}
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值