修饰符 返回值类型 方法名称(参数类型1 参数名称1,参数类型2,参数名称2){
过程体;
return 返回值;
}
书写一个add()方法
package Method;
import java.util.Scanner;
public class Demo1 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int x=scanner.nextInt();
int y=scanner.nextInt();
int sum = add(x,y);
int temp1=3;
int temp2=5;
swap(temp1,temp2);
System.out.println(temp1);
System.out.println(temp2);
System.out.println("sum is:"+sum);
}
public static int add(int x, int y) {
return x + y;
}
public static void swap(int x,int y){//不能实现交换
int temp;
temp=x;
x=y;
y=temp;
}
}
6
9
3
5
sum is:15
public static int add(int x, int y) {
return x + y;
}
public static int add(int x,int y,int z){
return x+y+z;
}
通过命令行传参
package Method;
public class Demo2 {
public static void main(String[] args) {
for(int i=0;i< args.length;++i){
System.out.println("args["+i+"]"+args[i]);
}
}
}
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-lscXh5ou-1645418248045)(C:\Users\92088\Pictures\2.png)]
int[] arrays={1,2,3,4,5};
for (int i = 0; i
for (int array : arrays) {
System.out.println(array);
}
public static void printArray(int[] arrays) {
for (int i = 0; i <arrays.length ; i++) {
System.out.println(arrays[i]+" ");
}
printArray(arrays);
写一个函数实现数组内容翻转,返回一个数组
public static int[] reverse(int[] arrays){
int[] result = new int[arrays.length];
for (int i = 0,j=arrays.length-1; i
高维数组
int[][] array = {{1,2},{2,3},{3,4},{4,5}};
for (int i = 0; i < array.length ; i++) {
System.out.println("=====");
for (int j = 0; j < array[i].length; j++) {
System.out.println(array[i][j]);
}
}
Arrays方法
导入代码:import java.util.Arrays;
package Arrays;
import java.util.Arrays;
public class Demo3 {
public static void main(String[] args) {
int[] array={12,233,2,1,99};
// Arrays.sort(array);//顺序排序
Arrays.fill(array,2,4,0);//数组从下标2到3(4-1)的元素被替换为0
System.out.println(Arrays.toString(array));//输出方法toString
}
}