在这里插入代码片
第8章 方法
01 习题
- 3000米长的绳子,每天减一半,问多少天这个绳子会小于5米。
//3000米的绳子,每天减少一半,多少天小于5米
int x = 3000;
int count = 0;
while(x >= 5) {
x = x / 2;
count++;
}
System.out.println(count);
另一种写法:
int day = 1;
int i = 3000;
while(true) {
i /= 2;
System.out.println("第" + day + "天" + i);
day++;
if(i < 5) {
break;
}
}
tip:
还可用右移,右移相当于除2。
- 打印99乘法表。
//打印99乘法表
//外循环控制行数,内循环控制算式的个数
for (int i = 1; i <= 9; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(i + "*" + j + "=" + (i * j) + " ");
}
System.out.println();
}
02 方法的定义
方法是一段可以重复执行的代码块。
方法总是预先设计好,并且在被调用的时候执行。
- 方法的调用
public class day063 {
//public 指示此方法是公有的
//static 指示此方法是静态的
//void 指示此方法没有返回值
//printX是方法的名字,必须遵循标识符命名规则
//printX方法不必在main方法之前定义
//printX是提供打印直角三角形的功能
public static void printX() {
//打印一个直角三角形
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= i; j++) {
System.out.print("*");
}
System.out.println();
}
}
public static void main(String[] args) {
//调用printX方法
//调用方法时,jvm会执行此方法的代码
printX();
//当print方法中的代码块执行结束后,才会执行main方法后续的代码
System.out.println("第一个方法调用结束");
}
}
2. 方法的重复调用
public class day063 {
//public 指示此方法是公有的
//static 指示此方法是静态的
//void 指示此方法没有返回值
//printX是方法的名字,必须遵循标识符命名规则
//printX方法不必在main方法之前定义
//printX是提供打印直角三角形的功能
public static void printX() {
//打印一个直角三角形
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= i; j++) {
System.out.print("*");
}
System.out.println();
}
}
public static void main(String[] args) {
//调用printX方法
//调用方法时,jvm会执行此方法的代码
printX();
//当print方法中的代码块执行结束后,才会执行main方法后续的代码
System.out.println("第一个方法调用结束");
//再次调用 printX方法
printX(); //方法可以被重复调用
}
}
_____________________________________________________
运行结果:
*
**
***
****
*****
第一个方法调用结束
*
**
***
****
*****
3. printX();和day063.printX(); 效果一样,
由于printX方法和当前的方法在同一个类,所以可以忽略掉类名。
public class day063 {
//public 指示此方法是公有的
//static 指示此方法是静态的
//void 指示此方法没有返回值
//printX是方法的名字,必须遵循标识符命名规则
//printX方法不必在main方法之前定义
//printX是提供打印直角三角形的功能
public static void printX() {
//打印一个直角三角形
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= i; j++) {
System.out.print("*");
}
System.out.println();
}
}
public static void main(String[] args) {
//调用printX方法
//调用方法时,jvm会执行此方法的代码
printX();
//当print方法中的代码块执行结束后,才会执行main方法后续的代码
System.out.println("第一个方法调用结束");
//再次调用 printX方法
printX(); //方法可以被重复调用
System.out.println("第二个方法调用结束");
//printX方法被声明为静态的,所以可以通过类名调用。
day063.printX(); //由于printX方法和当前的方法在同一个类,所以可以忽略掉类名
}
}
______________________________________________________
运行结果
*
**
***
****
*****
第一个方法调用结束
*
**
***
****
*****
第二个方法调用结束
*
**
***
****
*****
03 方法参数
向方法传递参数
public class day064 {
public static void printX() {
//打印一个直角三角形
for (int i = 1; i <= 6; i++) {
for (int j = 1; j <= i; j++) {
System.out.print("*");
}
System.out.println();
}
}
public static void main(String[] args){
printX();
}
}
- 向方法传递参数
public class day064 {
//int n声明了一个int类型的变量,这个变量叫做形式参数
//当printX方法被调用时,n会接受实际参数的值。
//形式参数在方法中作为变量为使用,它在被调用时赋值
public static void printX(int n) {
//打印一个直角三角形
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
System.out.print("*");
}
System.out.println();
}
}
public static void main(String[] args){
int x = 5;
//x是实际参数,它的值将被拷贝给形式参数
printX(x); //打印5行的直角三角形
x = 6;
printX(x);// 打印6行的直角三角形
printX(7); //打印7行的直角三角形
// String c = "a";
// printX(c); //编译错误,实际参数的类型必须要和形式参数匹配
char c = 'a'; //97
printX(c); //正确的,char可以自动转换为int
}
}
—————————————————————————————————————————————————————
运行结果
*
**
***
****
*****
*
**
***
****
*****
******
*
**
***
****
*****
******
*******
*
**
***
****
*****
******
*******
********
*********
**********
***********
************
*************
**************
***************
****************
*****************
******************
*******************
********************
*********************
**********************
***********************
************************
*************************
**************************
***************************
****************************
*****************************
******************************
*******************************
********************************
*********************************
**********************************
***********************************
************************************
*************************************
**************************************
***************************************
****************************************
*****************************************
******************************************
*******************************************
********************************************
*********************************************
**********************************************
***********************************************
************************************************
*************************************************
**************************************************
***************************************************
****************************************************
*****************************************************
******************************************************
*******************************************************
********************************************************
*********************************************************
**********************************************************
***********************************************************
************************************************************
*************************************************************
**************************************************************
***************************************************************
****************************************************************
*****************************************************************
******************************************************************
*******************************************************************
********************************************************************
*********************************************************************
**********************************************************************
***********************************************************************
************************************************************************
*************************************************************************
**************************************************************************
***************************************************************************
****************************************************************************
*****************************************************************************
******************************************************************************
*******************************************************************************
********************************************************************************
*********************************************************************************
**********************************************************************************
***********************************************************************************
************************************************************************************
*************************************************************************************
**************************************************************************************
***************************************************************************************
****************************************************************************************
*****************************************************************************************
******************************************************************************************
*******************************************************************************************
********************************************************************************************
*********************************************************************************************
**********************************************************************************************
***********************************************************************************************
************************************************************************************************
*************************************************************************************************
3. 方法可以有多个参数
public class day065 {
//方法可以有多个参数
//多个参数使用,分割。
public static void printX(int n, char s) {
//打印一个直角三角形
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(s);
}
System.out.println();
}
}
public static void main(String[] args) {
printX(8,'.'); //8传递给形式参数n,'.'传递给形式参数s
}
}
04 方法的返回值
- 这个方法将计算start和end之间数字的和。
public class Day066_2 {
// 这个方法将计算start和end之间数字的和
// int是返回值的类型
public static int sum(int start, int end) {
int sum = 0;
for (int i = start; i <= end; i++) {
sum = sum + i;
}
return sum;// return语句将sum的值返回给调用者
}
public static void main(String[] args) {
// 返回值会替代方法调用
int result = sum(3, 5); // 调用方法,在调用结束后将返回的值赋值给result
System.out.println(result);
}
}
tip:
public static void f1() {
}
//编译错误,如果方法声明了返回值,就必须返回一个值
public static int f2() {
}
如果方法声明了返回值,就必须返回一个值
方法实际返回的值要和声明的返回值类型匹配
强转是可以的,但是强转需要谨慎,因为可能导致数据丢失。
2. return 表示终止方法执行。
//void方法也可以使用return
public static void printX(int n) {
//打印一个直角三角形
if(n <= 0) {
return; //return 表示终止方法执行
}
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= i; j++) {
System.out.println("*");
}
System.out.println();
}
}
public static void main(String[] args) {
printX(-1);
System.out.println("执行结束");
}
}
- return语句后面不能再有其他代码。
以下代码是错误的,因为return语句后面不能再有其他代码,
但是需要注意的是:如里return语句在一个条件表达式中,后面就是可以有其他代码的,因为这个return语句有可能执行,也可能不执行。
public static int f6() {
return 0;
System.out.println("Hello"); //编译错误,return语句后面不能再有其他代码
}
05 方法重载
一个方法可以和另外一个方法有同样的方法名,但是必须有不同的参数列表,这种情况叫做方法重载。
- 例一
public class day067 {
// 参数列表是空
public static void f() {
System.out.println("空的方法");
}
// 参数列表为int
public static void f(int n) {
System.out.println("一个int形参");
}
public static void main(String[] args) {
f();
f(1);
}
}
错误示例:
参数列表为int,形参名不属于参数列表,所以会导致编译错误。
这时会提示:方法名重复
这样就不能重载了。
如下图:
下面代码可行
- 参数列表是方法所有参数类型的列表(不包括参数名),与参数类型,个数,顺序有关。
示例:返回最大值
public class Day068 {
// 方法功能:返回最大值
public static int max(int a, int b) {
return a > b ? a : b;
}
// 方法功能:返回三个数中的最大数值
public static int max(int a, int b, int c) {
return a > b ? (a > c ? a : c) : (b > c ? b : c);
}
public static double max(double a, double b) {
return a > b ? a : b;
}
public static void main(String[] args) {
System.out.println(max(3, 5)); // 找max(int,int)
System.out.println(max(3, 5, 6)); // max(int,int,int)
System.out.println(max(4.5, 6.0)); // max(double,double)
}
}
**tips: 重载的好处:只需要记一个方法名就可以了。
示例:调用字符类
示例:Math.max方法的重载。
示例:println的重载
Ctrl+左键,看一下方法的源码
06 变量的作用域
变量的作用域决定了变量可以在什么地方(哪段代码)被访问。
在块中声明的变量(块变量)只能在当前块中使用。
在方法中声明的变量(本地变量)只能在当前方法中被使用。
在类中定义的静态变量(类变量)可以在当前类的任何地方使用。
-
例一
07 类常量和类方法
在类中使用satic声明的变量叫做类变量,使用static声明的方法叫做类方法。
在类中使用static final声明的叫做类常量。
public class Day0610 {
//类常量
static final double PI = 3.14;
public static void main(String[] args) {
System.out.println(Day0610.PI);
System.out.println(PI);
//public static final double PI = 3.14159265358979323846;
System.out.println(Math.PI); //使用Math类的类常量PI
System.out.println(Math.max(6.5, 3.5)); //调用Math类的类方法max
}
}
____________________________________
3.14
3.14
3.141592653589793
6.5
导入
导入成功
由于没有public,报错Day0610.PI不可见
解决
转到Day0610.java中,
把static final double PI = 3.14;
添加成 public static final double PI = 3.14;
调Day069.java中的f方法: