Java程序流程控制

计算机的语言分三个层次

  1. 机器语言 – 计算机能直接识别的语言,一堆的0和1组成。
  2. 汇编语言 – 比机器语言强一些,有一些简单的指令组成。
  3. 高级语言 – C语言,Java,Python等

流程控制语句是所有高级语言都有的,只不过不同的语言语法上略有区别。

流程控制语句 分为:**分支语句(条件语句) ** 和 循环语句

分支语句包括: if-else语句 和 switch-case 语句

循环语句包括: for循环 while循环 do-while循环。

程序的执行结构有三种:

顺序结构 – 代码从上到下,逐行执行。绝大多数的代码都是顺序结构。

分支结构 – 代码按照一定的条件,选择一个分支执行或者一个都不选(有选择的执行,不是都执行)。

循环结构 – 代码按照一定的条件,多次执行同一段代码,也可以不执行(反复的执行)。

分支语句(分支结构)

if -else结构

语法:

if(条件1){ //只能出现1次,必须出现 如果
//满足条件1时执行的代码,可以是一大段
}
else if(条件2){ //出现0-N次 或者
//满足条件2时执行的代码,可以是一大段
}
else{ //出现0-1次 否则
//以上所有条件都不满足时执行的代码,可以是一大段
}

案例:

​ 判断一个整数是正数、零还是负数
int a = 1;

if(a>0){
System.out.println(“正数”);
}
else if(a==0){
System.out.println(“零”);
}
else{
System.out.println(“负数”);
}

如果某个分支里面只有一个语句,可以省略不写{}。

所有的分支语句和循环语句,一定要谨慎考虑 临界点问题。分清 临界点的 归属。

分支语句的分支中,隐含了一个条件:就是当进入第N个分支时,前面N-1个分支的条件都 不成立。

练习:

1、输入5个整数,打印最大值。

public class Max {
	public static void main(String[] args) {
		Scanner sc=new Scanner(System.in);
		System.out.println("请输入五个整数");
		int num1=sc.nextInt();
		int num2=sc.nextInt();
		int num3=sc.nextInt();
		int num4=sc.nextInt();
		int num5=sc.nextInt();
		int max=num1;
		if(max<num2){
			max=num2;
		}
		if(max<num3){
			max=num3;
		}
		if(max<num4){
			max=num4;
		}
		if(max<num5){
			max=num5;
		}
		System.out.println("最大值为"+max);
	}
}

2、输入工资数,输出旅游时选择的交通工具。
工资不超过5000的,选择自行车
超过5000,不超过8000的,选择汽车
超过8000,不超过15000的,选择火车
超过15000的,选飞机

public class Money {
	public static void main(String[] args) {
		Scanner sc=new Scanner(System.in);
		System.out.println("请输入工资数");
		int num=sc.nextInt();
		if(num<0){
			System.out.println("非法的月份,无法判断天数");
			return;
		}else if(num<=5000){
			System.out.println("自行车");
		}else if(num<=8000){
			System.out.println("汽车");
		}else if(num<=15000){
			System.out.println("火车");
		}else{
			System.out.println("飞机");
		}
	}
}

3、输入年月,输入当月有多少天。
1 3 5 7 8 10 12 月 有 31天
4 6 9 11 月 有30天
2月 平年是 28天, 闰年是 29天 。
如何判断闰年:
1、能被4整除并且不能被100整除的是闰年
2、或者能被 400 整除的是闰年

public class Day {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		System.out.println("请输入年月");
		int year = sc.nextInt();
		int month = sc.nextInt();
		if (month < 1 || month > 12) {
			System.out.println("非法的月份,无法判断天数");
			return;
		}
		if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) {
			System.out.println("当月31天");
		} else if (month == 2) {
			if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
				System.out.println("当月29天");
			} else {
				System.out.println("当月28天");
			}
		} else {
			System.out.println("当月30天");
		}
		sc.close();
	}
}

4、计算出租车的车费。
出租车的车费包括两个部分: 里程和等候时间。
等候时间 每2分半1块,不足2分半不收钱 ,比如: 4分59秒 收 1 块。
里程前3公里 10元 , 4-15公里 每公里 2元, 16公里 以上的每公里3元。
等候时间 + 里程 = 车费。
要求:输入公里数和 等候的秒数,计算车费。

 比如:16公里,299秒 
  车费 = 10 + (15-3) * 2 + (16-15) * 3   +  1 = 38 
public class Taxi {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		System.out.println("请输入公里数和等候秒数");
		int km=sc.nextInt();
		int time=sc.nextInt();
		int pay;
		int kmPay;
		int timePay;
		if(km<0||time<0){
			System.out.println("非法输入");
			return;
		}
		if(km<=3){
			kmPay=10;
		}else if(km<=15){
			kmPay=10+(km-3) * 2;
		}else{
			kmPay=10+(15-3)*2+(km-15)*3;
		}	
		timePay=time/150;	
		pay=kmPay+timePay;
		System.out.println(pay);
	}
}

switch-case结构

语法:

switch-case语法

案例1:

switch-case案例1

案例2:
public class Demo {
	public static void main(String[] args) {
		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;
		}
	}
}
switch语句有关规则

switch语句有关规则

循环语句(循环结构)

for循环

语法
 for( 循环变量的声明和初始化 ; 循环的条件 ; 改变循环变量的值的语句  ) {
      //反复执行的代码,可以是一大段
 }
案例:打印出1 到 10 的代码
 for(int i=1 ; i<11 ; i++){
      System.out.println(i);
 }
      1        2     3 
 for(int i=1 ; i<11 ; i++){
      System.out.println(i); // 4
 }

