7.9作业

作业 day06

. 方法(函数)

1:函数的概念?函数的格式?格式的解释说明

  完成特定功能的代码块

修饰符 返回值类型 方法名(参数类型 参数名1,参数类型 参数名2...) {
            方法体语句;
            return 返回值; 

 public int sum (int a,int b) {

 int sum = a + b;

return sum ;

 

}

2:函数的调用
A:明确返回值类型的函数调用

单独调用,一般来说没有意义,所以不推荐。
输出调用,但是不够好。因为我们可能需要针对结果进行进一步的操作。
赋值调用,推荐方案。


B:void类型的函数调用
只能单独调用
3:函数的练习:
A:求两个数据之和
B:判断两个数据是否相等
C:获取两个数中较大的值

import java.util.Scanner;

class W_Add {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.println("请输入你选择的操作1-求和,2-判断相等,3-求最值");

int x = sc.nextInt();

 

int y = systemIn();

 

int z = systemIn();

switch (x) {

case 1: add(y,z);

break;

case 2: boolean c = equal(y,z);

System.out.println(c);

break;

case 3:int max = getMax(y,z);

System.out.println(max);

break;

default: System.out.println("你输入的数据有误");

 

}

}

 

public static int systemIn() {

Scanner sc = new Scanner(System.in);System.out.println("请输入数据 ");

int y =  sc.nextInt();

return y ;

}

 

 

public static void add(int a ,int b) {

int sum = a + b;

System.out.println(sum);

 

}

 

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

return a == b ? true: false;

 

 

}

 

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

return a > b ? a: b;

 

}

 

}


D:打印m行n列的星形矩形


E:打印nn乘法表

import java.util.Scanner;

class nnMul {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.println("请输入你选择的操作1-矩阵,2-nn乘法表");

int x = sc.nextInt();

 

 

switch (x) {

case 1:

System.out.println("请输入行数m");

int m = systemIn();

System.out.println("请输入列数n");

int n = systemIn();

star(m,n);

break;

case 2:

System.out.println("请输入n值");

int n2 = systemIn();

nnMul(n2);

break;

default: System.out.println("你输入的数据有误");

 

}

}

 

public static int systemIn() {

Scanner sc = new Scanner(System.in);System.out.println("请输入数据");

int y = sc.nextInt();

return y ;

}

 

 

public static void star(int m ,int n) {

for (int i = 0;i < m ;i++ ) {

for (int j = 0;j < n ;j++ ) {

System.out.print("*");

}

System.out.println();   

}

 

}

 

public static void nnMul(int n) {

/*1 * 1 = 1

  2 * 1 = 2 2 * 2 =3 */

 

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

for (int j = 1;j < i ;j++ ) {

System.out.print(i + "*" + j + "=" + i * j + "\t" );

} System.out.println();

}

 

 

}

 

}

 

4:什么是函数重载?以及函数重载的练习?把讲过的案例练习一次即可

在同一个类中,方法名相同,参数列表不同。与返回值类型无关。
    
    *参数列表不同:
        参数个数不同
        参数类型不同
        参数的顺序不同(算重载,但是在开发中不用)

practive

俩个整数的合与三个整数的合

import java.util.Scanner;

 

class Noname8 {

 

public static void main(String[] args) {

 

//俩个整数的合与三个整数的合

 

 

Scanner sc = new Scanner(System.in);

System.out.println("请输入你要对几个数进行加法运算");

int count = sc.nextInt();

 

if (count == 2) {

System.out.println("请输入一个整数");

int x = sc.nextInt();

 

System.out.println("请输入一个整数");

int y = sc.nextInt();

 

int sum1 = add(x , y);

System.out.println(sum1);

}

 

else {

 

System.out.println("请输入一个整数");

int q = sc.nextInt();

 

System.out.println("请输入一个整数");

int w = sc.nextInt();

 

System.out.println("请输入一个整数");

int e = sc.nextInt();int sum2 = add(q , w, e);

System.out.println(sum2);}

 

 

}

 

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

int sum = a + b;

return sum;

 

}

 

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

int sum = a + b + c;

return sum;

 

}

 

}

 

. 内存图

画图操作:


1.一个数组的内存图

2.两个数组的内存图

3.三个引用两个数组的内存图

 

=======================================
=======================================

.数组

1:数组的概念?有什么特点?

 数组是存储同一种数据类型多个元素的集合。也可以看成是一个容器。
    * 数组既可以存储基本数据类型,也可以存储引用数据类型。

2:一维数组的定义格式?

数据类型[] 数组名 = new 数据类型[数组的长度];

 1 int[] arr = new int[];

3:数组操作的两个小问题

 a:ArrayIndexOutOfBoundsException:数组索引越界异常

