JAVASE,
分享于 点击 28367 次 点评:216
JAVASE,
JAVASEday05
一.循环语句
1.循环的分类
2. 循环的使用和执行过程
1. for和while之间是可以完全互换的.
3. while(循环继续条件){
需要被循环执行的代码//循环体
}
4.while(循环继续条件){
循环体
循环间距
}
- while(true){
循环体
循环间距
if(循环结束条件){
break;
}
}
5.do-while 先执行 再判断
do{
循环体;//循环初始化 循环间距
}while(循环结束条件)
for语句
for( 循环初始化 ; 循环继续条件 ; 循环间距){
循环体
6.执行过程:循环初始化->循环继续条件->循环体->循环间距
二.方法重载(概述详细请见后续章节)
1.什么叫函数
1.函数也叫方法
2.指的就是一段具有独立功能的代码片段
3.为了减少代码会有重复->代码冗余的现象
4.函数规范:修饰符 返回值类型 函数名(参数列表){
函数体
}
5.修饰符:访问权限
6.返回值类型:如果需要返回的数据值int 返回值类型int 如果不需要返回数据 返回值类型void
7.函数名:就是对这一片段进行起名
8.参数列表:函数需要用到的一些数据 可以给 可以不给
9.return:仅仅代表结束当前函数 如果有返回值 则在函数结束时 将数据进行返回
参数传递示例
public class FunctionDemo2 {
public static void main(String[] args) {
int a=10;
int b=20;
//实际参数:给形式参数赋值
System.out.println(show(a,b));//7
System.out.println(a);//10
System.out.println(b);//20
}
public static int show(int a,int b){//形式参数 接收实际参数传来的值
a=3;
b=4;
return a+b;
}
}
2. 重载:
在同一类中 如有同名函数 则称之为函数之间为重载关系
重载的前提是 函数重名
与 修饰符 返回值类型 参数列表没有关系
仅仅和参数列表中 参数类型的排列组合有关系
重载示例
public class FunctionDemo {
public static void main(String[] args) {
System.out.println(pow(2,-4));
int a=3;
int b=7;
System.out.println(add(a,b));
double d1=3.1;
double d2=3.2;
System.out.println(add(d1,d2));
//The method add(int, int) in the type FunctionDemo is not applicable for the arguments (double, double)
}
//计算加法运算
public static int add(int n,int m){
return n+m;
}
// private static double add(double n,double m){
// return n+m;
// }
// private static double add(double n,int m){
// return n+m;
// }
// private static double add(int n,double m){
// return n+m;
// }
// private static double add(int n,double m,char c,long l,String s,Object c){
// return n+m;
// }
// private static double add(double n,double m){
// return n+m;
// }
//计算一个数字n的m次幂
public static double pow(int n,int m){//2 -4
double res=1.0;
if(m==0){
return 1.0;
}else if(m>0){
for(int i=1;i<=m;i++){
res=res*n;
}
return res;
}else{
for(int i=1;i<=Math.abs(m);i++){
res=res*n;
}
return 1/res;
}
}
}
相关文章
- 暂无相关文章
用户点评