一、方法定义
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;
}
// 方法:循环输出1-1000之间能被5整除的数,并且每行输出3个。
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();
}
}
}
}
public class Demo02 {
public static void main(String[] args) {
int max = max(10,10); // 实际参数:实际调用时传递给方法的参数
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;
}
}
二、方法的调用与重载
变量名.方法名(实参列表)
Java中只有值传递
public class Demo01 {
public static void main(String[] args) {
int sum = add(1,2); // 方法调用
System.out.println(sum); // 方法调用。println方法返回值为空,调用的是一条语句。
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();
}
}
}
}
public class Demo002 {
public static void main(String[] args) {
int max = max(10,10);
System.out.println(max);
}
// 方法重载:方法名相同,但是参数列表不同(个数不同、或类型不同、或参数排序不同等)。
// 【JVM在方法调用时会根据实参的参数类型来选择相应类型的形参,所对应的方法】
public static int max (double num1,double num2){
int result = 0;
if (num1 == num2){
System.out.println("num1 == num2");
return 0; // 终止方法
}
if (num1 > num2){
result = (int)num1;
}else {
result = (int)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;
}
public static int max (int num1,int num2,int num3){
int result = 0;
if (num1 == num2){
System.out.println("num1 == num2");
return 0;
}
if (num1 > num2){
result = (int)num1;
}else {
result = (int)num2;
}
return result;
}
}
三、扩展:命令行传参
扩展:
下面以对com.kuang.method包下的Demo03类传入字符串类型的数组元素为例:
以下是Demo03的代码:
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]);
}
}
}
以下是对Demo03这个类的命令行传参的实操:
四、可变参数
可变参数就是再不确定实参会输入多少个相同类型的数据时,在方法的形参定义时使用省略号。
public class Demo04 {
public static void main(String[] args) {
Demo04 demo04 = new Demo04();
demo04.test(1,2,3,4,5,6,7);
}
public void test(int x, int...i){
System.out.println(i[0]);
System.out.println(i[1]);
System.out.println(i[2]);
System.out.println(i[3]);
}
}
在Java中,可变参数的方法本质上是用数组来实现的。
使用:类型…参数名(如:int…nums),编译器会将其转换为数组。
public class Demo05 {
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){ // 不传参数时,可变参数会接收一个长度为0的数组(并非null)
System.out.println("No argument paased");
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);
}
}
如果同时定义了可变参数方法和具体参数数量的重载方法,那么在JVM选择方法调用时,具体方法优先匹配。
五、递归
下面是阶乘的递归:
public class Demo07 {
public static void main(String[] args) {
/* 阶乘
2! 2*1
3! 3*2*1
4! 4*3*2*1
5! 5*4*3*2*1
*/
System.out.println(f(5));
/*
递归计算 f(4) 的完整流程:
1. 递推阶段(调用栈展开):
f(4) = 4 * f(3)
f(3) = 3 * f(2)
f(2) = 2 * f(1)
f(1) = 1 // 递归终止条件
2. 回归阶段(结果返回):
f(2) = 2 * 1 = 2
f(3) = 3 * 2 = 6
f(4) = 4 * 6 = 24
最终结果:24
*/
}
public static int f(int n){
if (n == 1){
return 1;
}else {
return n*f(n - 1);
}
}
}
下面是递归反例,是一个死循环,直到把栈占满然后报错。
public class Demo06 {
public static void main(String[] args) {
Demo06 test = new Demo06();
test.test();
}
public void test(){
test();
}
}
六、练习题
题目:
代码如下:
package com.kuang.method;
import java.util.Scanner;
public class test {
public static void main(String[] args) {
for (boolean judge = true;judge;){
System.out.println("请分别输入您的操作数和运算符,以回车键完成一个输入,总计输入三次。先输入两个操作数,再输入运算符。");
Scanner inputNum1 = new Scanner(System.in);
double num1 = inputNum1.nextDouble(); // Scanner类的nextDouble()方法的输入格式与next()相同,只是输入数据类型不同。
Scanner inputNum2 = new Scanner(System.in);
double num2 = inputNum1.nextDouble();
Scanner operator = new Scanner(System.in);
String symbol = operator.next(); // next()不会获取空白,只会获取输入的第一串有效字符串。空格后,后面输入的不会被记入。nextLine()会获取空白。
double result = 0;
switch (symbol){
case "+":
result = add(num1, num2);
System.out.println("您输入的计算结果为:" + num1 + symbol + num2 + "=" + result);
break;
case "-":
result = minus(num1, num2);
System.out.println("您输入的计算结果为:" + num1 + symbol + num2 + "=" + result);
break;
case "*":
result = ride(num1, num2);
System.out.println("您输入的计算结果为:" + num1 + symbol + num2 + "=" + result);
break;
case "/":
result = getRidOf(num1, num2);
System.out.println("您输入的计算结果为:" + num1 + symbol + num2 + "=" + result);
break;
default:
System.out.println("您输入的运算符号不合法!");
}
System.out.println("请问您是否要继续输入,请回答ture或false,以回车确认输入,输入不可有空格。");
Scanner pause = new Scanner(System.in);
judge = pause.nextBoolean();
}
System.out.println("程序结束!!");
}
public static double add(double a ,double b){
double sum = a + b;
return sum;
}
public static double minus(double a ,double b){
double differFrom = a - b;
return differFrom;
}
public static double ride(double a ,double b){
double amass = a * b;
return amass;
}
public static double getRidOf(double a,double b){
double discuss = a / b;
return discuss;
}
}