JavaSE (四)程序流程控制 -- if 、switch、for、while

个人博客网:https://wushaopei.github.io/    (你想要这里多有)

目录

前置:

 * . 从键盘读取数据:

1、分支结构

1.1 if-else结构

1.2 switch-case结构

2、循环结构

 2.1 循环: for循环  

案例实操:水仙花数

2.2 循环  while


前置:

 * . 从键盘读取数据:

    1.导包 import java.util.Scanner;
    2.创建对象 Scanner s = new Scanner(System.in);
    3.调用方法 int age = s.nextInt();

//1.导包 import java.util.Scanner;
import java.util.Scanner;
public class ScannerTest{

    public static void main(String[] args){
    
        //2.创建对象 Scanner s = new Scanner(System.in);
        Scanner s = new Scanner(System.in);

        System.out.println("请输入您的名字");
        String name = s.next();

        System.out.println("请输入您的年纪");
        //3.调用方法 int age = s.nextInt(); (对象名.方法名)
        int age = s.nextInt();

        System.out.println("请输入您的成绩");
        double score = s.nextDouble();

        System.out.println("请输入您长的帅吗?(true/false)");
        boolean handsome = s.nextBoolean();

        System.out.println("名字=" + name);
        System.out.println("年纪=" + age);
        System.out.println("成绩=" + score);
        System.out.println("帅不帅=" + handsome);
    }
}

1、分支结构

1.1 if-else结构

1:

If(条件表达式){

执行语句;

}

如果条件表达式的结果为true则执行执行语句

2:二选一

If(条件表达式){

执行语句1:

}else{

执行语句2;

}

如果表达式的结果为true则执行 执行语句1,否则执行 执行语句2.

 

3:多选一

If(条件表达式1){

执行语句1;

}else if(条件表达式2){

执行语句2;

} else if(条件表达式3){

执行语句3;

}

......

else{

执行语句n;

}

哪个条件表达式的结果为true就执行相应的执行语句.

明:

  1. 条件表达式的结果是布尔类型。
  2. else可以省略
  3. 如果执行语句只能一句的话,大括号可以省略但不建议
  4. 多个条件表达式如果为互斥关系,谁上谁下都可以。如果为包含关系,范围小的在上面范围大的在下面。
public class IfTest{

	public static void main(String[] args){
	
		//第一种形式
		boolean handsome = false;

		if(handsome){
			System.out.println("伦家愿意的了");
		}
		
		//第二种形式

		int money = 50;
		if(money < 50){
			System.out.println("人家不是那种人了");
		}else{
			System.out.println("千万别把我当人");
		}
		
		//第三种形式
		int age = 20;

		if(age < 18){
			System.out.println("少儿不宜");
		}else if(age >= 18 && age < 30){
			System.out.println("别看片了还是和媳妇逛街去吧");
		}else if(age >= 30 && age < 50){
			System.out.println("看片多了吧,还是应该多运动");
		}else{
			System.out.println("年纪太大了,还是不玩了");
		}
		System.out.println("---------------------------------------------------");

		int score = 160;

		if(score == 100){
			System.out.println(100);
		}else if(score > 90 && score < 100){
			System.out.println("大于90");
		}

		System.out.println("代码执行完毕");

	}
}

案例实操(1):

岳小鹏参加Java考试,他和父亲岳不群达成承诺:
如果:
成绩为100分时,奖励一辆BMW;
成绩为(80,99]时,奖励一台iphoneX;
当成绩为[60,80]时,奖励一个 iPad;
其它时,什么奖励也没有。
请从键盘输入岳小鹏的期末成绩,并加以判断


说明:
    1.当有多个条件表达式时,如果多个条件表达式是互斥关系,谁上谁下都可以。
        如果多个条件表达式是包含关系,范围小的在上面。范围大的在下面。

import java.util.Scanner;
public class IfTest3{

