Java初了解(四)--- 流程控制+经典案例
补充:运算符(自增自减)
- 自增自减运算符存在于Java/Python等高级语言中,它的作用是在运算结束前(前置自增自减运算符)或后(后置自增自减运算符)将变量的值加(或减)一。
相较于这些语言中的+=和-=运算符,自增运算符更加简洁,且可以控制效果作用于运算之前还是之后,具有很大的便利性。
package cn.daniel.day03;
//自增自减运算符
public class TestAdd {public static void main(String[] args) {
//++ 单独使用
int a = 10;
int b = 10;
a++;//相当于a=a+1
++b;
System.out.println(a);//11
System.out.println(b);//11
//++ 混合使用
int c = 10;
int d = c++;//等同于,r=b,b=b+1,注意++后执行
System.out.println(d);//10
int e = 10;
int f = ++e;//注意++先执行
System.out.println(f);//11
// -- 单独使用
int g = 10;
int h = 10;
g--;//相当于o=o-1
--h;
System.out.println(g);//9
System.out.println(h);//9
// -- 混合使用
int i = 10;
System.out.println(i--);//10 --后执行
int n = 10;
System.out.println(--n);//9 -- 先执行
}
}
前置后置区别的举例
a = i++;//先赋值,后计算
相当于 a = i; i++;
a = ++i;//先计算,后赋值
相当于 i++; a = i;
(--同理)
二、异常处理
异常处理使用try、catch和finally关键字来尝试可能未成功的操作,处理失败,以及在事后清理资源。
不处理异常案例演示:
package cn.daniel.day03;
public class TestTryCatch {
public static void main(String[] args) {
//人为制造异常 2/0(报错)
int n = 2/0;//如果不管,抛出异常给调用者,main函数,抛出异常
System.out.println(n);
}
}
控制台输出:
Exception in thread "main" java.lang.ArithmeticException: / by zero
at cn.daniel.day03.TestTryCatch.main(TestTryCatch.java:6)
异常的两种处理方式:
- throws 抛出异常,自己不处理
- try-catch 自己处理异常
1、使用try、catch、finally关键字捕获异常:
语法格式为1:
try{
//可能会出现异常的代码
}catch(ParseException e){
//捕获执行的代码
};
案例展示:
package cn.daniel.day03;
public class TestTryCatch {
public static void main(String[] args) {
try { //自己管理异常
System.out.println("进入了:try");
//写可能发生异常的代码
int n = 2/0;
System.out.println("执行完了:try");
} catch (Exception e) {
// TODO: handle exception
System.out.println("进入了:catch");
//获取出错,打印错误信息
System.out.println(e.getMessage());
System.out.println("执行完了catch");
}
System.out.println("try~catch执行完毕!!!");
}
}
控制台显示:
进入了:try
进入了:catch
/ by zero
执行完了catch
try~catch执行完毕!!!
语法格式为2:
try{
//可能会出现异常的代码
}catch(Exception e){
//捕获执行的代码
}finally{
//不管是否发生异常都要执行的代码
}
案例展示:
package cn.daniel.day03;
public class TestTryCatchFinally {
public static void main(String[] args) {
try {
System.out.println("try start...");
int i = 1/0;
System.out.println("try end...");
} catch (Exception e) {
// TODO: handle exception
System.out.println("catch start...");
System.out.println(e.getMessage());
System.out.println("catch end...");
}finally {
//try 正确还是错误,都会执行这里的代码
System.out.println("finally....");
}
System.out.println("finash");
}
}
控制台输出:
try start...
catch start...
/ by zero
catch end...
finally....
finash
关键词try后的一对大括号将一块可能发生异常的代码包起来,称为监控区域。Java方法在运行过程中出现异常,则创建异常对象。将异常抛出监控区域之外,由Java运行时系统试图寻找匹配的catch子句以捕获异常。若有匹配的catch子句,则运行其异常处理代码,try-catch语句结束。
2、使用throws关键字声明抛出异常:
谁调用抛给谁去解决
分成两步:
- 抛异常 throw new Exception(“”)
- 接异常 throws Exception
案例展示:
package cn.daniel.day03;
public class TestThrowThrows {
//在main函数来处理,它内部错误,接盘
public static void main(String[] args) throws Exception {
try {
int i = 1/0;
} catch (Exception e) { // 抛出异常
//创建一个新的异常对象Exception,括号里写错误信息
throw new Exception("系统出错了,请联系管理员");
}
}
}
控制台输出:
Exception in thread "main" java.lang.Exception: 系统出错了,请联系管理员
at cn.daniel.day03.TestThrowThrows.main(TestThrowThrows.java:10)
三、程序流程控制
- 分支结构:根据判断,有些代码执行,有些代码不执行
- 顺序结构:代码依次从上往下执行
- 循环结构:反复执行一段代码
(一)分支结构
if
结构式:if(条件表达式){
语句块;
}
if~else:条件是互斥的
结构式:if(条件表达式){
语句块1;
}else{
语句块2;
}
if~else if~else
结构式:if (条件表达式1) {
语句块1;
} else if(条件表达式2) {
语句块2;
}else {
语句块3;
}
测试用例:(使用单元测试演示)
package cn.daniel.day03;
import java.util.logging.Level;
import org.junit.Test;
public class TestIfElse {
@Test
public void testif() {
//需求:判断男女
boolean isMan = true;//默认值是false
System.out.println(isMan);
if (isMan) {
System.out.println("男人");
}
}
@Test
public void testIfElse() {
//需求:判断男女
//if~else:条件是互斥的,要么真,要么假
// boolean isMan = true;
boolean isMan = false;
if (isMan) {
System.out.println("真男人");
}else {
System.out.println("女人");
}
}
@Test
public void testIfElseIfElse() {
/* * 需求: * 级别:level=1 经理 * level=2 组长 * level=3 员工 */
int level = 2;
if (level == 1) {
System.out.println("经理");
} else if(level == 2) {
System.out.println("组长");
}else if (level == 3) {
System.out.println("员工");
}else {
System.out.println("不是公司数据!");
}
}
}
- switch~case:
了解即可,基本可以通过if-else来替代,而且java的switch很笨,远不如其他语言灵活,例如vb,cpp。
结构式:
switch (key) {
case value:
break;
default:
break;
}
测试用例:
package cn.daniel.day03;
import org.junit.Test;
//多分支语句
public class TestSwitch {
@Test
public void testSwitchCase() {
int phone = 110;
switch(phone) {
case 110 :
System.out.println("我是警察");
break;
case 120 :
System.out.println("我是医生");
break;
case 119 :
System.out.println("我是消防");
break;
default :
System.out.println("错误号码!!!");
break;
}
}
}
控制台输出:我是警察
switch case 语句有如下规则:
- switch 语句中的变量类型可以是: byte、short、int 或者 char。从 Java SE 7 开始,switch 支持字符串 String 类型了,同时 case 标签必须为字符串常量或字面量。
- switch 语句可以拥有多个 case 语句。每个 case 后面跟一个要比较的值和冒号。
- case 语句中的值的数据类型必须与变量的数据类型相同,而且只能是常量或者字面常量。
- 当变量的值与 case 语句的值相等时,那么 case 语句之后的语句开始执行,直到 break 语句出现才会跳出 switch 语句。
- 当遇到 break 语句时,switch 语句终止。程序跳转到 switch 语句后面的语句执行。case 语句不必须要包含 break 语句。如果没有 break 语句出现,程序会继续执行下一条 case 语句,直到出现 break 语句。
- switch 语句可以包含一个 default 分支,该分支一般是 switch 语句的最后一个分支(可以在任何位置,但建议在最后一个)。default 在没有 case 语句的值和变量值相等的时候执行。default 分支不需要 break 语句。
switch case 执行时,一定会先进行匹配,匹配成功返回当前 case 的值,再根据是否有 break,判断是否继续输出,或是跳出判断。
(二)循环结构
for循环
结构式:for (初始化; 布尔表达式; 更新) {
语句块;
}
当判断条件不成立时,结束for循环
演示案例:
package cn.daniel.day03;
public class TestFor {
public static void main(String[] args) {
//打印一个星星
System.out.println("*");
System.out.println();
//控制台打印10个星星
for (int i = 0; i < 10; i++) {
System.out.println("*");
}
}
}
双层循环
package cn.daniel.day03;
public class TestFor2 {
public static void main(String[] args) {
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 10; j++) {
System.out.print("*");
}
System.out.println();
}
}
}
for循环习惯使用小写字母作为变量:i,j,m,n,k
注意:for循环不能交叉,必须嵌套
三重循环
for(int i=0; i<5; i++) {
for(int j=0; j<5; j++) {
for(int k=0; k<5; k++) {
System.out.println("i="+i + ", j="+j + ",k="+k);
}
}
}
问题:
内循环先执行完,还是外循环先执行完?
答:内循环
层数很多,程序可读性非常差,开发人员难理解,维护你的代码时晕了。
一般情况下java不超过3层
while循环
while循环和do-while循环差异,while循环可能一次都不执行,而do-while循环至少执行一次
(文件处理时需要最少先执行一次)package cn.tedu.hello.day03;
public class TestWhile {
public static void main(String[] args) {
for(int i=0; i<10; i++) {
System.out.print(i);
}
System.out.println();
int i = 0; //初始化
while( i<0 ) { //判断,true继续执行,false结束执行
//循环体
System.out.print(i);
i++; //一定要改变i的值,不改变死循环
}
System.out.println();
int j = 0;
do {
System.out.print(j);
j++; //在循环体内一定要改变判断条件值
}while( j<0 );
}
}
退出循环
关键字 | 作用 |
---|---|
break | break 主要用在循环语句或者 switch 语句中,用来跳出整个语句块。break 跳出最里层的循环,并且继续执行该循环下面的语句。 |
continue | 适用于任何循环控制结构中。作用是让程序立刻跳转到下一次循环的迭代。在 for 循环中,continue 语句使程序立即跳转到更新语句。在 while 或者 do…while 循环中,程序立即跳转到布尔表达式的判断语句。 |
return | 返回调用者 |
经典案例:
(一)九九乘法表
package cn.daniel.day03;
public class Test1 {
public static void main(String[] args) {
//9 * 9 乘法表
for (int i = 0; i < 10; i++) { //控制层数
for (int j = 0; j < i; j++) { //控制每层的输出
System.out.print(i+"*"+j+"="+i*j+" ");
}
System.out.println();//换行
}
}
}
控制台显示:
(二)打印菱形
package cn.daniel.day03;
public class TestPrint {
public static void main(String[] args) {
print(8);
// print(9);
}
public static void print(int size) {
//行数判断
if (size%2 == 0) {
size++;//计算菱形的大小
}
//上半部分
for (int i = 0; i < size/2 + 1; i++) {
for (int j = size/2 + 1; j > i + 1 ; j--) {
//输出左上角位置的空白处
System.out.print(" ");
}
for (int j = 0; j < 2 * i + 1; j++) {
//打印上半部分的形状
System.out.print("*");
}
System.out.println();//换行
}
//下半部分
for (int i = size/2 + 1; i < size; i++) {
for (int j = 0; j < i - size/2; j++) {
//打印左下角空白格
System.out.print(" ");
}
for (int j = 0; j < 2 * size - 1 - 2 * i; j++) {
//输出菱形下半部分
System.out.print("*");
}
System.out.println();
}
}
}
控制台显示:
(三)镂空三角形
package cn.daniel.day03;
import java.util.Scanner;
public class Test2 {
public static void main(String[] args) {
//输入层数
Scanner scanner = new Scanner(System.in);
System.out.println("请输入你要打印的层数:");
int input = scanner.nextInt();//从键盘获取数据
//控制层数
for (int i = 0; i <= input; i++) {
for (int j = input; j > i; j--) { //打印空格
System.out.print(" ");
}
//控制每层的输出量
for (int j2 = 1; j2 <= 2 * i + 1; j2++) {
//第一层和最后一层都必须打印“ * ”
if (i==0 || i==input) {
System.out.print("*");
}else if (j2==1 || j2==2*i+1) {
System.out.print("*");
}else {
System.out.print(" ");//其他层数内部输出空格
}
}
//换行
System.out.println();
}
}
}
控制台显示:
(四)镂空菱形
package cn.daniel.day03;
import java.util.Scanner;
public class Test3 {
public static void main(String[] args) {
//输入层数
Scanner scanner = new Scanner(System.in);
System.out.println("请输入你要打印的层数:");
int input = scanner.nextInt();//从键盘获取数据
//控制层数
for (int i = 0; i <= input; i++) {
for (int j = input; j > i; j--) { //打印空格
System.out.print(" ");
}
//控制每层的输出量
for (int j2 = 1; j2 <= 2 * i + 1; j2++) {
if (i==0) {
System.out.print("*");
}else if (j2==1 || j2==2*i+1) {
System.out.print("*");
}else {
System.out.print(" ");//其他层数内部输出空格
}
}
System.out.println();//换行
}
for (int i = input; i >= 0 ; i--) {
for (int j = input - i; j >= 0; j--) {
System.out.print(" ");
}
for (int j2 = 2*i-1; j2 >= 1; j2--) {
if (i == 1) {
System.out.print("*");
}else if(j2 == 1 || j2 == 2*i-1){
System.out.print("*");
}else {
System.out.print(" ");
}
}
System.out.println();
}
}
}
控制台显示:
还没有评论,来说两句吧...