栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

第二章、Java基本语法(3):程序流程控制

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

第二章、Java基本语法(3):程序流程控制

文章目录

前言一、顺序结构二、分支结构 if-else

1、if-else的三种形式:2、if-else例子3、if-else的一个测试题 三、分支结构 switch-case结构

1、结构形式2、说明:3、基础使用 四、循环语句---for循环五、循环语句---while循环六、魂环语句---do while循环七、嵌套循环八、特殊关键字:break,continue总结


前言

主要是if-else和switch-case语句、for、while、do—whlie、循环的嵌套、break 和continue的使用。


一、顺序结构

程序从上到下逐行地执行,中间没有任何判断和跳转。

二、分支结构 if-else

根据条件,选择性地执行某段代码。有if…else和switch-case两种分支语句。

1、if-else的三种形式:

    if(条件表达式){
    执行代码块;
    }

    if(条件表达式){
    执行代码块1;
    }
    else{
    执行代码块2;
    }

    if(条件表达式1){
    执行代码块1;
    }
    else if (条件表达式2){
    执行代码块2;
    }
    ……
    else{
    执行代码块n;
    }

2、if-else例子

代码如下(示例):

package shangguigu.javabase;
  
import java.util.Scanner;

 class IfElseTest {
	public static void main(String[] args) {
		//Scanner的实例化
		Scanner scan = new Scanner(System.in);
		
		//输入分数
		System.out.println("请输入分数:");
		double mark = scan.nextDouble();
		System.out.println(mark);
		if(mark>100&&mark<0) {
			System.out.println("请输入正确的成绩!!!");
		}else if(mark==100.0) {
			System.out.println("奖励一辆BMW");
		}else if(mark>80){
			System.out.println("奖励一台iPhone xs");
		}else if(mark>=60) {
			System.out.println("奖励一个ipad");
		}else {
			System.out.println("不及格奖励个屁。");
		}	
	}
}
3、if-else的一个测试题

编写程序:Java由键盘输入三个整数分别存入变量mun1、mun2、mun3,对他们进行排序(使用if-else if–else),并且从小到大输出。

package shangguigu.javabase;


