Java基础
1.循环控制语句: break、continue
public static void main ( String [ ] args) { int sum = 0 ; for ( int i = 1 ; i <= 1000 ; i++ ) { sum += i; if ( i >= 100 ) { break ; } } System . out. println ( "sum:" + sum) ; sum = 0 ; int num = 1 ; while ( true ) { if ( num > 100 ) { break ; } sum += num; num += 1 ; } System . out. println ( "sum:" + sum) ; int sum = 0 ; for ( int i = 1 ; i <= 100 ; i++ ) { if ( i % 2 == 1 ) { continue ; } sum += i; } System . out. println ( "sum:" + sum) ; wd: for ( int i = 0 ; i < 5 ; i++ ) { for ( int j = 0 ; j <= i ; j++ ) { System . out. print ( "*\t" ) ; if ( i == 3 && j == 2 ) { break wd; } } System . out. println ( ) ; } System . out. println ( "=============" ) ; for ( int i = 0 ; i < 5 ; i++ ) { for ( int j = 0 ; j <= i ; j++ ) { if ( j == 2 ) { continue ; } System . out. print ( "*\t" ) ; } System . out. println ( ) ; } }
2.方法/函数
public static void main ( String [ ] args) { printFun ( ) ; int funRes = addFun ( 4 , ( short ) 5 ) ; System . out. println ( "funRes:" + funRes) ; String str = strAddFun ( 3 , ( short ) 4 ) ; System . out. println ( str) ; int a = 3 ; short b = 4 ; String s = strAddFun ( a, b) ;
} public static void printFun ( ) { System . out. println ( "这是一个无参无返回值的函数" ) ; } public static int addFun ( int a, short b) { return a + b; } public static String strAddFun ( int a, short b) { return a + b + "" ; }
3.方法重载
public static void main ( String [ ] args) { System . out. println ( add ( 3 ) ) ; System . out. println ( add ( 2 , 3 ) ) ; } public static int add ( int a) { return a+ a; } public static int add ( int a, int b) { return a+ b; }
4.递归
public static void main ( String [ ] args) { System . out. println ( reloadFun ( 5 ) ) ; reloadFunLong ( 10 ) ; }
public static int reloadFun ( int n) { System . out. println ( "接收到N:" + n) ; if ( n == 1 ) { return 1 ; } else { return n * reloadFun ( n- 1 ) ; }
}
5.一维数组
public static void main ( String [ ] args) {
String [ ] employName = new String [ 3 ] ; employName = new String [ 4 ] ; employName[ 0 ] = "李白" ; employName[ 1 ] = "杜甫" ; employName[ 2 ] = "白居易" ;
employName[ 3 ] = "纳兰性德" ; System . out. println ( employName) ; System . out. println ( employName[ 0 ] ) ; System . out. println ( employName[ 1 ] ) ; System . out. println ( employName[ 2 ] ) ; System . out. println ( employName[ 3 ] ) ; String [ ] names1 = new String [ 3 ] ; String [ ] names2 = new String [ 2 ] ; names1[ 0 ] = "唐三" ; names1[ 1 ] = "小舞" ; names2[ 0 ] = "大明" ; names1 = names2; System . out. println ( names1[ 0 ] ) ; names1[ 1 ] = "唐三" ; System . out. println ( names2[ 1 ] ) ; }
6.数组的遍历、最值、逆序
public static void main ( String [ ] args) { int numArr[ ] = { 1 , 2 , 3 , 3 , 2 , 1 , 4 , 5 , 6 } ; for ( int i = 0 ; i < numArr. length; i++ ) { System . out. println ( "获取到数据:" + numArr[ i] ) ; } System . out. println ( "+++++++++++分割+++++++++++" ) ; int index = 0 ; while ( index < numArr. length) { System . out. println ( "获取到数据:" + numArr[ index] ) ; index++ ; } System . out. println ( "+++++++++++分割+++++++++++" ) ; for ( int elem : numArr) { System . out. println ( "获取到数据:" + elem) ; } int max = numArr[ 0 ] ; int min = numArr[ 0 ] ; for ( int elem : numArr) { max = elem > max ? elem : max; min = elem < min ? elem : min; } System . out. println ( "max:" + max) ; System . out. println ( "min:" + min) ; for ( int i = 0 ; i < numArr. length / 2 ; i++ ) { int tmpNum = numArr[ i] ; numArr[ i] = numArr[ numArr. length - i - 1 ] ; numArr[ numArr. length - i - 1 ] = tmpNum; } System . out. println ( "====================分割====================" ) ; for ( int elem : numArr) { System . out. println ( "获取到数据:" + elem) ; } }
7.二维数组
public static void main ( String [ ] args) { String [ ] [ ] allClassStu = new String [ 30 ] [ 40 ] ; String [ ] oneClassStu = allClassStu[ 1 ] ; oneClassStu[ 0 ] = "张三" ; oneClassStu[ 1 ] = "李四" ; System . out. println ( oneClassStu[ 0 ] ) ; System . out. println ( oneClassStu[ 1 ] ) ; System . out. println ( allClassStu[ 1 ] [ 0 ] ) ; String [ ] [ ] allClassStu2 = new String [ 30 ] [ ] ; String [ ] oneClassStu2 = allClassStu2[ 29 ] ;
allClassStu2[ 29 ] = new String [ 22 ] ; allClassStu2[ 29 ] [ 0 ] = "邱六" ; System . out. println ( allClassStu2[ 29 ] [ 0 ] ) ; int [ ] [ ] allInt = { { 1 , 2 , 4 } , { 2 , 3 , 4 } , { 2 , 3 , 4 } } ; for ( int i = 0 ; i < allInt. length; i++ ) { for ( int index = 0 ; index < allInt[ i] . length; index++ ) { System . out. println ( allInt[ i] [ index] ) ; } } int [ ] [ ] allSalary = { { 22 , 66 , 44 } , { 77 , 33 , 88 } , { 25 , 45 , 65 } , { 11 , 66 , 99 } } ; int sum = 0 ; for ( int [ ] oneArr : allSalary) { for ( int elem : oneArr) { sum += elem; } } System . out. println ( "sum:" + sum) ; }