	public static void main(String[] args){
		
		//第一步 从键盘读取成绩
		Scanner s = new Scanner(System.in); //创建对象
		System.out.println("请输入小鹏鹏的成绩");
		int score = s.nextInt(); //从键盘读取成绩

		//第二步 使用 if-else if进行判断
		/*
		if(score == 100){
			System.out.println("不错不错给你一辆BMW");
		}else if(score > 80 && score <= 99){
			System.out.println("还可以给你iphoneX");
		}else if(score >= 60 && score <= 80){
			System.out.println("呐 给你一个ipad");
		}else{
			System.out.println("哼 还想要礼物 ,啥都没有");
		}
		*/

		//带校验功能
		/*
		if(score >= 0 && score <= 100){
		
			if(score == 100){
				System.out.println("不错不错给你一辆BMW");
			}else if(score > 80){
				System.out.println("还可以给你iphoneX");
			}else if(score >= 60){
				System.out.println("呐 给你一个ipad");
			}else{
				System.out.println("哼 还想要礼物 ,啥都没有");
			}
		}
		*/
			
		if(score == 100){
			System.out.println("不错不错给你一辆BMW");
		}else if(score >= 60 && score <= 80){
			System.out.println("呐 给你一个ipad");
		}else if(score > 80 && score <= 99){
			System.out.println("还可以给你iphoneX");
		}else{
			System.out.println("哼 还想要礼物 ,啥都没有");
		}		

		System.out.println("代码执行完毕");
	
	}
}

案例实操(2):

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

import java.util.Scanner;
public class IfTest4{

	public static void main(String[] args){
	
		//1.从键盘读取数据
		Scanner s = new Scanner(System.in);

		System.out.println("请输入一个数");
		int num1 = s.nextInt();
		System.out.println("请输入一个数");
		int num2 = s.nextInt();
		System.out.println("请输入一个数");
		int num3 = s.nextInt();

		//比较前两个数的大小。再用第三个数和前两个数进行比较
		if(num1 > num2){
			//如果num3大于num1说明 num3 > num1 > num2
			if(num3 > num1){
				System.out.println(num2 + " " + num1 + " " + num3);
			}else if(num3 < num2){
				//如果num3小于num2 说明  num1 > num2 > num3
				System.out.println(num3 + " " + num2 + " " + num1);
			}else{
				//其它 num1 > num3 > num2
				System.out.println(num2 + " " + num3 + " " + num1);
			}
		}else{ //num1 <= num2
			if(num3 > num2){
				System.out.println(num1 + " " + num2 + " " + num3);
			}else if(num3 < num1){
				System.out.println(num3 + " " + num1 + " " + num2);
			}else{
				System.out.println(num1 + " " + num3 + " " + num2);
			}
			
		}
	}
}

案例实操(3)

/*

	(int)(Math.random() * 90  + 10) : 获取随机数
*/
public class IfTest6{

	public static void main(String[] args){
	
		//Math.random() 返回一个随机数,随机数的范围是 [0.0,1.0)
		int number = (int)(Math.random() * 90  + 10);

		System.out.println(number);
	}
}

1.2 switch-case结构

式:

switch(表达式){
	
	case 常量1:
		执行语句1;
		break;
	case 常量2:
		执行语句2;
		break;
	case 常量3:
		执行语句3;
		break;

	default:
		执行语句n;
		break;
	
	}

明:

1.当执行switch-case时,会先根据switch中的变量的值,依次和case后面的常量进行匹配。一旦匹配成功,则执行相应的执行语句。如果该case中没有break则继续向下执行其它执行语句。直到遇到break或者到达该结构中的结尾则跳出switch-case结构。

2.break :可选。如果有break当执行语句遇到时则跳出switch-case结构.

3.default : 可选(位置是灵活的)。如果没有匹配成功的。则执行default中的执行语句。

4.表达式的类型只能是 byte short int char 枚举 String

5.case后面只能是常量

案例实操(1):

import java.util.Scanner;
public class SwitchTest{

	public static void main(String[] args){
		
		int a = 100;

		switch(a){
		default:
			System.out.println("other");
			//break;

		case 0:
			System.out.println("0");
			//break;
		case 1:
			System.out.println("1");
			//break;
		case 2:
			System.out.println("2");
			//break;
		case 3:
			System.out.println("3");
			//break;
		
		}
		
		String str = "a";
		switch(str){
		case "a":
			System.out.println("a");
			break;
		case "b":
			System.out.println("b");
			break;
		
		}
	}
}

案例实操(2):

对学生成绩大于60分的,输出“合格”。低于60分的,输出“不合格”。

import java.util.Scanner;
public class SwitchTest3{