* 原因:你访问了不存在的索引。

* b:NullPointerException:空指针异常

* 原因:数组已经不在指向堆内存了。而你还用数组名去访问元素。

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

* arr = null;

* System.out.println(arr[0]);

 

 

4:数组常见操作:
数组遍历(依次输出数组中的每一个元素)
数组获取最值(获取数组中的最大值最小值)
数组元素逆序 (就是把元素对调)

class Reversal {

public static void main(String[] args) {

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

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

int temp = arr[i];

    arr[i] = arr[arr.length - 1 - i];

arr[arr.length - 1 - i] = temp;

System.out.println("反转数据为" + arr[i] + "\t" + arr[arr.length - 1 - i] );

}

}

}


数组查表法(根据键盘录入索引,查找对应星期)
数组元素查找(查找指定元素第一次在数组中出现的索引)

import java.util.Scanner;

class All_Show {

public static void main(String[] args) {

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

Show(arr);

getMax(arr);

getMin(arr);

search(arr,8);

week(arr);

 

}

/*数组遍历(依次输出数组中的每一个元素)

数组获取最值(获取数组中的最大值最小值)

数组元素逆序 (就是把元素对调)

数组查表法(根据键盘录入索引,查找对应星期)

数组元素查找(查找指定元素第一次在数组中出现的索引)*/

 

public static void Show(int[][] s) {

for (int i=0;i < s.length ;i++ ) {

for (int j = 0;j < s[i].length ;j++ ) {

System.out.println(s[i][j]);

}

System.out.println();

}

 

}

 

public static void getMax(int[][] m) {

int max = m[0][0];

for (int i = 0;i < m.length ;i++ ) {

for (int j = 0;j < m[i].length ;j++ ) {

if (max < m[i][j]) {

max = m[i][j];

}

 

}

 

}

System.out.println(max);

}

 

public static void getMin(int[][] m) {

int min = m[0][0];

for (int i = 0;i < m.length ;i++ ) {

for (int j = 0;j < m[i].length ;j++ ) {

if (min > m[i][j]) {

min = m[i][j];

}

 

}

 

}

System.out.println(min);

}

 

public static void search(int[][] s,int b) {

for (int i = 0;i < s.length ;i++ ) {

for (int j = 0;j < s[i].length ;j++ ) {

while (b == s[i][j]) {

System.out.print("数据的索引为" + i + "\t" + j );

break;

}

}

}

 

}

 

public static void week(int[][] w) {

Scanner sc = new Scanner(System.in);

System.out.println("输入二维数组第一个索引");

int a = sc.nextInt();

System.out.println("输入二维数组第二个索引");

int b = sc.nextInt();

 

switch (w[a][b]) {

case 1:

System.out.print("星期一");

break;

case 2:

System.out.print("星期二");

break;

case 3:

System.out.print("星期三");

break;

case 4:

System.out.print("星期四");

break;

case 5:

System.out.print("星期五");

break;

case 6:

System.out.print("星期六");

break;

case 7:

System.out.print("星期日");

break;

default: System.out.println("输入有误");

 

}

 

 

 

}

 

 

}


5:二维数组定义格式?

 int[][] arr = new int[3][2];

简体 int[][] arr ={{},{},{}}
看懂针对每种格式的内存图解?

6:案例
A:二维数组遍历

class T_Array {

public static void main(String[] args) {

int[][] arr = {{1,2},{30,40},{666}};

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

for (int j = 0;j < arr[i].length ;j++ ) {

System.out.println(arr[i][j]);

}

}

}

}

 


B:公司年销售额求和
某公司按照季度和月份统计的数据如下:单位(万元)
第一季度:22,66,44
第二季度:77,33,88
第三季度:25,45,65
第四季度:11,66,99

class  Add_Sell{

public static void main(String[] args) {

/*

B:公司年销售额求和

某公司按照季度和月份统计的数据如下:单位(万元)

第一季度:22,66,44

第二季度:77,33,88

第三季度:25,45,65

第四季度:11,66,99*/

int[][] arr = {{22,66,44},{77,33,88},{25,45,65},{11,66,99}};

int sum = 0;

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

for (int j = 0;j < arr[i].length ;j++ ) {

sum = sum + arr[i][j];

}

}System.out.println(sum);

 

}

 

}

 

7:参数传递问题

 基本数据类型的值传递,不改变原值,因为方法调用后就会弹栈,而局部变量随之消失

引用数据类型的值传递,改变原值,因为即使方法调用后弹栈,但是堆内存中的数组对象还在,可以通过地址(引用)继续访问.

猜你喜欢

转载自www.cnblogs.com/lijuenyi/p/9284208.html
今日推荐