何谓方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package com.wang.method;

public class Demo01 {
//main方法
public static void main(String[] args) {
// int sum = add(1,2);
// System.out.println(sum);

test();
}
//加法
public static int add(int a,int b){
return a+b;
}
public static void test(){
for (int i = 0; i <= 1000; i++) {
if(i%5==0){
System.out.print(i+"\t");
}
if (i%(5*3)==0) {//每行
System.out.println("\n");
}
}
}
}

方法的定义

方法调用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package com.wang.method;

public class Demo02 {
public static void main(String[] args) {
int max = max(10,20);
System.out.println(max);
}
//比大小
public static int max(int num1,int num2){
int result = 0;

if(num1==num2){
System.out.println("num1==num2");
return 0;//终止方法
}

if (num1>num2){
result = num1;
}else {
result = num2;
}
return result;
}
}

方法的重载

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package com.wang.method;

public class Demo02 {
public static void main(String[] args) {
double max = max(10,20);
System.out.println(max);
}
//比大小
public static double max(double num1,int num2){
double result = 0;

if(num1==num2){
System.out.println("num1==num2");
return 0;//终止方法
}

if (num1>num2){
result = num1;
}else {
result = num2;
}
return result;
}
//比大小
public static int max(int num1,int num2){
int result = 0;

if(num1==num2){
System.out.println("num1==num2");
return 0;//终止方法
}

if (num1>num2){
result = num1;
}else {
result = num2;
}
return result;
}
}

命令行传参

1
2
3
4
5
6
7
8
9
10
11
package com.wang.method;

public class Demo03 {
public static void main(String[] args) {
//args.length 数组长度
for (int i = 0;i < args.length;i++){
System.out.println("args[" + i +"]:" +args[i]);

}
}
}

windows终端–回退到src–输入 java com.wang.method.Demo03 (输入参数,比如:this is wang)

可变参数or不定项参数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package com.wang.method;

public class Demo04 {
public static void main(String[] args) {
//调用可变参数的方法
printMax(34,3,3,2,56.5);
printMax(new double[]{1,2,3});
}
public static void printMax(double...numbers) {
if (numbers.length == 0){
System.out.println("No argument passed");
return;
}

double result = numbers[0];

//排序!
for (int i = 1;i < numbers.length; i++){
if (numbers[i] > result){
result = numbers[i];
}
}
System.out.println("The max value is " + result);
}
}

递归

1
2
3
4
5
6
7
8
9
10
11
12
13
14
package com.wang.method;

public class Demo05 {
public static void main(String[] args) {
System.out.println(f(4));
}
public static int f(int n){
if (n==1){
return 1;
}else{
return n*f(n-1);
}
}
}

边界条件:边界

前阶段:

返回阶段: n*(n-1)

小计算用递归,大计算不用

作业

写一个计算器,要求实现加减乘除功能,并且能够接受新数据,通过用户交互实现

要求:

1.写四个方法:加减乘除

2.利用循环+switch进行用户交互

3.传递需要操作的两个数

4.输出结果

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package com.wang.method;

import java.util.Scanner;

public class Demo06 {
public static void main(String[]args){
Scanner scanner=new Scanner(System.in);
int flag=0;
while(flag==0){
System.out.println("输入第一个数:");
double a=scanner.nextDouble();

System.out.println("输入运算符:");
String str=scanner.next();

System.out.println("输入第二个数:");
double b=scanner.nextDouble();

//System.out.println(a+" "+b+" "+str);
double result=0;

switch (str){
case "+":
result=add(a,b);
break;
case "-":
result=minus(a,b);
break;
case "*":
result=multiply(a,b);
break;
case "/":
result=divide(a,b);
break;
default:
System.out.println("Operation error!Input again!");
}
System.out.println(result);
System.out.println("是否继续?(请输入Y或者N)");
String ifContinue=scanner.next();
if(ifContinue.equals("Y")) {
flag = 0;
}else {
flag =1;
scanner.close();
}
}
}

public static double add(double a,double b) {
return a+b;
}
public static double divide(double a,double b){
return a/b;
}
public static double minus(double a,double b) {
return a - b;
}
public static double multiply(double a,double b){
return a*b;
}
}