	public static void main(String[] args){
	
		Scanner s = new Scanner(System.in);

		System.out.println("请输入学生成绩");

		int score = s.nextInt();
		
		/*
		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):

编写程序:从键盘上输入2017年的“month”和“day”,要求通过程序输出输入的日期为2017年的第几天。

    从键盘分别输入年、月、日,判断这一天是当年的第几天
 
    注:判断一年是否是闰年的标准:
       1)可以被4整除,但不可被100整除
       2)可以被400整除

import java.util.Scanner;
public class SwitchTest4{

	public static void main(String[] args){
	
		Scanner s = new Scanner(System.in);

		System.out.println("请输入年份");
		int year = s.nextInt();

		System.out.println("请输入月份");
		int month = s.nextInt();

		System.out.println("请输入日期");
		int day = s.nextInt();

		int sumDay = 0; //记录总天数

		switch(month){
		
		case 3:
			//sumDay += 28; //sumDay = sumDay + 28;

			if((year % 400 == 0 )|| (year % 4 == 0 && year % 100 != 0)){
				sumDay += 29;
			}else{
				sumDay += 28;
			}
		case 2:
			sumDay +=  31;//sumDay = sumDay + 31;
		case 1:
			sumDay += day;//sumDay = sumDay + day;
					
		}
	
		System.out.println(year + "-" + month + "-" + day + ",是当年的第" + sumDay + "天");
	}
}

2、循环结构

循环的四个部分:

        1.初始化条件
        2.循环条件
        3.循环体
        4.迭代条件

 2.1 循环: for循环  

格式 :      

   for(初始化条件; 循环条件;迭代条件){
                循环体
          }

          for(1; 2;4){
                3
          }

          循环顺序 : 1- 2 - 3 - 4 - 2 - 3 - 4 ...... 2

public class ForTest{

	public static void main(String[] args){
	
		//需求: 打印五遍我爱苍老师
		//注意 : 循环条件结果为false时跳出循环体,结果为true时执行循环体
		/*
		int i = 0;
		for(; i < 5; i++){
			System.out.println("我爱苍老师");
		}

		System.out.println(i);

		System.out.println("-------------------------------------");
		*/

		/*
			练习 :求一百以内的偶数,输出偶数的个数,求偶数的总和。

			练习:求一百以内的奇数,输出奇数的个,求奇数的总和(作业)
		*/
		int count = 0; //用于统计偶数的个数
		int sum = 0; //用于统计偶数的总和
		for(int i = 1; i <= 100; i++){
			
			if(i % 2 == 0){
				count++;
				sum += i;
				System.out.println(i);
			}
		}

		System.out.println("count=" + count + " sum=" + sum);	
	}
}

案例实操:水仙花数

输出所有的水仙花数,所谓水仙花数是指一个3   位数,其各个位上数字立方和等于其本身。
         例如: 153 = 1*1*1 + 3*3*3 + 5*5*5

public class ShuiXianHua{

	public static void main(String[] args){
	
		for(int i = 100; i < 1000; i++){
		
			//将各个位上的数进行拆分
			int a = i / 100;
			int b = i % 100 / 10;
			int c = i % 10;

			if(i == (a * a * a + b * b * b + c * c * c)){
				System.out.println(i);
			}
		}
	}
}

2.2 循环  while

格式:

         初始化条件
         while(循环条件){
        
            循环体;
            迭代条件;
        
         }

   说明: 1.while和for循环可以相互转换

   思考: while循环和for循环的区别是什么?

            while初始化条件在结构外。for循环的初始化条件在结构内(也可以写在结构外)。

案例实操:

public class WhileTest{

	public static void main(String[] args){
	
		/*
			练习 :求一百以内的偶数,输出偶数的个数,求偶数的总和。

			练习:求一百以内的奇数,输出奇数的个,求奇数的总和(作业)
		*/

		//1.初始化条件
		int i = 1;
		int count = 0; //偶数的个数
		int sum = 0; //偶数的总和	
		while(i <= 100){//2.循环条件 当循环条件的值为false时跳出当前while结构继续向下执行
			//3.循环体
			if(i % 2 == 0){
				count++;
				sum += i;
				System.out.println(i);
			}
			//4.迭代条件
			i++;
		}

		System.out.println("count=" + count + " sum=" + sum);


	}
}
posted @ 2019-10-21 10:30  维宇空灵  阅读(284)  评论(0编辑  收藏  举报