目录
if - else 语句
switch语句
for、while循环
方法重载
笔记基于
if - else 语句
public class IfYuju{
public static void main(String[] args){
fangF1(4); //函数调用方式1:直接调用,out:4
System.out.println("------------");
System.out.println(fangF2(3)); //函数调用方式2:打印调用,out:3 <=3
System.out.println("------------");
int number = fangF3(3); //函数调用方式3:赋值调用
System.out.println(++number); //out:4
}
public static void fangF1(int a){ //无返回值,返回类型声明为void
if(a > 3){ //单if语句
System.out.println(a);
}
}
public static String fangF2(int a){ //返回a,返回类型声明为String
if(a > 3){ //if—else语句
return a + " >3";
}else{
return a +" <=3";
}
}
public static int fangF3(int a){ //函数调用方式3:赋值调用,返回a,返回类型声明为int
if(a<3){ //if-else扩展
a ++;
}else if(a == 3){
a = a;
}else if(a > 3){
a --;
}
return a;
}
}
switch语句
public class SwitchYuju{
public static void main(String[] args){
int num = 3;
switch(num){ // ()内为表达式
case 1: // 检查swich后()内的返回值
System.out.println("Monday"); //从上到下依次执行,
break; //到符合的case开始执行语句体
case 2: //到break停止执行
System.out.println("Tuesday"); //如果中间缺少某个break,就会发生“穿透”
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;
}
}
}
for、while循环
public class ForYuju{
public static void main(String[] args){
for(int i = 1;i <101; i++){
System.out.println("抄写一百遍"+ i);
}
System.out.println("====================");
int i = 1;
while(i<11){
System.out.println("抄写十遍" + i);
i ++;
}
System.out.println("====================");
int j = 1;
do{
System.out.println("do-while语句最少执行一次循环体");
j ++;
}while(j<10);
}
}
方法重载
public class OverLoad{
public static void main(String[] args){
System.out.println(sum(1, 3)); //out:4
System.out.println(sum(2.3, 3.2)); //out:5.5
System.out.println(sum(1,2,3)); //out:6
}
public static int sum(int a,int b){
return a + b;
}
public static double sum(double a, double b){
return a + b;
}
public static int sum(int a, int b, int c){
return a + b + c;
}
/*
public static double sum(int a, int b){
return a+b;
}
错误,重载时,不能让同一输入同时被两种方法接收*/
}
笔记基于
Java基础+进阶_哔哩哔哩_bilibilihttps://www.bilibili.com/video/BV1uJ411k7wy不足与错误之处,敬请指正。
|