-
三种控制结果分为是:顺序,条件,循环
- 顺序语句:顺序执行,根据编写的顺序,从上到下运
public static void main(String[] args){ //顺序执行,根据编写的顺序,从上到下运行 System.out.println(1); System.out.println(2); System.out.println(3); }
- 条件语句:
if(关系表达式){ 语句体; } public class Flow_Control_Statements { public static void main(String[] args) { // if 条件语句 System.out.println("---开始if条件语句---"); // 定义两个变量 int a = 10; int b = 20; int c = 10; if(关系表达式){ 语句体; } if(a==b){ System.out.println("a and b is equal"); } if (a==c){ System.out.println("a and c is equal"); } System.out.println("---结束if语句---"); System.out.println(); c = a < b ? a:b; System.out.println("三元运算符:"+ c); "========================================================" if(关系表达式) { 语句体1; }else { 语句体2; } // if else语句 System.out.println("---开始if...else语句---"); if (a % 2 == 1){ System.out.println("a是偶数"); }else{ System.out.println("a是奇数"); } System.out.println("---结束if...else语句---"); System.out.println(); "========================================================" if (判断条件1) { 执行语句1; } else if (判断条件2) { 执行语句2; } ... }else if (判断条件n) { 执行语句n; } else { 执行语句n+1; } // if...else if ...else int x = 5; int y; if (x >= 3) { y = 2 * x + 1; }else if (x >= -1 && x < 3){ y = 2 * x; }else{ y = 2 * x -1; } System.out.println("y值是:"+y); System.out.println(); int score = 50; if (score<0 || score>100){ System.out.println("your score is error!"); }else if (score>=90 && score<=100){ System.out.println("your score is excellent"); }else if (score>=80 && score<90){ System.out.println("your score is good"); }else if (score>=70 && score<80){ System.out.println("your score is fine"); }else if (score>=60 && score<70){ System.out.println("your score is pass"); }else{ System.out.println("your score is fail"); } } }
-
选择语句:
switch(表达式) { case 常量值1: 语句体1; break; case 常量值2: 语句体2; break; ... default: 语句体n+1; break; } public class Select_Statements { public static void main(String[] args) { // 判断是星期几 int weekday = 6; switch (weekday){ case 1: System.out.println("Monday"); break; case 2: System.out.println("Tuesday"); break; case 3: System.out.println("Wednesday"); break; case 4: System.out.println("Thursday"); break; case 5: System.out.println("Friday"); break; case 6: System.out.println("Saturday"); break; case 7: System.out.println("Sunday"); break; default: System.out.println("your input info is error!"); break; } System.out.println(); // 穿透性验证 int i = 5; switch (i) { case 0: System.out.println("执行case0"); break; case 5: System.out.println("执行case5"); case 10: System.out.println("执行case10"); default: System.out.println("执行default"); } } }
- switch比if else效率要高;
- switch中的表达式结果支持:byte,short,int,char;
-
for循环和while的区别:
for
循环内置了break
和continue
语句,可以方便地控制循环的流程。break
语句可以中断循环,提前退出;continue
语句可以跳过当前迭代,进入下一次迭代
-
java中参数的传递:
- 如果是基本数据类型则使用值传递,传递栈中的值
- 如果是引用数据类型则使用引用传递,传递栈中引用值地址
-
方法定义中的一些概念
- 形式参数:形参只有在被调用时才会被Java虚拟机分配内存空间,形参只有在方法内部有效。方法调用结束返回主调用方法后则不能再使用该形参变量;
- 实际参数:实参可以是常量、变量、表达式、函数等,无论实参是何种类型的量,在进行函数调用时,它们都必须有确定的值,以便把这些值传送给形参
public class Method_Demo2 {
public static void main(String[] args) {
// 调用方法getSum,传递两个整数,这里传递的实际数据又称为实际参数
// 并接收方法计算后的结果,返回值
int sum = getSum(5, 6);
System.out.println(sum);
}
/*
定义计算两个整数和的方法
返回值类型,计算结果是int
参数:不确定数据求和,定义int参数.参数又称为形式参数
*/
public static int getSum(int a, int b) {
return a + b;
}
}
- 方法重载:
- 重载的前提条件是在同一类中;方法名相关,形参类型不同相关,形参个数相关,形参类型顺序相关和返回值类型无关
- 返回值类型不同不构成重载;
public class Method_Demo3 {
public static void main(String[] args) {
//定义不同数据类型的变量
byte a = 10;
byte b = 20;
short c = 10;
short d = 20;
int e = 10;
int f = 10;
long g = 10;
long h = 20;
System.out.println(compare(a, b));
System.out.println(compare(c, d));
System.out.println(compare(e, f));
System.out.println(compare(g, h));
}
public static boolean compare(byte a, byte b){
System.out.println("====byte=====");
return a ==b;
}
public static boolean compare(short a, short b){
System.out.println("====short====");
return a ==b;
}
public static boolean compare(long a, long b){
System.out.println("====long====");
return a ==b;
}
public static boolean compare(int a, int b){
System.out.println("====int====");
return a ==b;
}
}