java基础-day5

时间:2023-03-10 07:13:48
java基础-day5

第05天 java基础知识

今日内容介绍

u 方法的概述及基本使用

u 方法的练习及注意事项

u 方法的重载及参数传递

u 方法的操作数组的练习

第1章   方法的概述及基本使用

1.1  方法定义格式及格式解释

1.1.1 方法的概述

假设有一个游戏程序,程序在运行过程中,要不断地发射炮弹(植物大战僵尸)。发射炮弹的动作需要编写100行的代码,在每次实现发射炮弹的地方都需要重复地编写这100行代码,这样程序会变得很臃肿,可读性也非常差。为了解决代码重复编写的问题,可以将发射炮弹的代码提取出来放在一个{}中,并为这段代码起个名字,这样在每次发射炮弹的地方通过这个名字来调用发射炮弹的代码就可以了。上述过程中,所提取出来的代码可以被看作是程序中定义的一个方法,程序在需要发射炮弹时调用该方法即可

简单的说:方法就是完成特定功能的代码块

在很多语言里面都有函数的定义 , 函数在Java中被称为方法

1.1.2 方法格式

修饰符 返回值类型 方法名(参数类型 参数名1,参数类型 参数名2…) {

函数体;

return 返回值;

}

1.1.3 方法格式解释

修饰符 目前记住 public static

返回值类型 用于限定返回值的数据类型

方法名 一个名字,为了方便我们调用方法

参数类型 用于接收调用方法时传入的数据的类型

参数名  用于接收调用方法时传入的数据的变量

方法体 完成功能的代码

return 结束方法,把返回值带给调用者

1.1.4 案例代码一

package com.itheima_01;

/*

* 方法:就是完成特定功能的代码块。

*

* 定义格式:

*      修饰符 返回值类型 方法名(参数类型 参数名1,参数类型 参数名2...) {

*          方法体;

*          return 返回值;

*      }

* 格式解释:

*      A:修饰符 目前记住 public static

*      B:返回值类型 用于限定返回值的数据类型

*      C:方法名 一个名字,为了方便我们调用方法

*      D:参数类型 用于接收调用方法时传入的数据的类型

*      E:参数名  用于接收调用方法时传入的数据的变量

*      F:方法体 完成功能的代码

*      G:return 结束方法,把返回值带给调用者

*

* 方法的两个明确:

*      A:返回值类型 明确功能结果的数据类型

*      B:参数列表 明确有几个参数,以及参数的类型

*

* 案例:

*      求两个数和的案例。

*/

public class MethodDemo {

}

1.2  求和方法的编写

1.2.1 定义方法注意事项

写一个方法首先有两点需要明确

返回值类型 明确功能结果的数据类型

参数列表 明确有几个参数,以及参数的类型

按照格式和两个明确来完成如下功能

1.2.2 案例代码二

/ *

* 方法的两个明确:

*      A:返回值类型 明确功能结果的数据类型

*      B:参数列表 明确有几个参数,以及参数的类型

*

* 案例:

*      求两个数和的案例。

*/

public class MethodDemo {

/*

* 写一个方法,用于求和。 两个明确: 返回值类型 int 参数列表 int a,int b

*/

public static int sum(int a, int b) {

// int c = a + b;

// return c;

return a + b;

}

public static void main(String[] args) {

}

}

1.3      求和方法的调用

有明确返回值的方法调用:

单独调用,没有意义

输出调用,有意义,但是不够好,因为我不一定非要把结果输出

赋值调用,推荐方式

1.3.1 案例代码三

package com.itheima_01;

/*

* 有明确返回值的方法的调用:

*      A:单独调用,没有什么意义。

*      B:输出调用,有意义,但是不够好,因为我可能需要对求和的结果进行进一步的操作

*      C:赋值调用

*/

public class MethodDemo2 {

// 求和的方法

public static int sum(int a, int b) {

return a + b;

}

public static void main(String[] args) {

// 单独调用

// sum(10,20);

// 输出调用

// System.out.println(sum(10,20));

// 赋值调用

int s = sum(10, 20);

// s+=100;

System.out.println("s:"+s);

}

}

1.4  求和方法的调用图解

1.4.1 方法调用流程图

第2章   方法的练习及注意事项

