Java入门 Day004

1、if else语句

应用场景:针对两种结果进行判断
if语句的格式2:
if(关系表达式) {
语句体1;
}else {
语句体2;
}

执行流程:
1)首先判断表达式是否成立
2)如果成立,语句1;
3)如果不成立,执行语句2

//导包
import java.util.Scanner;
class IfDemo2{
	public static void main(String[] args){
		
		//创建键盘录入对象
		Scanner sc = new Scanner(System.in);
		
		//提示
		System.out.println("请输入一个要判断数据: ");
		//接收
		int a = sc.nextInt() ;
		
		//针对数据进行判断
		if(a>=20){
			System.out.println("a大于等于20") ;
		}else{
			System.out.println("a小于20");
		}
		
		System.out.println("over");
	}
}

2、if语句的格式2,和三元运算符的区别?

三元运算符这对的是某个数据的数据值在进行判断;
(表达式)?true的结果:false的结果;
if语句格式2:
针对两个结果进行判断,输出一个结果(可能是一个输出语句,也可能是具体的值)

三元运算符能够使用的,一定可以使用if;但是if使用,不一定能够收三元!
开发过程中,没有特别要求,都是用If语句!
奇数和偶数的判断

class IfDemo3{
	public static void main(String[] args){
		
		//定义变量
		int a  = 100 ;
		
		//if语句判断:格式2
		if(a %2 ==0){
			System.out.println("当前数据是偶数");
		}else{
			System.out.println("当前数据是奇数");
		}
		
		//使用三元运算符 
		//String:字符串类
		//String s = 
		//	(a % 2 == 0)? (System.out.println("当前数据是偶数");):(System.out.println("当前数据是奇数");) ;
	}
}

if格式3:
if(关系表达式1) {
语句体1;
}else if (关系表达式2) {
语句体2;
}

else {
语句体n+1;
}

执行流程:
1)先判断表达式1是否成立,成立,执行语句1
2)如果不成立,在此判断表达式2是否成立,成立,执行语句2
3)…最终上面的结果都不匹配,执行else中的语句n+1;
if三种格式的应用场景:
if格式1:针对单个情况判断
if格式2:针对两种情况判断 (开发中使用居多)
if格式3:针对两个以上的判断

需求:
	
		90-100 优秀
		80-90 好
		70-80 良
		60-70 及格
		60一下 不及格
//导包
import java.util.Scanner;
class IfDemo4{
	public static void main(String[] args){
		//创建键盘录入对象
		Scanner sc = new Scanner(System.in); 
		
		System.out.println("请输入当前学生成绩: ");
		//接收
		int score  = sc.nextInt() ;
		
		//经过判断,使用if语句格式3
		/*
		if(score>=90 && score <=100){
			System.out.println("该学生成绩-优秀");
		}else if(score >= 80 && score < 90){
			System.out.println("该学生成绩-较好");
		}else if( score >=70 && score <80){
			System.out.println("该学生成绩-良好");
		}else if(score >= 60 && score<70){
			System.out.println("该学生成绩-及格");
		}else{
			System.out.println("该学生成绩-不及格");
		}
		*/
		
		/*
			上述代码存在漏洞,当用户输入超过当前100,或者低于0,都是非法数据
			判断的时候
				测试 :"错误数据","边界数据","正确的数据"
			使用这种方式进行改进!
		*/
		if(score<0 || score>100){
			System.out.println("当前数据是非法数据...");
		}else if(score>=90 && score <=100){
			System.out.println("该学生成绩-优秀");
		}else if(score >= 80 && score < 90){
			System.out.println("该学生成绩-较好");
		}else if( score >=70 && score <80){
			System.out.println("该学生成绩-良好");
		}else if(score >= 60 && score<70){
			System.out.println("该学生成绩-及格");
		}else{
			System.out.println("该学生成绩-不及格");
		}
		
	}
}

3、switch语句

	switch语句格式: 
	switch(表达式) {
		case 值1:
			语句体1;
			break;			(中断,结束的意思)
		case 值2: 
			语句体2;
			break; 
		…
		default: 			(默认的意思)
		语句体n+1;
		break;
	}

执行流程:
1)表达式中的值和case中的 值1进行比较,匹配成功,就执行语句1,遇见break,switch 结束了
2)依次case值2,进行比较,成立,执行语句2,语句结束…
3)…如果上述都不匹配,执行default中的语句,break结束.

需求:键盘录入一个数据,输入当前星期数

格式解释:
switch ----->启用当前switch语句
表达式------>一般接收具体的变量
表达式的类型一般情况:byte,short,int,char
如果是jdk1.5以后,表达式的类型可以是"枚举"
如果是jdk1.7以后,表达式的类型可以是String类型 (String:是一个引用类型)
case语句---->需要将当前的case的值和switch中表达式进行比较,是否匹配
break语句—> 表示结束,中断 switch语句
default ---->case后面都不匹配,执行default

面试题:
switch中的表达式可以是byte类型吗? 是
switch中的表达式可以是String类型吗? jdk1.7以后可以跟String
switch中的表达式可以是枚举类型吗? jdk1.5以后可以是枚举

