方法/函数的封装格式:
概念:方法可以把相对独立的某个功能抽象出来,使之成为程序中的一个独立实体,可以在同一程序或其他程序中多次重复使用
类型:1、无参无返回值
- 有参无返回值
- 有参有返回
- 无参有返回值
修饰符 返回值 方法名(参数的类型 参数的变量名){
方法体
}
修饰符:public 修饰,一般方法都是用public
返回值:可以是基础数据类型或者复合数据类型,如果没有返回值用void表示
如果有 方法体中结束的时候用return返回对应内容
参数:可以有或者没有,如果有,格式为(参数类型 参数的变量名)
使用main调用,main方法对应类里面的方法的时候,只能调用也有static修饰的方法
调用:
在同一个类里面调用方法
gameHllo()等同与 这个类 this.gameHello()
如果调用其他类里面的方法
先实例化这个类
类的变量名.方法名
方法:
package com.yunshen.home;
public class HomeUtil {
//第一题
public void showLove(int item) {//item为形参
for (int i = 0; i < item; i++) {
System.out.println("云神");
}
}
//第二题
public void ps(String str) {//形参,只有在后面的括号中可以使用,在调用方法的时候,传入的是实参
System.out.println(str);
}
//第三题
public void showIntArr(int [] arr) {
System.out.print("arr中的值");
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
}
//第四题
public void showCharArr2(char[][] chr) {
// TODO Auto-generated method stub
for (int i = 0; i < chr.length; i++) {
for (int j = 0; j < chr.length; j++) {
System.out.print(chr[i][j] + " ");
}
System.out.println();
}
}
//第7题
public int findMax(int[] arr) {
// TODO Auto-generated method stub
int max = 0;
for (int i = 0; i < arr.length; i++) {
if (arr[i] > max) {
max = arr[i];
}
}
return max;
}
//第9题
public int[] addNum(int num, int[] arr) {
// TODO Auto-generated method stub
//获取新数组
int [] newArr = new int [arr.length + 1];
for (int i = 0; i < arr.length; i++) {
newArr[i] = arr[i];//把旧数组放到新数组
}
newArr[newArr.length - 1] = num;//newArr.length - 1是数组的最后一位数
return newArr;
}
}
package com.yunshen.home;
import java.util.Scanner;
/*
*
* 在main方法中根据键盘插入一个1-3000之间的数字n,另外写一个方法showLove,传入这个n,循环n次“云神”
*
*/
public class Home1 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("输入一个1-3000之间的数字");
int num = scanner.nextInt();
if (num > 0 && num <3000) {
/*
for (int i = 0; i < num; i++) {
System.out.println("云神");
}
*/
//实例化工具
HomeUtil hu = new HomeUtil();
hu.showLove(num);
}else {
System.out.println("输入有误");
}
}
}
package com.yunshen.home;
import java.util.Scanner;
/*
*
* 在main方法中定义一个字符串,另外写一个方法ps,传入这个字符串,此方法专门负责将传入的字符串打印出来
*
*/
public class Home2 {
public static void main(String[] args) {
String str = "我是个字符串";
// 实例化工具
HomeUtil hu = new HomeUtil();
hu.ps(str);
}
}
package com.yunshen.home;
/*
*
* 在main方法中定义一个数组,写一个方法showInterArr,专门用来遍历int类型的一维数组
*
*/
public class Home3 {
public static void main(String[] args) {
int [] arr = {1,22,333,44,5};
HomeUtil hu = new HomeUtil();
hu.showIntArr(arr);
}
}
package com.yunshen.home;
/*
*
* 写一个方法showCharArr2,专门用来遍历char类型的二维数组
*
*/
public class Home4 {
public static void main(String[] args) {
char [][] chr = {
{'0','0','0','0','0'},
{'0','0','0','0','0'},
{'0','0','0','0','0'},
{'0','0','0','0','0'},
{'0','0','0','0','0'}
};
HomeUtil hu = new HomeUtil();
hu.showCharArr2(chr);
}
}
package com.yunshen.home;
/*
*
* 7.在main方法中有一个int类型的一维数组,写一个方法findMax,用于获取数组中的最大值,调用方法之后将返回的最大值赋值给一个变量a,把a打印出来
*
*/
public class Home5 {
public static void main(String[] args) {
int [] arr = {1,-43,23,345,34};
HomeUtil hu = new HomeUtil();
int a = hu.findMax(arr);
System.out.println(a);
}
}
package com.yunshen.home;
import java.util.Scanner;
/*
*
* 9.在main方法中定义一个int类型的一维数组,根据键盘输入一个数字,写一个方法addNum,传入这个数字和数组,把这个
* 数字加到这个数组的末尾,之后在main方法中调用addNum的方法把返回的数组赋值给原来的数组,并遍历原来的数组
*
*
*/
public class Home9 {
public static void main(String[] args) {
int [] arr = {1,22,33,44,555,6};
Scanner scanner = new Scanner(System.in);
System.out.println("输入一个数字:");
HomeUtil hu = new HomeUtil();
int num = scanner.nextInt();
arr = hu.addNum(num,arr);
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
}
}