循环的执行次序:
1  2  4  3   2  4   3   .......   2 

while循环

语法

while循环语法

案例

while案例

do-while循环

语法

do-while循环语法

案例
public class DoWhileLoop {
	public static void main(String args[]) {
		int result=0,i=1;
		do { 
			result += i;
			i++;
		} while (i <= 100);
		System. out. println("result=" + result) ;
	}
}

循环次数不确定的循环格式:

在无限循环(死循环)中,按照一定的条件退出。 退出循环用 break 关键字

for( ; ; ){
…;
if(退出条件) break;
…;
}

while(true){
…;
if(退出条件) break;
…;
}

案例:登录

当输入用户名和密码为zhangfei 和 1234 时,登录成功,退出循环;否则登录失败,循环输入。

public class TestBreak{
    public static void main(String[] args) {
    	Scanner sc = new Scanner(System.in);
    	for(;;){
		    System.out.println("请输入用户名和密码");
		    String name = sc.next();
		    String pass = sc.next();
		    if(name.equals("zhangfei") && pass.equals("1234")) break; //String比较值是否相同,只能用equals()
		    System.out.println("用户名/密码错误");
    	}
		System.out.println("登录成功");
	}
}

嵌套循环

双重循环:

循环里面嵌套另外一个循环。一般来说,循环不超过3重,所以只讲双重循环。

for(…){
…;
for(…){
…;
}
…;
}

双重循环的案例:

打印如下图形:

*
**
***
****
*****

如果双重循环中,第二层的循环次数是等差的,就可以使用如下的公式

两个循环变量使用 i 和 j 。

内层循环的条件公式:

j< m*i + n

m是循环的次数差,如果递减的取负数;
n是第一次循环的次数。

使用条件:
i和j的初始值相同,最好都从0开始。

步进语句使用 i++ 和 j++ .

public class Star {
	public static void main(String[] args) {
		for(int i=0;i<5;i++){
			for(int j=0;j<i+1;j++){
				System.out.print("*");
			}
			System.out.println();
		}
	}
}
案例:九九乘法表
public class Multiplication {
	public static void main(String[] args) {
		for(int i=1;i<=9;i++){
			for(int j=1;j<i+1;j++){
				System.out.print(j+"*"+i+"="+i*j);
				System.out.print(" ");
			}
			System.out.println();
		}	
	}
}

综合练习

  1. 编程题:
    打印出所有的三位水仙花数。 个位数的三次方 + 十位数的三次方 + 百位数的三次方 等于这个数本身。 比如: 370 = 333 + 777 + 0
public class Flower {
	public static void main(String[] args) {	
		for(int i=100;i<=999;i++){	
			int sum=(i/100)*(i/100)*(i/100)
					+(i/10%10)*(i/10%10)*(i/10%10)
					+(i%10)*(i%10)*(i%10);
			if(sum == i){
				System.out.println(i);
			}
		}       
	}
}
  1. 编程题:
    打印从输入数字开始到100的所有与7无关数字。与7无关 就是 不能是7的倍数 并且 个位数不能是7 并且 十位数也不能是7。

    public class Seven {
    	public static void main(String[] args) {
    		Scanner sc=new Scanner(System.in);
    		System.out.println("请输入一个100以内的正数");
    		for(int i=sc.nextInt();i<=100;i++){
    			if(i<0){
    				System.out.println("输入不合法");
    				return;
    			}else if(i%7 !=0 && i%10 != 7 && i/10%10 != 7){
    				System.out.println(i);
    			}
    		}
    	}
    }
    
  2. 猜数字小游戏。 先生成一个 1到100的随机数,然后让用户输入整数猜数字,
    如果输入的数比随机数大,打印猜大了;或者输入的数比随机数小,打印猜小了;如果相同,打印猜对了,退出循环。

    public class Guess {
    	public static void main(String[] args) {
    		Random ran = new Random();
    		int num=ran.nextInt(100)+1;
    		Scanner sc=new Scanner(System.in);
    		for(;;){
    			System.out.println("请输入一个数字");
    			int num1=sc.nextInt();
    			if(num1>num){
    				System.out.println("猜大了");
    			}else if(num1<num){
    				System.out.println("猜小了");
    			}else{
    				System.out.println("猜对了");
    				break;
    			}
    		}
    		System.out.println("恭喜你答对了!");
    	}
    }
    
  3. 用循环解决数学问题:搬砖问题。 有36人,36块砖。 男人每人每次搬4块,女人每人每次搬3块,小孩每2人搬一块,正好一次搬完,每个人都搬了一次。问几男几女几小孩? 3,3,30

    public class Brick {
    	public static void main(String[] args) {
    		for(int i=0;i<36;i++){
    			for(int j=0;j<36;j++){
    				if(4*i+3*j+(36-i-j)*0.5==36){
    					System.out.println("男有"+i+"人");
    					System.out.println("女有"+j+"人");
    					System.out.println("小孩有"+(36-i-j)+"人");
    				}
    			}
    		}
    	}
    }
    
  4. 打印2-200的所有素数/质数。 只能被1和它本身整除的数叫素数。 2 3 5 7 11 …

    public class Prime {
    	public static void main(String[] args) {	
    		boolean flag=true;
    		for(int i=2;i<=200;i++){
    			for(int j=2;j<=Math.sqrt(i);j++){
    				if(i%j==0){
    					flag=false;
    					break;
    				}			
    			}
    			if(flag){
    				System.out.println(i);
    			}
    			flag=true;
    		}
    	}
    }
    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值