import java.util.Scanner;
public class IfTest2 {
		public static void main(String[] args) {
			Scanner scan = new Scanner(System.in);
		
			System.out.println("请输入三个整数:");
			int mun1 = scan.nextInt();
			System.out.println(mun1+":输入成功");
			int mun2 = scan.nextInt();
			System.out.println(mun2+":输入成功");
			int mun3 = scan.nextInt();
			System.out.println(mun3+":输入成功");
			System.out.println("排序结果为:");
			int change = 0;
			if(mun1mun2) {
					System.out.println(mun1+"<"+mun2+"<"+mun3);
				}else if(mun3 

三、分支结构 switch-case结构

说明:

    凡是可以使用switch-case的结构,都可以转换为if-else。反之,不成立。我们写分支结构时,当发现既可以使用switch-case,(同时,switch中表达式的取值情况不太多),
    又可以使用if-else时,我们优先选择使用switch-case。原因:switch-case执行效率稍高。
1、结构形式
switch(表达式){
case 常量1:
	执行语句1;
	//break;

case 常量2:
	执行语句2;
	//break;
...
default:
	执行语句n;
	//break;
}
2、说明:

① 根据switch表达式中的值,依次匹配各个case中的常量。一旦匹配成功,则进入相应case结构中,调用其执行语句。当调用完执行语句以后,则仍然继续向下执行其他case结构中的执行语句,直到遇到break关键字或此switch-case结构末尾结束为止。
② break,可以使用在switch-case结构中,表示一旦执行到此关键字,就跳出switch-case结构
③ switch结构中的表达式,只能是如下的6种数据类型之一: byte 、short、char、int、枚举类型(JDK5.0新增)、String类型(JDK7.0新增)
④ case 之后只能声明常量。不能声明范围。
⑤ break关键字是可选的。
⑥ default:相当于if-else结构中的else,default结构是可选的,而且位置是灵活的。

3、基础使用

1、

String season = "summer";
		switch (season) {
		case "spring":
			System.out.println("春暖花开");
			break;
		case "summer":
			System.out.println("夏日炎炎");
			break;
		case "autumn":
			System.out.println("秋高气爽");
			break;
		case "winter":
			System.out.println("冬雪皑皑");
			break;
		default:
			System.out.println("季节输入有误");
			break;
		}

输出结果:

夏日炎炎

2、例题:对学生成绩大于60分的,输出“合格”。低于60分的,输出“不合格”。
说明:如果switch-case结构中的多个case的执行语句相同,则可以考虑进行合并。

		int score = 78;
		switch(score / 10){
		case 0:
		case 1:
		case 2:
		case 3:
		case 4:
		case 5:
			System.out.println("不及格");
			break;
		case 6:
		case 7:
		case 8:
		case 9:
		case 10:
			System.out.println("及格");
			break;

		}
		//更优的解决方案:
		switch(score / 60){
		case 0:
			System.out.println("不及格");
			break;
		case 1:
			System.out.println("及格");
			break;
		}

输出结果

及格
及格

3、编写程序:从键盘上输入一个日期的年月日,要求通过程序输出输入的日期为当年的第多少天。

		Scanner scan = new Scanner(System.in);
		System.out.println("请输入year:");
		int year = scan.nextInt();
		System.out.println("请输入month:");
		int month = scan.nextInt();
		System.out.println("请输入day:");
		int day = scan.nextInt();
		
		int sumday=0;
		switch(month) {
		case 12 : sumday+= 30;
		case 11 : sumday+= 31;
		case 10 : sumday+= 30;
		case 9 : sumday+= 31;
		case 8 : sumday+= 31;
		case 7 : sumday+= 30;
		case 6 : sumday+= 31;
		case 5 : sumday+= 30;
		case 4 : sumday+= 31;
		case 3 : 
			if((year%4==0)&&(year%100!=0)||(year%400==0)) {
				sumday+= 29;
			}else {
				sumday += 28;
			}
		case 2 : sumday+= 31;
		case 1 : sumday+= day;
		}
		
		System.out.println(year+"年的"+month+"月"+day+"日"+"是当年的第"+sumday+"天");

运行结果是:

请输入year:
2017
请输入month:
2
请输入day:
3
2017年的2月3日是当年的第34天
四、循环语句—for循环

示例:

package shangguigu.javabase;

public class ForTest1 {
  public static void main(String[] args) {
	  for(int i=1;i<=150;i++) {
		  System.out.print(i+"  ");
		  if(i%3==0) {
			  System.out.print("foo");
		  }
		  if(i%5==0) {
			  System.out.print("biz");
		  }
		  if(i%7==0) {
			  System.out.print("baz");
		  }
		  System.out.println();
	  }
  }
}

输出结果:

1  
2  
3  foo
4  
5  biz
6  foo
7  baz
8  
9  foo
10  biz
11  
12  foo
13  
14  baz
15  foobiz
16  
17  
18  foo
19  
20  biz
21  foobaz
......

题目:输入两个正整数m和n,求其最大公约数和最小公倍数。
比如:12和20的最大公约数是4,最小公倍数是60。
说明:break关键字的使用:一旦在循环中执行到break,就跳出循环

import java.util.Scanner;
class ForTest{
	public static void main(String[] args){
	
		Scanner scan = new Scanner(System.in);
		System.out.println("请输入第一个正整数:");
		int m = scan.nextInt();
		System.out.println("请输入第二个正整数:");
		int n = scan.nextInt();
		//获取最大公约数
		//1.获取两个数中的较小值
		int min = (m <= n)? m : n;
		//2.遍历
		for(int i = min;i >= 1 ;i--){
			if(m % i == 0 && n % i == 0){
				System.out.println("最大公约数为:" + i);
				break;//一旦在循环中执行到break,就跳出循环
			}
		}
		//获取最小公倍数
		//1.获取两个数中的较大值
		int max = (m >= n)? m : n;
		//2.遍历
		for(int i = max;i <= m * n;i++){
			if(i % m == 0 && i % n == 0){
				System.out.println("最小公倍数:" + i);
				break;	
			}
		}	
	}
}

运行结果:

请输入第一个数:
12
请输入第二个数:
20
最大公约数为:4
最小公倍数为:60

五、循环语句—while循环


while(②){
③;
④;
}

执行过程:① - ② - ③ - ④ - ② - ③ - ④ - … - ②

说明:
1.写while循环千万小心不要丢了迭代条件。一旦丢了,就可能导致死循环!
2.for循环和while循环是可以相互转换的!
区别:for循环和while循环的初始化条件部分的作用范围不同。
示例:

class  WhileTest{
  public static void main(String[] args) {
  	
  	//遍历100以内的所有偶数
  	int i = 1;
  	while(i <= 100){
  		if(i % 2 == 0){
  			System.out.println(i);
  		}			
  		i++;
  	}
  	//出了while循环以后,仍可以调用。
  	System.out.println(i);//101
  }
}

六、魂环语句—do while循环

do-while循环结构:


do{
③;
④;

}while(②);
执行过程:① - ③ - ④ - ② - ③ - ④ - … - ②

说明:
1.do-while循环至少会执行一次循环体!
2.开发中,使用for和while更多一些。较少使用do-while
示例:

class DoWhileTest {
	public static void main(String[] args) {
		
		//遍历100以内的偶数,并计算所有偶数的和及偶数的个数
		int num = 1;
		int sum = 0;//记录总和
		int count = 0;//记录个数
		do{
			
			if(num % 2 == 0){
				System.out.println(num);
				sum += num;
				count++;
			}

			num++;

		}while(num <= 100);
		

		System.out.println("总和为:" + sum);
		System.out.println("个数为:" + count);

		/
		for(int j = 1;j <= 4;j++ ){
			for(int i = 1;i <= 6;i++){
				System.out.print('*');
			}
			System.out.println();
		}

		for(int i = 1;i <= 5;i++){//控制行数
			for(int j = 1;j <= i;j++){//控制列数
				System.out.print("*");
			
			}
			System.out.println();
		}
	

		for(int i = 1;i <= 4;i++){
			for(int j = 1;j <= 5 - i;j++){
				System.out.print("*");	
			}
			System.out.println();
		}

案例二:九九乘法表

class NineNineTable {
	public static void main(String[] args) {
		
		for(int i = 1;i <= 9;i++){
			
			for(int j = 1;j <= i;j++){
				System.out.print(i + " * " + j + " = " + (i * j) + "  ");
			}

			System.out.println();
		}


	}
}

八、特殊关键字:break,continue

相同点和不同点:

项目使用范围循环中使用的作用(不同点)相同点
breakswitch-case;循环结构中结束当“前”循环关键字后面不能声明执行语句
continue循环结构中结束当“次”循环关键字后面不能声明执行语句

标签的使用:

//******************************
		
		label:for(int i = 1;i <= 4;i++){
		
			for(int j = 1;j <= 10;j++){
				
				if(j % 4 == 0){
					//break;//默认跳出包裹此关键字最近的一层循环。
					//continue;

					//break label;//结束指定标识的一层循环结构
					continue label;//结束指定标识的一层循环结构当次循环
				}
				
				System.out.print(j);
			}
			
			System.out.println();
总结

一些零碎知识:
1、

//获取当前时间距离1970-01-01 00:00:00 的毫秒数
		long start = System.currentTimeMillis();
		//获取当前时间距离1970-01-01 00:00:00 的毫秒数
		long end = System.currentTimeMillis();
		System.out.println("所花费的时间为:" + (end - start));

2、Math.sqrt(i)求i的平方差,eg:Math.sqrt(9)==3
3、break和continue的标签的使用,跳出指定的循环。
4、return是直接跳出方法的,方法都跳出了,循环肯定直接结束了。
5、变量.equals(“需要对比的字符串”),得出一个Boolean型。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/716233.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号