2.1  方法的练习

2.1.1 方法练习之获取两个数据中的较大

2.1.2 案例代码四

package com.itheima_01;

import java.util.Scanner;

/*

* 需求:键盘录入两个数据,返回两个数中的较大值

*

* 两个明确:

*      返回值类型:int

*      参数列表:int a,int b

*/

public class MethodTest {

// 返回两个数中的较大值

public static int getMax(int a, int b) {

if (a > b) {

return a;

} else {

return b;

}

}

public static void main(String[] args) {

//创建对象

Scanner sc = new Scanner(System.in);

//接收数据

System.out.println("请输入第一个数据:");

int x = sc.nextInt();

System.out.println("请输入第二个数据:");

int y = sc.nextInt();

//调用方法

int max = getMax(x,y);

System.out.println("max:"+max);

}

}

2.1.3 方法练习之比较两个数据是否相等

2.1.4 案例代码五

package com.itheima_01;

import java.util.Scanner;

/*

* 需求:键盘录入两个数据,比较两个数是否相等

*

* 两个明确:

*      返回值类型:boolean

*      参数列表:int a,int b

*/

public class MethodTest2 {

//比较两个数是否相等

public static boolean compare(int a,int b){

if(a==b){

return true;

}else {

return false;

}

}

public static void main(String[] args) {

//创建对象

Scanner sc = new Scanner(System.in);

//接收数据

System.out.println("请输入第一个数据:");

int a = sc.nextInt();

System.out.println("请输入第二个数据:");

int b = sc.nextInt();

//调用方法

boolean flag = compare(a,b);

System.out.println("flag:"+flag);

}

}

2.1.5 方法练习之获取三个数据中的较大值

2.1.6 案例代码六

package com.itheima_01;

import java.util.Scanner;

/*

* 需求:键盘录入三个数据,返回三个数中的最大值

*

* 两个明确:

*      返回值类型:int

*      参数列表:int a,int b,int c

*/

public class MethodTest3 {

// 返回三个数中的最大值

public static int getMax(int a, int b, int c) {

if (a > b) {

if (a > c) {

return a;

} else {

return c;

}

} else {

if (b > c) {

return b;

} else {

return c;

}

}

}

public static void main(String[] args) {

//创建对象

Scanner sc = new Scanner(System.in);

//接收数据

System.out.println("请输入第一个数据:");

int a = sc.nextInt();

System.out.println("请输入第二个数据:");

int b = sc.nextInt();

System.out.println("请输入第三个数据:");

int c = sc.nextInt();

//调用方法

int max = getMax(a,b,c);

System.out.println("max:"+max);

}

}

2.1.7 void修饰的方法的调用

写一个方法,在控制台输出10次HelloWorld案例。

没有明确返回值的函数调用:

其实就是void类型方法的调用

只能单独调用

2.1.8 案例代码七

package com.itheima_02;

/*

* 需求:写一个方法,在控制台输出10次HelloWorld案例。

*

* 两个明确:

*      返回值类型:void

*      参数列表:无参数

*

* 如果一个方法没有明确的返回值类型,java提供了void进行修饰。

*

* void修饰的方法的调用:

*      A:单独调用

*/

public class MethodDemo {

//在控制台输出10次HelloWorld案例。

public static void printHelloWorld() {

for(int x=1; x<=10; x++) {

System.out.println("HelloWorld");

}

}

public static void main(String[] args) {

//单独调用

printHelloWorld();

//输出调用

//System.out.println(printHelloWorld());

//System.out.println(void);

//赋值调用

//void v = printHelloWorld();

}

}

2.1.9 打印1到n之间的数据

2.1.10         案例代码八

package com.itheima_02;

/*

* 需求:写一个方法,传递一个整数(大于1),在控制台打印1到该数据的值。

*

* 两个明确:

*      返回值类型:void

*      参数列表:int n

*/

public class MethodTest {

//在控制台打印1到该数据n的值

public static void printNumber(int n) {

for(int x=1; x<=n; x++) {

System.out.println(x);

}

}

public static void main(String[] args) {

printNumber(10);

System.out.println("-------------------");

printNumber(100);

}

}

2.1.11         打印所有的水仙花数

2.1.12         案例代码九

package com.itheima_02;