*/
//导包
import java.util.Scanner ;
class SwitchDemo{
	public static void main(String[] args){
		//创建键盘录入对象
		Scanner sc = new Scanner(System.in);
		
		System.out.println("请输入数据(1-7):");
		int week = sc.nextInt() ;
		
		//使用switch语句
		switch(week){
			case 1:
				System.out.println("星期一");
				break ;
			case 2:
				System.out.println("星期二");
				break ;
			case 3:
				System.out.println("星期三");
				break ;
			case 4:
				System.out.println("星期四");
				break ;
			case 5:
				System.out.println("星期五");
				break ;
			case 6:
				System.out.println("星期六");
				break ;
			case 7:
				System.out.println("星期日");
				break ;
			default :
				System.out.println("非法数据");
				break ;
				
		}
		
	}
}

switch语句的注意事项:
1)在Java语言中,case后面的值只能是常量,不能是变量 ---->前端:javascript (js)
2)关于switch语句的中break(属于跳转控制语句中的一种)

break一般情况下建议不要省略,如果没有书写break语句,会出现case穿透现象!
3)关于default语句的位置问题
a)default语句中的break可以忽略不写,但是不建议
b)可以在switch语句中的任何位置,但是还要遵循switch的按照顺序进行执行(case中都不匹配,才能执行default)
c)如果default语句在语句中,break不能省略!
4)switch语句的结束条件:
a)语句break,语句结束
b)程序默认执行末尾,语句结束!

//导包
import java.util.Scanner ;
class SwitchDemo2{
	public static void main(String[] args){
			//创建键盘录入对象
		Scanner sc = new Scanner(System.in);
		
		System.out.println("请输入数据(1-7):");
		int week = sc.nextInt() ;
		
		//定义变量
		//int number = 3 ;
		
		//使用switch语句
		switch(week){
			default :
				System.out.println("非法数据");
				//break ;
			case 1:
				System.out.println("星期一");
				break ;
			case 2:
				System.out.println("星期二");
				break ;
			//编译报错:需要常量表达式
			//case number:
			case 3 :
				System.out.println("星期三");
				//break ;
			case 4:
				System.out.println("星期四");
				break ;
			case 5:
				System.out.println("星期五");
				break ;
			case 6:
				System.out.println("星期六");
				break ;
			case 7:
				System.out.println("星期日");
				break ;
			
			
				
		}
	}
	
}

拓展题

看程序,写结果
int x = 2;
int y = 3;
switch(x){
default:
y++;
break;
case 3:
y++;
case 4:
y++;
当前y的值是多少?



int a = 2; 
int b = 3; 
switch(a){
	default: 
		b++;
	case 3: 
		b++;
	case 4: 
		b++;
当前b的值是多少?
*/
class SwitchTest{
	public static void main(String[] args){
		int x = 2;
		int y = 3;
		switch(x){
			default: 	
				y++;	//3 --->4
				break;
			case 3:
				y++;
			case 4: 
				y++;
		}
		System.out.println("y:"+y);
		System.out.println("------------") ;
		
		int a = 2; 
		int b = 3; 
		switch(a){
			default: 
				b++;	//3 --->4
			case 3: 
				b++;	//5
			case 4: 
				b++;	//6
			}
		System.out.println("b:"+b);
	}
	
}

4、for循环语句

for循环语句
	for(初始化语句;条件表达式;控制体语句){
			语句体;
	}	
	
执行流程:
	1)执行初始化语句---->给变量进行赋值 
	2)判断条件表达式是否成立,如果成立,true--->执行语句体
	3)执行控制体语句,----->再次判断条件是否成立,如果成立,继续执行语句体
	4)一直执行到条件判断表达式不成立,即为false的时候,循环语句结束..
*/
class ForDemo2{
	public static void main(String[] args){
	
		//需要输出10次"helloworld";
		//原始的做法
		System.out.println("helloworld") ;
		System.out.println("helloworld") ;
		System.out.println("helloworld") ;
		System.out.println("helloworld") ;
		System.out.println("helloworld") ;
		System.out.println("helloworld") ;
		System.out.println("helloworld") ;
		System.out.println("helloworld") ;
		System.out.println("helloworld") ;
		System.out.println("helloworld") ;
		System.out.println("-----------------------") ;
		
		//使用for循环来完成
		for(int x = 1;x<=10;x++){
		//1)先给x变量赋值  1
		//2)1<=10---->System.out.println("helloworld") ;
		//3)1+1 =2 <=10 --->System.out.println("helloworld") ;
		///... x = 10 +1 = 11 <=10
			System.out.println("helloworld") ;
		}
	}
}

打印1-10数字

for(int x = 1 ; x <= 10 ; x ++){
			//输出变量x
			//1)int x =1
			//2)x <=10
			System.out.println(x) ; //1 2...10
		}

循环中的求和思想
分析:
0+1 = 1
1+2=3
3 +3 = 6
6 + 4 = 10
10 + 5 = 15

规律:需要两个加数
	第一个加数:是前面所有数据之和
	第二个加数:值在变化,1,2,3...10...
class ForDemo4{
	public static void main(String[] args){
		
		//原始做法
		System.out.println(1+2+3+4+5+6+7+8+9+10);
		System.out.println("-------------------") ;
		
		//求和思想:
		//定义一个最终结果变量
		int sum = 0 ;
		//使用for循环
		for(int x = 1 ; x <= 10 ; x ++){
			//将sum和x相加然后结果赋值sum
			sum += x ;
		}
		
		//输出sum变量
		System.out.println("1-10之间的和是:"+sum) ;
	}
	
}

阶乘计算

for循环练习
求5的阶乘
阶乘的概念:

		n!=n*(n-1)!
	 	n!=n*(n-1)...*2*1..
		5= 5 * 4 * 2 * 1 ;
class ForDemo5{
	public static void main(String[] args){
		
		//使用循环的思想
		//定义结果变量
		int jc = 1 ;
		for(int x = 2 ; x <=5 ; x ++){
			//jc*=
			jc *= x ;  //1 <=5  
					  //2)jc = jc * x  = 1 * 1 
					//3)jc = jc * x = 2 * 1
		}
		//输出
		System.out.println(jc);
		
	}
}

打印水仙花数
需求:输出所有的”水仙花数”

水仙花数:
	指定的是三位数,
	特点:个位数的立方+十位数的立方+百位数的立方之和是当前这个数据本身
		举例
			153=1*1*1 +5*5*5 +3*3*3
			
		思路:
			1) 使用for循环语句:水仙花数--->告诉一个范围
			2)想办法获取每个位上的数据 
				//定义三个变量
				int ge = 使用当前数据(X)对10取模(求余)
				int shi = 使用当前数据(X)先取整 /10 对10取模
				int bai = 使用当前数据(X)/10 /10 对10取模
			   
			3)符合当前条件 类似于1*1*1 +5*5*5 +3*3*3 是数据本身
			4)输出
class ForDemo6{
	public static void main(String[] arg
	s){
	
		//水仙花数--->告诉一个范围  100-1000
		for(int x = 100;x <1000 ; x ++){
			//使用x数据获取每一个位上的数
			//定义三个变量ge,shi,bai接收 当前的数据值
			int ge = x %10;
			int shi = x /10 % 10 ;
			int bai = x /10/10 %10 ;
			
			//判断:符合当前条件 :ge*ge*ge+shi*shi*shi+bai*bai*bai ==x
			if(x == ge*ge*ge+shi*shi*shi+bai*bai*bai){
				//满足条件
				//输出
				System.out.println(x);
				/*
					153
					370
					371
					407
				*/
			}
			
		}
	}
}

统计题:

请统计1-1000之间同时满足如下条件的数据有多少个:
对3整除余2
对5整除余3
对7整除余2

public class Test {

	public static void main(String[] args) {
		int count=0;
		for(int i=1;i<=1000;i++) {
			if(i%3==2&&i%5==3&&i%7==2) {
				System.out.println(i);
				count++;	
			}			
		}
	
		System.out.println(count++);
	}

}

while循环

while循环语句格式: 
		基本格式 
		 while(判断条件语句) { 
		  循环体语句; 
		 }
	扩展格式 (使用扩展格式)
        初始化语句; 
		while(判断条件语句) {
		循环体语句; 
		控制条件语句;
     }
     比如:int x=10;
     while(x>1){
     	System.out.println("helloworld");
     	x++;
     }
     
执行流程:
	1)初始化语句进行变量赋值
	2)判断条件是否成立,--->成立,执行循环体语句--->执行控制体语句
	3)再次判断条件是否成立--->成立----->依次
	4)一直条件条件不成立,结束!
	
}
需求:控制体打印10次helloworld
	使用while循环的方式,获取"所有的水仙花数"
						获取1-100之间的和
class WhileDemo{
		public static void main(String[] args){
			//for循环
			for(int x = 1 ; x <= 10 ; x ++){
				System.out.println("helloworld");
			}
			
			System.out.println("-----------------------");
			
			//使用while循环
			int y = 1 ;
			while(y<=10){
				System.out.println("helloworld");
				//while循环不要忘记 控制体语句,否则"死循环"
				y ++ ;
			}
		}
}

for循环和while循环的区别 ?
1)如果明确循环的次数,使用for循环,否则使用while循环
2)从内存角度考虑,如果循环结束后,还想去访问那个变量,使用while循环

class WhileDemo2{
	public static void main(String[] args){
		
		//for循环
		//控制体输出10次"我爱高圆圆"
		for(int x = 1 ; x <= 10; x ++){
			//x的生命周期 :x叫局部变量(在方法中定义的变量)---->一旦for循环结束--->x变量会被GC(垃圾回收器回收)
			System.out.println("Hello");
		}
		
		System.out.println("----------------------");
		
		//当前这个位置.访问变量x
		//编译不通过:报错 找不到符号
		//System.out.println(x) ;
		
		//while循环
		int j = 1 ;
		while(j<=10){
			System.out.println("Hello");
			j ++ ;
		}
		//访问这个变量
		System.out.println(j);
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值