Java第五章

程序流程控制(三大流程控制语句)

  1. 顺序控制(程序从上到下执行,中间没有任何判断和跳转,java中定义bian时采用合法的前向引用
  2. 分支控制(让程序有选择的执行)
    //if的快速入门,单分支
    import java.util.Scanner;
    public class if01{
    
    	//编写一个main方法
    	public static void main(String[] args){
    
    Scanner myScanner = new Scanner(System.in);
    System.out.println("请输入年龄");
    int age = myScanner.nextInt();
    if( age > 18){
    	System.out.println("你年龄大于18,要对自己的行为负责,否则送进监狱");
    }
    
    System.out.println("程序继续...");		
    
    	}
    }
    //if的快速入门,双分支
    import java.util.Scanner;
    public class if01{
    
    	//编写一个main方法
    	public static void main(String[] args){
    
    Scanner myScanner = new Scanner(System.in);
    System.out.println("请输入年龄");
    int age = myScanner.nextInt();
    if( age > 18){
    	System.out.println("你年龄大于18,要对自己的行为负责,否则送进监狱");
    }else{
    	System.out.println("你的年龄不大这次放过你了");	
    }
    
    System.out.println("程序继续...");		
    
    	}
    }
    public class if02
    {
    
    	//编写一个main方法
    	public static void main(String[] args)
    	{
    
    int num1 = 10;
    
    int num2 = 5;
    
    int sum = num1 + num2;
    
    if( sum % 3 == 0 && sum % 5 == 0)
    {
    	System.out.println("和可以被3和5整除");
    }else{
    	System.out.println("和不能被3和5整除");
    }
    
    	}
    }
    import java.util.Scanner;//引入一个扫描器类
    public class if02
    {
    
    	//编写一个main方法
    	public static void main(String[] args)
    	{
    
    Scanner myScanner = new Scanner(System.in);
    System.out.println("请输入芝麻信用分(1-100)");//提示用户输入
    int score = myScanner.nextInt();//接收用户输入
    
    if(score >=1 && score <= 100){
    	//因为有4种情况使用多分支
    if( score==100){
    	System.out.println("信用极好");
    }else if( score > 80 && score <= 99){
    	System.out.println("信用优秀");
    }else if( score >= 60 && score <= 80){
    	System.out.println("信用一般");
    }else{
    	System.out.println("信用不及格");
    }
    }else{
    	System.out.println("信用分需要在1-100,请重新输入:)");
    
    }
    
        }
    }

    嵌套分支:在一个分支结构中又完整的嵌套了另一个完整的分支结构,里面的分支的结构称为内层分支外面的分支结构称为外层分支。不过最好不要超过3层(可读性不好)

    import java.util.Scanner;//引入一个扫描器类
    public class if02
    {
    
    	//编写一个main方法
    	public static void main(String[] args)
    	{
    
    Scanner myScanner = new Scanner(System.in);//创建一个Scanner对象,接收用户输入
    
    System.out.println("请输入该歌手成绩");//提示用户输入
    double score = myScanner.nextDouble();//接收用户输入
    
    if(score >8.0){
    	System.out.println("恭喜你!!进入决赛:)");
    
    	System.out.println("请输入性别");
     	char gender = myScanner.next().charAt(0);//键盘接收字符串的第一个字符
    
    	if(gender=='女'){
    	System.out.println("进入女子组");
    	}
    	else if(gender=='男'){
    	System.out.println("进入男子组");
    	}
    	else{
    	System.out.println("你的性别有误,不能参加决赛~");
    	}
    }else{
    	System.out.println("你被淘汰了");
    
    }
    
    
        }
    }
    import java.util.Scanner;//引入一个扫描器类
    public class if02
    {
    
    	//编写一个main方法
    	public static void main(String[] args)
    	{
    
    Scanner myScanner = new Scanner(System.in);//创建一个Scanner对象,接收用户输入
    
    System.out.println("您想几月份去游玩^_^?");//提示用户输入
    double month = myScanner.nextDouble();//接收用户输入
    
    if(month >=4 && month <= 10){
    
    	System.out.println("请输入年龄");
        int age = myScanner.nextInt();//接收用户输入
    
    	if(age >= 18 && age <= 60){
    	System.out.println("成年人60元");
    	}
    	else if(age < 18){
    	System.out.println("儿童半价");
    	}
    	else{
    	System.out.println("1/3的票价");
    	}
    }else{
    
    	System.out.println("请输入年龄");
        int age = myScanner.nextInt();//接收用户输入
        
    	if(age >= 18){
    			System.out.println("成年人40元");
    	}else{
    			System.out.println("20元");
    
    	}
    
    }
    
    
        }
    }

    switch分支结构的细节:1.表达式数据类型,应和case后的常量类型一致,或者是可以自动转成可以相互比较的类型,比如输入的是字符,而常量是int。  2.switch(表达式)中表达式的返回值必须是:(byte,short,int,char,enum【枚举】,string。即只能为这些类型,不能是double等) 3.case子句中的值必须是常量,而不能是变量   4.default子句是可选的,当没有匹配的case时,执行default   5.break语句用来在执行完一个case分支后使程序跳出switch语句块;如果没有写break,程序会顺序执行到switch结尾,除非遇到break

    import java.util.Scanner;
    	public class switch01{
    	//编写一个main方法
    	public static void main(String[] args){
    		Scanner myScanner = new Scanner(System.in);
    		System.out.println("请输入一个字符(a-e)");
    		char c1 = myScanner.next().charAt(0);
    		//在java中,只要是有值返回,就是一个表达式
    		switch(c1){
    			case 'a' :
    			System.out.println("A");
    			break;
    			case 'b' :
    			System.out.println("B");
    			break;
    			case 'c' :
    			System.out.println("C");
    			break;
    			case 'd' :
    			System.out.println("D");
    			break;
    			case 'e' :
    			System.out.println("E");
    			break;
    			default:
    			System.out.println("你输入的字符不正确,没有匹配度");
    
    
    		}
    		System.out.println("退出了swich,继续执行程序");
    
    	}
    }
    import java.util.Scanner;
    	public class switch01{
    	//编写一个main方法
    	public static void main(String[] args){
    		Scanner myScanner = new Scanner(System.in);
    		System.out.println("请输入一个分数");
    		int score = myScanner.nextInt();
    		//在java中,只要是有值返回,就是一个表达式
    	if(score >= 0 && score <= 100){
    		switch((int)score/60){
    			case 0 :
    			System.out.println("不合格");
    			break;
    			case 1 :
    			System.out.println("合格");
    			break;
    
               }
    	}else{
    			System.out.println("请重新输入");
    	
    	}
    	}
    }
    import java.util.Scanner;
    	public class switch01{
    	//编写一个main方法
    	public static void main(String[] args){
    		Scanner myScanner = new Scanner(System.in);
    		System.out.println("请输入一个月份");
    		int month = myScanner.nextInt();
    		//在java中,只要是有值返回,就是一个表达式
    	if(month > 0){
    		switch(month){
    			case 3 :
    			case 4 :
    			case 5 :
    			System.out.println("春季");
    			break;
    			case 6 :
    			case 7 :
    			case 8 :
    			System.out.println("夏季");
    			break;
    			case 9 :
    			case 10 :
    			case 11 :
    			System.out.println("春季");
    			break;
    			case 12 :
    			case 1 :
    			case 2 :
    			System.out.println("春季");
    			break;
                }
    	}else{
    			System.out.println("请重新输入");
    	
    	}
    	}
    }

    switch和if的比较:1.如果判断的具体数值不多,而且符合byte、short、int、char,enum【枚举】,string这6种类型,最好使用switch。2.对区间判断,对结果为boolean类型判断,使用if,if的使用范围更广

  3. 循环控制
    public class forexercise{
    	public static void main(String[] args){
    
    		int n = 5;
    
    		for(int i = 0 ; i <= n ; i++ ){
    			System.out.println(i + "+" + (n-i) + "=n" );
    		}
    	}
    }
    public class while01{
    	public static void main(String[] args){
    
    		int i = 1;
    		int endnum = 100;
    		while(i <= 100){
    			if(i%3 == 0){
    			System.out.println(i);
    
    			}
    			i++;//变量自增
    		}
    		System.out.println("退出while,继续");
    
    	}
    }
    public class while01{
    	public static void main(String[] args){
    
    		int i = 40;
    		while(i <= 200){
    			if(i%2 == 0){
    			System.out.println(i);
    
    			}
    			i++;//循环变量的迭代
    		}
    		System.out.println("退出while,继续");
    
    	}
    }

    do while循环控制

    import java.util.Scanner;
    public class dowhile{
    	public static void main(String[] args){
    
    		Scanner myScanner = new Scanner(System.in);
    		char answer = ' ';
    		do{
    		System.out.println("老韩使出五连鞭");
    		System.out.println("老韩问:还钱吗?y/n");
    		answer = myScanner.next().charAt(0);
    		System.out.println("他的回答是" + answer);
    		}while(answer != 'y');//判断条件
    		System.out.println("李三还钱了!!");
    	}
    }

    多重循环控制(难点,重点

    public class mulfor{
    	public static void main(String[] args){
    		for(int i = 0; i < 2 ; i++){
    			for( int j = 0; j < 3; j++){
    				System.out.println("i=" + i + "j=" + j);
    			}
    		}
    	}
    }
    import java.util.Scanner;
    public class mulfor{
    	public static void main(String[] args){
    		Scanner myScanner = new Scanner(System.in);//创建Scanner对象
    		double totalScore = 0;//累计所有学生的成绩
    		int passNum = 0;//累积及格的人数
    		int classNum = 3;//班级个数
    		int stuNum = 5;//学生人数
    		for(int i = 1; i <= classNum ; i++){//i表示班级
    			double sum = 0;//一个班级的总分
    			for( int j = 1; j <= stuNum; j++){//j表示学生
    				System.out.println("请输第" + i +"个班的第" + j + "个学生的成绩");
    				double score = myScanner.nextDouble();
    				if(score >= 60){//当一个学生成绩>=60,passNum++
    					passNum++;
    				}
    				sum += score;//累计学生的成绩
    				System.out.println("成绩为" + score);
    			}
    			System.out.println("sum=" + sum + "平均分=" + (sum / stuNum));
    			totalScore += sum;//所有班级学生成绩的总分
    		}
    		System.out.println("3个班总分=" + totalScore + "平均分=" + (totalScore / stuNum));
    		System.out.println("及格人数有" + passNum + "人");
    
    	}
    }
    //99乘法表
    public class mulfor{
    	public static void main(String[] args){
    		int i;
    		int row = 9;
    		int j;
    		int mul;
    		for( i = 1 ; i <= row ; i++){
    			for( j = 1 ; j <=i ; j++){
    				mul = i * j;
    				System.out.print(j + "*" + i + "=" + mul + '\t');
    			}
    			System.out.print('\n');
    
    		}
    	}
    }
    //打印一个空心金字塔
    public class mulfor{
    	public static void main(String[] args){
    		int i;
    		int row = 10;
    		int j;
    		int k;
    		for( i = 1 ; i <= row ; i++){
    			for( k = 1 ; k <= row - i  ; k++){
    				System.out.print( " " );
    			}
    			for( j = 1 ; j <= 2 * i - 1 ; j++){
    				if(j == 1 || j == 2 * i - 1 || i == row){
    				System.out.print( "*" );
    			}else{
    				System.out.print( " " );
    
    			}
    			}
    
    			System.out.print('\n');
    
    		}
    	}
    }
    //打出一个空心菱形
    import java.util.Scanner;
    public class mulfor{
    	public static void main(String[] args){
    
    		Scanner myScanner = new Scanner(System.in);//创建Scanner对象
    
    		int i;
    		int j;
    		int k;
    		int row;
    		System.out.println("请输入row(row>2)");
           	int row = myScanner.nextInt();
    
    		for( i = 1 ; i <= row ; i++){
    			for( k = 1 ; k <= row - i  ; k++){
    				System.out.print( " " );
    			}
    			for( j = 1 ; j <= 2 * i - 1 ; j++){
    				if(j == 1 || j == 2 * i - 1 ){
    				System.out.print( "*" );
    			}else{
    				System.out.print( " " );
    
    			}
    			}
    
    			System.out.print('\n');
    
    		}
    
    			for( i = 1 ; i <= row - 1 ; i++){
    			for( k = 1 ; k <= i ; k++){
    				System.out.print( " " );
    			}
    			for( j = 1 ; j <= (2 * row - 1 ) - 2 * i ; j++){
    				if(j == 1 || j == (2 * row - 1 ) - 2 * i ){
    				System.out.print( "*" );
    			}else{
    				System.out.print( " " );
    			}
    			}
    
    			System.out.print('\n');
    
    		}
    	}
    }

    跳转控制语句-break注意事项和细节说明:1.break语句出现在多层嵌套的语句中时,可以通过标签指明要终止的是哪一层语句块BreakDetail.java  2.标签的基本使用

     a.break语句可以指定退出哪一层  b.label1是标签,由程序员指定,也可叫别的名称,比如:abc  c.break后指定到哪个label就退出到哪里  d.在实际开发中尽量不使用标签(可读性会变差)  e.如果没有指定break,默认退出最近的循环体

    public class break01{
    	public static void main(String[] args){
    		int sum = 0;//累积和
    		int n = 0;//记录i,起到了扩大i作用域的功能
    		for( int i = 1; i < 100;i++){
    			sum += i;//累积
    			if( sum > 20){
    				n = i;//将i的值赋给n
    				break;
    			}
    		}
    		System.out.println("和>20的时候,当前数i=" + n);
    
    	}
    }
    import java.util.Scanner;//引入包
    public class break02{
    	public static void main(String[] args){//编写一个main方法
    		//实现登录验证,有3次机会,如果用户名为"丁真",密码"666"提示登陆成功
    		//否则提示还有几次机会,请使用for+break完成
    		//
    		//思路分析
    		//1.创建Scanner对象接收用户输入
    		//2.定义String name;String passwd;保存用户名和密码
    		//3.循环3次【登录3次】,如果满足条件就提前退出
    		//4.定义一般变量int chance记录还有几次登录机会
    		
    		Scanner myScanner = new Scanner(System.in);//1.创建Scanner对象接收用户输入
    		String name = "";
    		String passwd = "";
    		int chance = 3;//登陆一次就少一次
    		for(int i = 1;i <= 3; i++){
    			System.out.println("请输入名字");
    			name = myScanner.next();
    			System.out.println("请输入密码");
    			passwd = myScanner.next();
    			//比较输入的名字和密码是否正确
    			//补充说明字符串的内容比较使用的方法时equals
    			if("丁真".equals(name) && "666".equals(passwd)){
    				System.out.println("恭喜你!登陆成功!");
    				break;
    			}
    			chance--;
    			System.out.println("你还有" + chance + "登录机会");
    
    		}
    	}
    }

    return的使用说明:当return用在方法时,表示跳出方法,如果使用在main,表示退出main程序,break则是跳出当前循环,continue是继续执行下一次循环

本文记录java学习过程中的笔记,如发现有错误,欢迎批评指正。大家有好的学习方法也可以发出来!!!

学习地址:【零基础 快速学Java】韩顺平 零基础30天学会Java_哔哩哔哩_bilibili

给大家推荐一款好用的截图软件:【爱折腾Michael】Snipaste 史上最强大截图软件截图神器 最好用截图却也是最不正紧的截图软件 使用技巧讲解_哔哩哔哩_bilibili

免费在线画图,无需注册:未命名绘图.iodraw - iodraw.com未命名绘图.iodraw - iodraw.com未命名绘图.iodraw - iodraw.com

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值