方法定义和调用
package com.ithema_01;
/*
方法:是将具有独立功能的代码块组织成为一个整体,使其具有特殊功能的代码集
定义格式:
public static void 方法名( ) {
/方法体
}
调用格式:
方法名();
*/
public class MethodDemo {
public static void main(String[] args) {
//调用方法
isEvenNumber();
}
//需求:定义一个方法,在方法中定义一个变量,判断该数据是否是偶数
public static void isEvenNumber(){
//定义变量
int nuber=10;
nuber=9;
//判断改数据是否是偶数
if (nuber%2==0){
System.out.println(true);
}else {
System.out.println(false);
}
}
}
方法练习
package com.ithema_01;
/*
需求:
设计一个方法用于打印两个数中的较大数
思路:
1:定义一个方法,用于打印两个数字中的较大数,例如getNax()
2:方法中定义两个变量,用于保存两个数字
3:使用分支语句分两种情况对两个数字的大小关系进行处理
4:在main()方法中调用定义好的方法
*/
public class MethodText {
public static void main(String[] args) {
//在main()方法中调用定义好的方法
getMax();
}
//定义一个方法,用于打印两个数字中的较大数,例如getNax()
public static void getMax(){
//方法中定义两个变量,用于保存两个数字
int a=10;
int b=20;
//使用分支语句分两种情况对两个数字的大小关系进行处理
if (a>b){
System.out.println(a);
}else {
System.out.println(b);
}
}
}
带参数方法定义和调用
package com.ithema_02;
/*
带参数方法的定义和调用
定义格式:
public static void 方法名(参数)i ... ... }
调用格式:
方法名(参数);
*/
public class MethodDemo {
public static void main(String[] args) {
//常量值的调用
isEvenNumber(10);
//变量的调用
int number =10;
isEvenNumber(number);
}
//需求:定义一个方法,该方法接受一个参数,判断该数据是否是偶数
public static void isEvenNumber(int number){
if (number%2==0){
System.out.println(true);
}else {
System.out.println(false);
}
}
}
带参数练习
package com.ithema_02;
/*
需求:
设计一个方法用于打印两个数中的较大数,数据来自于方法参数
思路:
1:定义一个方法,用于打印两个数字中的较大数,例如getNax()
2:为方法定义两个参数,用于接收两个数字
3:使用分支语句分两种情况对两个数字的大小关系进行处理
4:在main()方法中调用定义好的方法(使用常量)
5:在main()方法中调用定义好的方法(使用变量)
*/
public class MethodText {
public static void main(String[] args) {
//在main()方法中调用定义好的方法(使用常量)
getMax(10,20);
//调用方法是,数量和类型要对应起来
//getMax(30);
//getMax(20.0,20.0);
//在main()方法中调用定义好的方法(使用变量)
int a=10;
int b=20;
getMax(a,b);
}
//定义一个方法,用于打印两个数字中的较大数,例如getNax()
//为方法定义两个参数,用于接收两个数字
public static void getMax(int a,int b){//形参
//使用分支语句分两种情况对两个数字的大小关系进行处理
if (a>b){
System.out.println(a);
}else {
System.out.println(b);
}
}
}
带返回值方法定义和调用
package com.ithema_03;
/*
带返回值方法的定义和调用
定义格式:
public static数据类型方法名(参数){
return数据;
}
调用格式:
1 :方法名(参数);
2 :数据类型变量名=方法名(参数);
*/
public class MethodDemo {
public static void main(String[] args) {
//1 :方法名(参数);
//isEvenNumber(10);
//true;
//2 :数据类型变量名=方法名(参数);
boolean flag=isEvenNumber(10);
//boolean flag =true;
System.out.println(flag);
}
//需求:定义一个方法,该方法接收一个参数,判断该数据是否是偶数,并返回真假值
public static boolean isEvenNumber(int number){
if (number%2==0){
return true;
}else {
return false;
}
}
}
带返回值方法练习
package com.ithema_01;
/*
需求:
设计一个方法可以获取两个数的较大值,数据来自于参数
思路:
1:定义一个方法,用于获取两个数字中的较大数
2:使用分支语句分两种情况对两个数字的大小关系进行处理
3:根据题设分别设置两种情况下对应的返回结果
4:在main()方法中调用定义好的方法并使用变量保存
5:在main()方法中调用定义好的方法并直接打印结果
*/
public class Demo {
public static void main(String[] args) {
//在main()方法中调用定义好的方法并使用变量保存
int result =getMax(10,20);
System.out.println(result);
//在main()方法中调用定义好的方法并直接打印结果
System.out.println(getMax(10,20));
}
//定义一个方法,用于获取两个数字中的较大数
public static int getMax(int a,int b){
//使用分支语句分两种情况对两个数字的大小关系进行处理
//根据题设分别设置两种情况下对应的返回结果
if (a>b){
return a;
}else {
return b;
}
}
}
方法的注意事项
package com.ithema_04;
/*
方法注意事项:
方法不能嵌套定义
void表示无返回值,可以省略return,也可以单独的书写return,后面不加数据
*/
public class MethodDemo {
public static void main(String[] args) {
}
public static void methodOne(){
}
public static void methodTwo(){
//return 100;
return;
//System.out.println(100);已结束,到达不了
}
}
方法重载
package com.ithema_01;
/*
方法重载:
多个方法在同一个类中
多个方法具有相同的方法名
多个方法的参数不相同,类型不同或者数量不同
与返回值无关
在调用的时候,Java虚拟机会通过参数的不太来区分同名的方法
*/
public class MethodDemo {
public static void main(String[] args) {
//调用方法
int result=sum(10,20);
System.out.println(result);
double result2=sum(10.0,20.0);
System.out.println(result2);
int result3=sum(10,20,30);
System.out.println(result3);
}
//需求1:求两个int类型数据和的方法
public static int sum(int a,int b){
return a+b;
}
//需求2:求两个double类型数据和的方法
public static double sum(double a,double b){
return a+b;
}
//需求3:求三个int类型数据和的方法
public static int sum(int a,int b,int c){
return a+b+c;
}
}
方法重载练习
package com.ithema_05;
/*
需求:
使用方法重载的思想,设计比较两个整数是否相同的方法,兼容全整数类型(byte , short, int , Long)
思路:
1:定义比较两个数字的是否相同的方法compare()方法,参数选择两个int型参数
2:定义对应的重载方法,变更对应的参数类型,参数变更为两个Long型参数
3:定义所有的重载方法,两个byte类型与两个short类型参数
4:完成方法的调用,测试运行结果
*/
public class MethodText {
public static void main(String[] args) {
//调用方法
System.out.println(compare(10,20));
System.out.println(compare((byte) 10,(byte) 20));
System.out.println(compare((short) 10,(short) 20));
System.out.println(compare((long) 10,(long) 20));
}
//int
public static boolean compare(int a,int b){
System.out.println("int");
return a==b;
}
//byte
public static boolean compare(byte a,byte b){
System.out.println("byte");
return a==b;
}
//short
public static boolean compare(short a,short b){
System.out.println("short");
return a==b;
}
//long
public static boolean compare(long a,long b){
System.out.println("long");
return a==b;
}
}
方法参数传递基本类型
package com.ithema_06;
/*
对于基本数据类型的参数,形式参数的改变,不影响实际参数的值
*/
public class ArgsDemo01 {
public static void main(String[] args) {
int number=100;
System.out.println("调用change方法前:"+number);
change(number);
System.out.println("调用change方法后:"+number);
}
public static void change(int number){
number=200;
}
}
方法参数传递引用类型
package com.ithema_06;
/*
对于引用类型的参数,形式参数的改变,影响实际参数的值
*/
public class ArgsDemo02 {
public static void main(String[] args) {
int[] arr={10,20,30};
System.out.println("调用change方法前:"+arr[1]);
change(arr);
System.out.println("调用change方法后:"+arr[1]);
}
public static void change(int[] arr){
arr[1]=200;
}
}
案例:数组遍历
package com.ithema_07;
/*
数组遍历
需求:
设计一个方法用于数组遍历,要求遍历的结果是在一行上的。例如:[11,22,33,44,55]
思路:
1:因为要求结果在一行上输出,所以这里需要在学习一个新的输出语句
System. out.print(“内容”);
System.out.println(“内容”);输出内容并换行
System. out.print(“内容”);输出内容不换行System. out.printLn();起到换行的作用
2:定义一个数组,用静态初始化完成数组元素初始化
3:定义一个方法,用数组遍历通用格式对数组进行遍历
4:用新的输出语句修改遍历操作
5:调用遍历方法
*/
public class MethodText01 {
public static void main(String[] args) {
// System.out.println("Hello");
// System.out.println("World");
// System.out.print("World");
// System.out.print("World");
// System.out.println();
//定义一个数组,用静态初始化完成数组元素初始化
int[] arr={11,22,33,44,55};
//调用方法
printArray(arr);
}
//定义一个方法,用数组遍历通用格式对数组进行遍历
/*
两个明确:
返回值类型:void
参数:int[]arr
*/
/*
public static void printArray(int[] arr){
for (int x=0;x< arr.length;x++){
System.out.println(arr[x]);
}
}
*/
public static void printArray(int[] arr){
System.out.print("[");
for (int x=0;x< arr.length;x++){
if (x== arr.length){
System.out.print(arr[x]);
}else {
System.out.print(arr[x]+",");
}
}
System.out.println("]");
}
}
案例:数组最大值
package com.ithema_07;
/*
数组最大值
需求:
设计一个方法用于获取数组中元素的最大值,调用方法并输出结果
思路:
1;定义一个数组,用静态初始化完成数组元素初始化
2:定义一个方法,用来获取数组中的最大值
3:调用获取最大值方法,用变量接收返回结果
4:把结果输出在控制台
*/
public class MethodText02 {
public static void main(String[] args) {
//定义一个数组,用静态初始化完成数组元素初始化
int[] arr ={12,45,89,73,60};
//调用获取最大值方法,用变量接收返回结果
int number=getMax(arr);
//把结果输出在控制台
System.out.println("number:"+number);
}
//定义一个方法,用来获取数组中的最大值
/*
两个明确:
返回值类型: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;
}
}