1:函数
(1)概念:定义在类中的有特殊功能的的一段代码。
(2)函数的格式:
修饰符 返回值类型 函数名(形参类型 形式参数1,形参类型 形式参数2...)
{
函数体;
return 返回值;
}
具体解释如下:
A:修饰符: public static
B:返回值类型:程序最终结果的数据类型。
C:函数名:就是函数的名字,方便我们调用。
D:参数:
形式参数:接受实际参数的变量。
形参类型:就是数据类型。
实际参数:实际参与操作的变量或常量。
E:函数体:完成某种功能的代码。
F:返回值:程序的最终结果。
G:return 返回值 :是哪里调用程序,return就把结果返回到哪里。
(3)函数的特点:
A:函数与函数之间是平级关系,不能在函数中在定义函数。
B:函数只有被调用才执行。
(4)函数的调用
A:有明确返回值
a:单独调用 一般没有意义。
b:输出调用 但是如果想拿结果以后继续操作,就有问题了。所以,不好。
c:赋值调用 推荐方式。
B:void类型
单独调用
(5)案例:
1.有明确返回值的例子。
求两个数的最大值
public class Max {
public static void main(String[] args) {
int x = 20;
int y = 30;
System.out.println("大的数是" + getMax(x, y));
}
public static int getMax(int x, int y) {
int max;
if (x > y) {
max = x;
} else {
max = y;
}
return max;
}
}
2.void类型例子。
九九乘法表
public class FunctionDemo {
public static void main(String[] args) {
jiuJiu();
}
private static void jiuJiu() {
for(int i=1;i<=9;i++){
for(int j=1;j<=i;j++){
System.out.print(j+"*"+i+"="+i*j+"\t");
}
System.out.println();
}
}
}
(6)函数重载(Overload)
A:概念:函数名相同,参数列表不同(个数不同或对应类型不同)与返回值类型无关。
B:例子:
public static int sum(int a,int b){...}
public static int sum(int a,int b,int c){...}
public static int sum(float a,float b){...}
注意public static int sum(int a,int b){...}和public static int sum(int x,int y){...}是同一个函 数
2:数组
(1)概念:数组是存储同一类型的多个元素的容器。
(2)特点:数组中的元素都会被自动从0开始编号。
(3)格式:
A:int[] arr=new int[5];
B:int arr[]=new int[5];
C:int[] arr={1,2,3,4};
D:int[] arr=new int[]{1,2,3,4};
常用A和C
(4)数组的常见操作
A:数组的遍历
public class ArrayDemo {
public static void main(String[] args) {
int[] arr={12,36,48,24,69,87};
for(int x=0;x<arr.length;x++){
System.out.println(arr[x]);
}
}
}
B:获取最值
public class ArrayDemo2 {
public static void main(String[] args) {
int[] arr={67,32,14,45,78,96};
getMax(arr);
getMin(arr);
}
public static void getMax(int[] arr) {
int max=arr[0];
for(int x=0;x<arr.length;x++){
if(arr[x]>max){
max = arr[x];
}
}
System.out.println("最大值是:"+max);
}
<span style="white-space:pre"> </span>public static void getMin(int[] arr) {
int min=arr[0];
for(int x=0;x<arr.length;x++){
if(arr[x]<min){
min = arr[x];
}
}
System.out.println("最小值是:"+min);
}
}
(5)二维数组
<1>格式:
A:int[][] arr = new int[3][2];
B:int[][] arr = new int[3][];
C:int[][] arr = {{1,2,3},{4,5},{6,7,8,9}};
<2>常见操作:
arr.length 获取二维数组的长度
arr[x].length 获取每个一维数组的长度
arr[x][y] 获取二维数组中的元素
二维数组的遍历(代码如下)
class Array2Demo
{
public static void main(String[] args)
{
int[][] arr = {{3,2,7},{4,9},{5,6,8}};
for(int x =0;x<arr.length;x++)
{
for(int y=0;y<arr[x].length;y++)
{
System.out.print(arr[x][y]+"\t");
}
System.out.println();
}
}
}
(6)二维数组的应用:遍历求和。
某个商场,每个月都有销售额,每个季度也都有销售额,年终还得有销售额。单位(万元)
春 夏 秋 冬
月份 3 4 5 6 7 8 9 10 11 12 1 2
金额 10 9 8 16 23 32 67 112 223 123 2 0
代码如下:
class Array2Test
{
public static void main(String[] args)
{
//二维数组表示一下
int[][] arr = {{10,9,8},{16,23,32},{67,112,223},{123,2}};
//要求分别计算每个季度的销售额。
//第一季度:
int sum = 0;
for(int x=0; x<arr[0].length; x++)
{
sum += arr[0][x];
}
System.out.println("第一季度销售额:"+sum);
//第二季度:
int sum = 0;
for(int x=1;x<arr[1].length;x++)
{
sum+=arr[1][x];
}
//第三季度:
int sum = 0;
for(int x=2;x<arr[2].length;x++)
{
sum+=arr[1][x];
}
//第四季度:
int sum = 0;
for(int x=1;x<arr[3].length;x++)
{
sum+=arr[3][x];
}
//最终计算年终销售额。
int sum = 0;
for(int x=0; x<arr.length; x++)
{
for(int y=0; y<arr[x].length; y++)
{
sum += arr[x][y];
}
}
System.out.println("年终销售额:"+sum);//625
}
}