方法:特定功能的代码块 方法定义格式:
格式解释:
方法明确:
方法调用:(有明确返回值的调用)
方法调用:(void修饰的方法)
求和方法
package com.data.it;
/**
* Created by JackFeng on 2020/3/1.
*/
public class SumTest {
public static void main(String[] args) {
//调用sum
// 单独调用 无意义
// sum(10, 20);
// 输出调用,有意义,不完美
// System.out.println(sum(10,20));
// 赋值调用,常用操作。
int i = sum(10, 20);
System.out.println(i);
System.out.println(i+1);
// 对i 进行操作
int j = sum(10,20)+10;
System.out.println(j);
}
/*
* 两个明确:
* 返回值:int
* 参数列表:int a , int b
*
*
* */
public static int sum(int a , int b ){
int c = a + b;
return c;
}
}
键盘录入两个数据判断是否相等
package com.data.it;
import java.util.Scanner;
/**
* Created by JackFeng on 2020/3/1.
*/
public class MethodTest {
public static void main(String[] args) {
// 创建键盘录入
Scanner sc = new Scanner(System.in);
// 接收数据
System.out.println("请输入第一个数据:");
int x = sc.nextInt();
System.out.println("请输入第二个数据:");
int y = sc.nextInt();
// 调用方法
boolean b = bijiao(x, y);
System.out.println(b);
}
/*
* 求两个值是否相等:
* 返回值:boolean
* 参数列表:int a,int b
*
* */
public static boolean bijiao(int a , int b ){
if (a==b){
return true;
}else {
return false;
}
}
}
水仙花数
package com.data.it;
/**
* Created by JackFeng on 2020/3/1.
*/
public class MethodTest1 {
public static void main(String[] args) {
//调用方法
printFlower();
}
/*
* 水仙花数打印到控制台方法
*
* 两个参数:
* 返回值类型:void
* 参数列表:无参数
*
* */
public static void printFlower(){
for (int i = 100; i <1000 ; i++) {
int g = i%10;
int s = i/10%10;
int b = i/10/10%10;
if (g*g*g + s*s*s + b*b*b == i){
System.out.println(i);
}
}
}
}
方法重载:在同一个类中,出现了方法名相同 不能出现参数名以及参数条件一致的方法 特点:
注意:在调用方法的时候,java虚拟机会通过参数列表的不同来区分同名的方法。
比较两个数据是否相等
package com.data.it;
/**
* Created by JackFeng on 2020/3/1.
*/
public class MethodTest2 {
public static void main(String[] args) {
System.out.println(compare(10,10));
System.out.println(compare(10L,10L));
System.out.println(compare((byte) 10,(byte)20));
System.out.println(compare((short) 10,(short) 20));
}
//byte
public static boolean compare(byte a , byte b){
System.out.println("byte");
return a == b;
}
//short
public static boolean compare(short a , short b){
System.out.println("short");
return a == b;
}
//int
public static boolean compare(int a , int b){
System.out.println("int");
return a == b;
}
//long
public static boolean compare(long a , long b){
System.out.println("long");
return a == b;
}
}
如果方法的参数是基本数据类型:形式参数的改变不影响实际参数 如果方法的参数是引用数据类型:形式参数的改变直接影响实际参数
数组遍历
package com.data.it;
/**
* Created by JackFeng on 2020/3/1.
*/
public class ArrayDo {
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4, 5};
System.out.println("第一种方式");
printArray(arr);
System.out.println("第二种方式");
printArray1(arr);
}
/*
* 两个明确:
* 返回类型:void
* 参数列表:int[] arr
* */
public static void printArray(int[] arr){
for (int i = 0; i <arr.length ; i++) {
System.out.println(arr[i]);
}
}
public static void printArray1(int[] arr){
// 输出为这样的 [1,2,3,4,5]
System.out.print("[");
for (int i = 0; i <arr.length ; i++) {
if (i==arr.length-1){
System.out.println(arr[i]+"]");
}else {
System.out.print(arr[i]+", ");
}
}
}
}