/*

* 写一个方法,把所有的水仙花数打印在控制台

*

* 两个明确:

*      返回值类型:void

*      参数列表:无参数

*/

public class MethodTest2 {

//把所有的水仙花数打印在控制台

public static void printFlower() {

for(int x=100; x<1000; x++) {

int ge = x%10;

int shi = x/10%10;

int bai = x/10/10%10;

if((ge*ge*ge+shi*shi*shi+bai*bai*bai) == x){

System.out.println(x);

}

}

}

public static void main(String[] args) {

printFlower();

}

}

第3章   方法的重载及参数传递

3.1  方法重载的概述和基本使用

在同一个类中,允许存在一个以上的同名方法,只要它们的参数个数或者参数类型不同即可。

方法重载特点

与返回值类型无关,只看方法名和参数列表

在调用时,虚拟机通过参数列表的不同来区分同名方法

3.1.1 案例代码十

package com.itheima_03;

/*

* 方法重载:在同一个类中,出现了方法名相同的方法,这就是方法重载。

* 方法重载特点:

*      方法名相同,参数列表不同。与返回值无关。

*      参数列表不同:

*          参数的个数不同。

*          参数对应的类型不同。

* 注意:

*      在调用方法的时候,java虚拟机会通过参数列表的不同来区分同名的方法。

*/

public class MethodDemo {

public static void main(String[] args) {

int a = 10;

int b = 20;

// 求和

int result = sum(a, b);

System.out.println("result:" + result);

int c = 30;

// 求和

//int result2 = sum2(a,b,c);

//System.out.println("result2:"+result2);

result = sum(a,b,c);

System.out.println("result:"+result);

}

//两个float类型的数据求和

public static float sum(float a,float b) {

return a + b;

}

// 三个整数的求和

public static int sum(int a,int b,int c) {

return a + b + c;

}

/*

public static int sum2(int a, int b, int c) {

return a + b + c;

}

*/

// 两个整数的求和

public static int sum(int a, int b) {

return a + b;

}

}

3.2  方法重载练习

3.2.1 方法重载练习之比较数据是否相等

3.2.2 代码案例十一

package com.itheima_03;

/*

* 需求:比较两个数据是否相等。参数类型分别为两个byte类型,两个short类型,两个int类型,两个long类型,

*     并在main方法中进行测试

*/

public class MethodTest {

public static void main(String[] args) {

// 调用

System.out.println(compare(10, 20));

System.out.println("-------------");

System.out.println(compare((byte)10, (byte)20));

System.out.println("-------------");

System.out.println(compare((short)10, (short)20));

System.out.println("-------------");

//System.out.println(compare((long)10, (long)20));

System.out.println(compare(10L, 20L));

}

// 两个byte类型的

public static boolean compare(byte a, byte b) {

System.out.println("byte");

// 第一种写法

// boolean flag = a==b?true:false;

// return flag;

// 第二种写法

// boolean flag = a == b;

// return flag;

// 第三种写法

return a == b;

}

// 两个short类型的

public static boolean compare(short a, short b) {

System.out.println("short");

return a == b;

}

// 两个int类型的

public static boolean compare(int a, int b) {

System.out.println("int");

return a == b;

}

// 两个long类型的

public static boolean compare(long a, long b) {

System.out.println("long");

return a == b;

}

}

3.3  方法中参数传递

3.3.1 方法的形式参数为基本数据类型

方法的参数是基本类型的时候:

形式参数的改变不影响实际参数。

形式参数:用于接收实际数据的变量

实际参数:实际参与运算的变量

3.3.2 代码案例十二

public class ArgsDemo {

public static void main(String[] args) {

// 定义变量

int a = 10;

int b = 20;

System.out.println("a:" + a + ",b:" + b);// a:10,b:20

change(a, b);

System.out.println("a:" + a + ",b:" + b);// a:10,b:20

}

public static void change(int a, int b) { // a=10,b=20

System.out.println("a:" + a + ",b:" + b);// a:10,b:20

a = b; // a=20;

b = a + b;// b=40;

System.out.println("a:" + a + ",b:" + b);// a:20,b:40

}

}

3.3.3 方法的形式参数是基本类型图解

3.3.4 方法的形式参数为引用数据类型

3.3.5 代码案例十三

