学习笔记之JavaSE(4)--Java基础语法3

今天学习的内容是流程控制

一、if语句

最常见的条件判断语句,有if、if-else、if-else if-else if...-else三种形式,当if-else运算后一定产生具体结果时最好使用三元运算符代替,示例程序:

public class Test11 {

	public static void main(String[] args){
		
		//练习1:根据用户输入,判断学生成绩
		Scanner s1=new Scanner(System.in);
		System.out.print("请输入成绩:");
		int score=s1.nextInt();
		s1.close();
		if(score>=0&&score<60){
			System.out.println("您的成绩为不及格");
		}else if(score>=60&&score<70){
			System.out.println("您的成绩为D");
		}else if(score>=70&&score<80){
			System.out.println("您的成绩为C");
		}else if(score>=80&&score<90){
			System.out.println("您的成绩为B");
		}else if(score>=90&&score<=100){
			System.out.println("您的成绩为A");
		}else{
			System.out.println("您的成绩不符合要求");
		}
		System.out.println("判断完成!");
		
		//练习2:根据用户输入,判断季节
		try{
			BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
			System.out.print("请输入当前月份:");
			int month=Integer.parseInt(in.readLine());
			in.close();
			if((month==3)||(month==4)||(month==5)){
				System.out.println(month+"月份是春季");
			}else if((month==6)||(month==7)||(month==8)){
				System.out.println(month+"月份是夏季");
			}else if((month==9)||(month==10)||(month==11)){
				System.out.println(month+"月份是秋季");
			}else if((month==12)||(month==1)||(month==2)){
				System.out.println(month+"月份是冬季");
			}else{
				System.out.println(month+"月份没有对应季节");
			}
		}catch(IOException e){
			e.printStackTrace();
		}
		//注意练习1和练习2不能同时运行,只能分别运行
	}
}

注意:

  1. 同类条件的判断可以使用多个if/if-else,也可以使用if-else if-else if...-else,不过后者更好,有一个条件成立立刻终止判断,节约性能
  2. 不同类条件的判断必须使用多个if/if-else

二、switch语句

也是条件判断语句,有两点需要注意:

  • switch语句中表达式必须为整型、char、枚举(Java5新特性)或字符串(Java7新特性
  • case语句能够堆叠在一起,为一段代码形成多重匹配,即只要符合多种条件的一种,就执行那段特别的代码
示例程序:
public class Test12 {

	public static void main(String[] args){
		Scanner s=new Scanner(System.in);
		System.out.print("请输入运算方法:");
		String str=s.nextLine();
		System.out.print("请输入一个整数a:");
		int a=s.nextInt();
		System.out.print("请输入一个整数b:");
		int b=s.nextInt();
		s.close();
		switch(str){
		case "加法":
		case "加":
			System.out.println("a+b="+(a+b));
			break;
		case "减法":
		case "减":
			System.out.println("a-b="+(a-b));
			break;
		case "乘法":
		case "乘":
			System.out.println("a*b="+(a*b));
			break;
		case "除法":
		case "除":
			System.out.println("a/b="+(a/b));
			break;
		case "取模":
		case "求模":
			System.out.println("a%b="+(a%b));
			break;
		default:
			System.out.println("无效运算方法");
		}
	}
}

三、while语句

有while和do-while两种形式。循环语句需要掌握累加思想计算器思想(如示例程序),有时候需要使用无限循环,使用while(true)即可,示例程序:
public class Test13 {

	public static void main(String[] args){
		//while语句
		int x=1;
		while(x<4){
			System.out.println("第"+x+"次循环");
			x++;
		}
		
		//do-while语句,了解即可(与while唯一的区别就是,do-while至少会执行一次do)
		int i = 1;
		do{
			System.out.println("第"+i+"次循环");//虽然不满足条件式,但是还是会执行一次
			i++;
		}while(i<1);
		
		//练习1:获取1-10数字的和(累加思想)
		int m=1;
		int sum=0;
		while(m<=10){
			sum+=m;
			m++;
		}
		System.out.println("1-10数字的和为"+sum);
		
		//练习2:1-100之间 6的倍数出现的次数(计算器思想)
		int n=1;
		int count=0;
		while(n<=100){
			if(n%6==0){
				count++;
				System.out.println(n+"是6的倍数");
			}
			n++;	
		}
		System.out.println("一共有"+count+"个");
	}
}


四、for语句

循环语句,和while语句可以互换使用,总结while语句和for语句的特点:
  • for循环结束后,参与循环的变量即销毁,而while循环结束后,参与循环的变量可以继续使用
  • for语句实现无限循环的方法是for(;;),与while(true)没有任何区别
  • for语句的初始化部分可以具有任意数量的同类型变量定义(通过逗号操作符)
  • for循环和while循环最好都要掌握累加思想和计算器思想,for循环还常用嵌套for循环
示例程序:
public class Test14 {

	public static void main(String[] args){
		//for语句
		for(int x=1;x<4;x++){
			System.out.println("第"+x+"次循环");
		}
		
		//练习1:获取1-10数字的和(累加思想)
		int sum=0;
		for(int m = 1;m <= 10;m++){
			sum+=m;
		}
		System.out.println("1-10数字的和为"+sum);
		
		//练习2:1-100之间 6的倍数出现的次数(计算器思想)
		int count=0;
		for(int n = 1;n <= 100;n++){
			if(n%6==0){
				System.out.println(n+"是6的倍数");
				count++;
			}
		}
		System.out.println("一共有"+count+"个");
		
		//练习3:打印九九乘法表(循环嵌套)
		for(int i=1;i<10;i++){//外循环控制函数
			for(int j=1;j<=i;j++){//内循环控制列数
				System.out.print(i+"*"+j+"="+i*j+"\t");
			}
			System.out.println();
		}	
	}
}
注意Java5之后引用了一种新的for语法--foreach,不过这种语法遍历的对象必须是数组、列表或者集,foreach的用法相当简单:foreach(类 引用 : 数组/集合){},下面对比一下for与foreach的区别:
  • for可以完成对某语句进行多次执行,而foreach必须有被遍历的数组、列表或集
  • 如果要对数组的下标进行操作,使用for


五、continue和break

最后介绍一下continue和break,它们的作用和特点为:
  • break:跳出最内层循环
  • continue:结束本次循环,继续下一次循环
  • break可以使用在switch语句和循环语句,一般和if语句联用
  • continue使用在循环语句,一般和if语句联用
示例程序:
public class Test15 {

	public static void main(String[] args){
		//break:跳出最内层循环(输出结果是三行一列的*)
		for(int i=1;i<4;i++){
			for(int j=1;j<4;){
				System.out.print("*");
				break;
			}
			System.out.println("");
		}
		
		//continue:结束本次循环,继续下一次循环(输出结果为i=1 i=3,i=2这次循环被结束了)
		for(int i=1;i<4;i++){
			if(i%2==0){
				continue;
			}
			System.out.println("i="+i);
		}
	}
}
需要注意的是:break和continue可以使用标签跳出外层循环或者结束本次外层循环。在Java中,标签唯一起作用的地方就是循环语句之前,并且使用标签的唯一理由就是有嵌套循环存在,并且想从多层嵌套中break或者continue。但是标签不推荐使用,了解即可

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值