Java程序逻辑控制


程序主要分为三种逻辑:顺序、分支和循环。

分支语句

if分支语句

if分支语句是最为基础的分支操作,但是其有三种使用形式。
在这里插入图片描述
范例:使用if判断

public class Hello {
	public static void main(String args[]){
		double money = 9.9;
		if(money == 9.9){
			System.out.println("可以购买此DVD(进口的...)");//可以购买此DVD(进口的...)
		}	
	}
}
public class Hello {
	public static void main(String args[]){
		double score = 89.9;
		if(score >= 90 && score<=100){
			System.out.println("考试成绩优秀");
		}else if(score >= 80 && score<90){
			System.out.println("考试成绩良好");
		}else if(score >= 70 && score<80){
			System.out.println("考试成绩中等");
		}else if(score >= 60 && score<70){
			System.out.println("考试成绩及格");
		}else
		System.out.println("哎呦,没及格");
	}
}

在这里插入图片描述
在整个开发过程中,分支语句是使用最多的,并且往往可能会结合判断条件一起判断。

switch分支语句(很少会优先考虑)

利用if…else可以实现多条件的验证,而switch只能进行多数值的判断,而且switch本身支持的判断数据类型:

  • 标准做法:int、char;
  • JDK1.5支持了枚举;
  • JDK1.7支持了字符串。
    对于switch的语法结构如下:
    在这里插入图片描述
    范例:观察switch操作
public class Hello {
	public static void main(String args[]){
		int ch = 2;
		switch(ch){
			case 2:{
				System.out.println("数字是2。");
			}
			case 1:{
				System.out.println("数字是1。");
			}
			default:{
				System.out.println("没有满足的判断数据。");
			}
		}
	}
}

在这里插入图片描述
在使用switch语句之中,如果在每一个case之后不加上break。那么就表示在第一个case满足条件之后将一直执行,执行到遇见break或整个switch执行完毕为止。所以好的习惯一定要在每个case后跟上break。

public class Hello {
	public static void main(String args[]){
		int ch = 2;
		switch(ch){
			case 2:{
				System.out.println("数字是2。");
				break;
			}
			case 1:{
				System.out.println("数字是1。");
				break;
			}
			default:{
				System.out.println("没有满足的判断数据。");
				break;
			}
		}
	}
}

![在这里插入图片描述](https://img-blog.csdnimg.cn/20190331223424355.png#pic_center)
从JDK1.7开始,switch进行了扩充,支持了字符串验证。
范例:判断字符串

public class Hello {
	public static void main(String args[]){
		String str="hello";
		switch(str){
			case "Hello":{
				System.out.println("字符串是Hello。");
				break;
			}
			case "hello":{
				System.out.println("字符串是hello。");
				break;
			}
			default:{
				System.out.println("没有满足的判断数据。");
				break;
			}
		}
	}
}

在这里插入图片描述
在进行字符串判断的时候,依然需要考虑到大小写问题。

循环语句

循环的意义本身非常好理解,就是让一串代码重复执行。但是在循环语法中,实际上定义了两类循环:while循环、for循环。

while循环语法

while循环中分为两种形式:while、do…while.
在这里插入图片描述
下面利用循环实现1~100的累加。
范例:使用while循环实现累加操作

public class Hello {
	public static void main(String args[]){
		//希望在循环之中x的内容可以按照1、2、3、...、100的方式增长
		//实际上当x增长到100的时候那么就表示循环的结束
		int x = 1;//循环的初始变量
		int sum = 0;//保存循环的计算结果
		//x=1、x=2、...、x=100、x=101
		while(x<=100){//判断x的内容是否小于100
			sum+=x;//进行数据的累加
			x++;//修改循环的控制,x自增1
		}
		System.out.println(sum);//5050
	}
}

x的初始值为101的结果:
在这里插入图片描述
范例:使用do…while循环

public class Hello {
	public static void main(String args[]){
		//希望在循环之中x的内容可以按照1、2、3、...、100的方式增长
		//实际上当x增长到100的时候那么就表示循环的结束
		int x = 1;//循环的初始变量
		int sum = 0;//保存循环的计算结果
		//x=1、x=2、...、x=100、x=101
		do{//判断x的内容是否小于100
			sum+=x;//进行数据的累加
			x++;//修改循环的控制,x自增1
		}while(x<=100);
		System.out.println(sum);//5050
	}
}

x的初始值为101的结果:
在这里插入图片描述
while与do…while的最大区别在于:while先判断后执行,而do…while先执行一次再判断,如果条件不满足,while执行0次,do…while执行1次。以后开发不许用do…while,就记住while。

for循环

for循环的语法要比while循环简化,其形式如下:
在这里插入图片描述
范例:利用for循环实现累加操作

public class Hello {
	public static void main(String args[]){
		int sum = 0;
		for(int x = 1;x<= 100;x++){
			sum+=x;
		}
		System.out.println(sum);//5050
	}
}

但是对于for循环注意下,有人有如下习惯:

public class Hello {
	public static void main(String args[]){
		int sum = 0;
		int x = 1;
		for(;x<= 100;){
			sum+=x;
			x++;
		}
		System.out.println(sum);//5050
	}
}

不提倡。

总结

现在给出了while和for两种循环,那么实际开发中用哪个

  • 如果已经明确知道循环次数就使用for循环;
  • 如果不知道循环次数,但是知道循环结束条件,那么就使用while循环。

循环控制(开发过程中基本不太用)

循环默认情况下肯定要一直执行,直到循环条件满足为止,但是在循环的执行中提供两循环控制语句:continue(中断一次),break(跳出循环 )。
范例:观察continue

public class Hello {
	public static void main(String args[]){
		for(int x=0;x<10;x++){
			System.out.println(x);
		}
	}
}

在这里插入图片描述

public class Hello {
	public static void main(String args[]){
		for(int x=0;x<10;x++){
			if(x==2){
				continue;//此语句之后的循环体代码不执行,直接结束本次循环
			}
			System.out.println(x);
		}
	}
}

在这里插入图片描述
说明,遇上一串代码相比,增加了continue循环控制语句,当判断x=2时,直接跳出循环,及不执行输出2的操作,故,与上面的结果相比,运行结果中少了一个2。
范例:观察break

public class Hello {
	public static void main(String args[]){
		for(int x=0;x<10;x++){
			if(x==2){
				break;//整个循环退出
			}
			System.out.println(x);
		}
	}
}

在这里插入图片描述
一般这样的两个语句都会结合if语句一起使用。
范例:循环嵌套

public class Hello {
	public static void main(String args[]){
		int line = 5;//打印5行
		for(int x=0;x<line;x++){//控制行
			for(int y=x;y<line;y++){//控制列
				System.out.print(" ");
			}
			for(int y=0;y<=x;y++){
				System.out.print("* ");
			}
			System.out.println();
		}
	}
}

!在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值