package com.itheima_04;

/*

* 方法的参数是引用类型:

*      形式参数的改变直接影响实际参数

*/

public class ArgsDemo2 {

public static void main(String[] args) {

// 定义数组

int[] arr = { 1, 2, 3, 4, 5 };

// 遍历数组

for (int x = 0; x < arr.length; x++) {

System.out.println(arr[x]);

}

System.out.println("----------------");

change(arr);

for (int x = 0; x < arr.length; x++) {

System.out.println(arr[x]);

}

}

public static void change(int[] arr) {

for (int x = 0; x < arr.length; x++) {

// 如果元素是偶数,值就变为以前的2倍

if (arr[x] % 2 == 0) {

arr[x] *= 2;

}

}

}

}

3.3.6
方法的形式参数是引用类型图

第4章  
方法的操作数组的练习

4.1  方法操作数组练习

4.1.1 方法的练习之数组遍历

需求:把遍历数组改进为方法实现,并调用方法

4.1.2 代码案例十四

package com.itheima_05;

/*

* 需求:把遍历数组改进为方法实现,并调用方法

*/

public class MethodTest {

public static
void main(String[] args) {

// 定义数组

int[] arr =
{ 11, 22, 33, 44, 55 };

// 遍历

// for (int x = 0; x < arr.length; x++) {

// System.out.println(arr[x]);

// }

//用方法改进

//printArray(arr);

//这一次虽然可以,但是我觉得格式不好看,能不能打印成下面的格式呢?

//[元素1, 元素2, 元素3,
...]

printArray(arr);

}

public static
void printArray(int[] arr)
{

System.out.print("[");

for(int
x=0; x<arr.length; x++){

if(x==arr.length-1){

System.out.println(arr[x]+"]");

}else {

System.out.print(arr[x]+", ");

}

}

}

/*

* 两个明确:

*      返回值类型:void

*      参数列表:int[] arr

*/

//  public static void printArray(int[] arr) {

//      for(int x=0; x<arr.length; x++){

//          System.out.println(arr[x]);

//      }

//  }

}

4.1.3 方法的练习之数组获取最值

需求:把获取数组最值改进为方法实现,并调用方法

4.1.4 代码案例十五

/*

* 需求:把获取数组最值改进为方法实现,并调用方法

*/

public class MethodTest2 {

public static void main(String[] args) {

// 定义数组

int[] arr = { 34, 67, 10, 28, 59 };

//获取数组中的最大值

// //定义参照物

// int max = arr[0];

// //遍历,依次比较,大的留下来

// for(int x=1;
x<arr.length; x++) {

// if(arr[x] > max) {

// max = arr[x];

// }

// }

//用方法改进

int max = getMax(arr);

System.out.println("max:"+max);

//获取数组中的最小值,用方法实现

int min = getMin(arr);

System.out.println("min:"+min);

}

//获取数组中的最小值的方法

public static int getMin(int[] arr)
{

int min = arr[0];

for(int x=1; x<arr.length; x++) {

if(arr[x] < min) {

min = arr[x];

}

}

return min;

}

/*

* 两个明确:

*      返回值类型:int

*      参数列表:int[]
arr

*/

public static int getMax(int[] arr)
{

int max = arr[0];

for(int x=1; x<arr.length; x++) {

if(arr[x] > max) {

max = arr[x];

}

}

return max;

}

}

4.1.5 方法的练习之数组元素求和

需求:写一个方法,用于对数组进行求和,并调用方法。

4.1.6 代码案例十六

package com.itheima_05;

/*

* 需求:写一个方法,用于对数组进行求和,并调用方法。

*/

public class MethodTest3 {

public static
void main(String[] args) {

// 定义数组

int[] arr =
{ 1, 2, 3, 4, 5 };

// //定义求和变量

// int sum = 0;

// //获取数组中的每一个元素

// for(int x=0; x<arr.length; x++) {

// sum += arr[x];

// }

//用方法改进

int sum = sum(arr);

System.out.println("sum:"+sum);

}

/*

* 两个明确:

*      返回值类型:int

*      参数列表:int[] arr

*/

public static
int sum(int[]
arr) {

int sum =
0;

for(int
x=0; x<arr.length; x++) {

sum += arr[x];

}

return sum;

}

}