基本概念
class point{
//java的函数必须定义到 类 里面
//有两种函数
//普通的public函数
public int f(){
return 0 ;
}
//加static的函数 类似于全局函数( 加static的变量类似于全局变量)
//加static相当于绑定到这个类本身
public static int g(){
return 0;
}
}
类是static,函数也必须是static
输出最大值
package test1;
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
System.out.println(max(a,b));
}
public static int max(int a,int b){
return Math.max(a,b);
}
}
为二维数组赋初值
package test1;
import java.util.Arrays;
public class Main{
public static void main(String[] args) {
int[][] a = new int[5][5];
fill(a, 3);
System.out.println(Arrays.deepToString(a));
}
private static void fill ( int[][] a, int val) {
for (int i = 0; i < a.length; i++)
for (int j = 0; j < a[i].length; j++) a[i][j] = val;
}
}
初始二维数组的行,列,值
package test1;
import java.util.Arrays;
public class Main{
public static void main(String[] args) {
int [][] b = getArray2d(2,3,6);
System.out.println(Arrays.deepToString(b));
}
private static int[][] getArray2d(int col,int row,int val){
int [][]a = new int[col][row];
for (int i = 0; i < col; i++) {
for (int j = 0; j < row; j++) {
a[i][j] = val;
}
}
return a;
}
}
package test1;
//相当于定义了全局变量
class Argument{
public static int x = 1;
public final static int y = 1;
}
public class Main{
public static void main(String[] args) {
System.out.println(Argument.x);//静态变量访问 用类名 普通变量和普通函数 用具体的实例访问
}
}