Java基礎(chǔ)練習(xí)及代碼_第1頁
Java基礎(chǔ)練習(xí)及代碼_第2頁
Java基礎(chǔ)練習(xí)及代碼_第3頁
Java基礎(chǔ)練習(xí)及代碼_第4頁
Java基礎(chǔ)練習(xí)及代碼_第5頁
已閱讀5頁,還剩94頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)

文檔簡介

1、只供學(xué)習(xí)與交流資料收集于網(wǎng)絡(luò),如有侵權(quán) 請聯(lián)系網(wǎng)站刪除Java 練習(xí)及作業(yè)代碼集資料收集于網(wǎng)絡(luò),如有侵權(quán) 請聯(lián)系網(wǎng)站刪除目錄一、 第一章 HYPERLINK l _bookmark1 31. 制作九九乘法表 HYPERLINK l _bookmark2 32. 從鍵盤輸入 HYPERLINK l _bookmark3 33. 判斷一個數(shù)是否為素數(shù) HYPERLINK l _bookmark4 34. 把數(shù)字倒著輸出 HYPERLINK l _bookmark5 45. 水仙花數(shù) HYPERLINK l _bookmark6 46. 判斷年份是否為閏年 . HYPERLINK l _bookma

2、rk7 57. 輸入三邊求三角形面積 HYPERLINK l _bookmark8 58. 一位數(shù)加法問答 HYPERLINK l _bookmark9 59. 空格和星星 HYPERLINK l _bookmark10 610. Swich 語句控制抽牌 . HYPERLINK l _bookmark11 711. 1000 以內(nèi)的完數(shù) HYPERLINK l _bookmark12 712. 形如 3+33+333+3333 單雙重循環(huán)練習(xí) HYPERLINK l _bookmark13 813. 求一組隨機數(shù)最大值 . HYPERLINK l _bookmark14 914. 隨機產(chǎn)生十

3、五個不重復(fù)的學(xué)號 HYPERLINK l _bookmark15 915. 插入排序 HYPERLINK l _bookmark16 1016. 選擇排序 HYPERLINK l _bookmark17 1 117. 從前往后進行冒泡排序 HYPERLINK l _bookmark18 1218. 從后往前進行冒泡排序 HYPERLINK l _bookmark19 1219. 冒泡排序(可提前結(jié)束排序) HYPERLINK l _bookmark20 1320. 二維數(shù)組輸出練習(xí) . HYPERLINK l _bookmark21 1421. 上三角和下三角練習(xí) . HYPERLINK l

4、_bookmark22 1522. 偶數(shù)列小到大輸出,奇數(shù)列大到小輸出 HYPERLINK l _bookmark23 1623. 數(shù)組方式隨機抽取 4 張牌 HYPERLINK l _bookmark24 1724. 二維數(shù)組輸出類似楊輝三角 HYPERLINK l _bookmark25 1725. 兩個矩陣相乘 HYPERLINK l _bookmark26 1826. 函數(shù)調(diào)用練習(xí) HYPERLINK l _bookmark27 1927. 鞍點( 1) HYPERLINK l _bookmark28 2028. 鞍點( 2) . HYPERLINK l _bookmark29 2 1

5、29. 打印電話號碼 HYPERLINK l _bookmark30 22二、 第二章 HYPERLINK l _bookmark31 221. 類,對象和構(gòu)造方法 . HYPERLINK l _bookmark32 222. 普通方法的重構(gòu) HYPERLINK l _bookmark33 233. 類的繼承 HYPERLINK l _bookmark34 244. 抽象類和抽象方法 . HYPERLINK l _bookmark35 255. 接口 . HYPERLINK l _bookmark36 266. 二分查找 HYPERLINK l _bookmark37 287. 全排列(遞歸)

6、 HYPERLINK l _bookmark38 29只供學(xué)習(xí)與交流資料收集于網(wǎng)絡(luò),如有侵權(quán) 請聯(lián)系網(wǎng)站刪除一、1. 制作九九乘法表public static void main(String / 九九乘法表 for ( int for ( intr =1; r =9; r +) c =1; c=1; r -)for ( int c =1; c=r ; c +)System. out .print( r +* +c+= +r * c+t );System. out .println();2. 從鍵盤輸入import java.util.*;public class MyInport publi

7、c static void main(String Scanner sc = new Scanner(S i =sc .nextInt();float f =sc .nextFloat();double d=sc .nextDouble();System. out .println( i= +i );System. out .println( f= +f );System. out .println( d= +d );3. 判斷一個數(shù)是否為素數(shù)package test;import java.util.*;public class SushuTest public stati

8、c void main(Stringargs )in );args ) Scanner sc = new Scanner(System. in );int Ss = sc .nextInt();boolean s = true ;for ( int i = 2; i 0)System. out .print( sr %10);sr =sr /10;5. 水仙花數(shù)package test;public class SXHS public static void main(String args ) / TODOAuto-generated method stubSystem. out .prin

9、t( 水仙花數(shù)有 : );for ( int sr = 100; sr c & a + c b & b + c a) double Zc_half = ( a + b + c) / 2;double Area = Math. sqrt ( Zc_half * ( Zc_half - a) * ( Zc_half - b )* ( Zc_half - c );System. out .print( 三角形的面積為 : + Area );else System. out .print( 無法構(gòu)成三角形 ); 8. 一位數(shù)加法問答import java.util.*;public class YWS

10、JF public int intstatic void main(String args ) number1 =( int )(Math. random ()*10);number2 =( int )(Math. random ()*10);Scanner input = new Scanner(System. in );只供學(xué)習(xí)與交流資料收集于網(wǎng)絡(luò),如有侵權(quán) 請聯(lián)系網(wǎng)站刪除System. out .print( number1 + +number2 += );int answer =input .nextInt();if ( answer =number1 +number2 )System

11、. out .println( 恭喜你,答對了! );elseSystem. out .println( 很遺憾,答錯了! );System. out .println( number1 + +number2 += +( number1 +number2 );9. 空格和星星package mine;public class PrintStar public static int t =9; for ( intvoidi = 1;main(String args ) i = t ; i +) for ( int j = 1; j = t - System. out .print(for ( i

12、nt k = 1; k = 2 *i ; j +) );i - 1; k +) System. out .print( * );System. out .println();for ( int i =1; i t ; i +)for ( int j =1; j =2* i -1; k -)System. out .print( * );System. out .println();只供學(xué)習(xí)與交流資料收集于網(wǎng)絡(luò),如有侵權(quán) 請聯(lián)系網(wǎng)站刪除10. Swich 語句控制抽牌package mine;publicclass a public static int cardvoid main(String

13、 args ) = ( int ) (Math. random () * 54);String suit = , rank = ;switch ( card / 13) case 0:suit = 黑桃 ;break ; case 1:suit =break ; case 2:suit =break ; case 3: 紅桃 ; 方塊 ;suit = 梅花 ;break ;switch ( card %13)case 0: rank = break ;case 10: rank = break ;case 11: rank =break ; case 12:A ;J ;Q ;rank = K

14、;break ;default : rank = +( card %13+1);System. out .print( 你抽到的牌是 : +suit + +rank );11. 1000 以內(nèi)的完數(shù)package mine;只供學(xué)習(xí)與交流資料收集于網(wǎng)絡(luò),如有侵權(quán) 請聯(lián)系網(wǎng)站刪除public class WanShu public forstatic void main(String( int i = 1; i = 1000;int sum = 0;for ( int j = 1; j i ;if ( i % j = 0) sum = sum + j ;if ( sum =System. i )

15、 out .println(args ) i +) j +) i );12. 形如 3+33+333+3333 單雙重循環(huán)練習(xí)import java.util.*;public class Test public static void main(String args ) / 3+33+333System. out .println( please input a and n );Scanner sc = new Scanner(System. in );int a = sc .nextInt();int n = sc .nextInt();/ 一重循環(huán)int sum1 = 0; int t

16、erm1 = 0;for ( int i = 1; iterm1 = term1sum1 = sum1 + = n ; i +) * 10 + a ; term1 ;System. out .println( / 二重循環(huán) int sum2 = 0;for ( int i = 1; iint term2 = 0; 一重循環(huán)結(jié)果: + sum1);= n ; i +) 只供學(xué)習(xí)與交流資料收集于網(wǎng)絡(luò),如有侵權(quán) 請聯(lián)系網(wǎng)站刪除for ( int j = 1; j = iterm2 = term2 * 10 + sum2 = sum2 + term2 ; j +) a;System. out .pri

17、ntln( 雙重循環(huán)結(jié)果: + sum2);13. 求一組隨機數(shù)最大值package mine;import java.util.*;public class Test public static void main(String args ) Random rad =new Random();int arr =new int 10;for ( int i =0; i 10; i +)arr i = rad .nextInt(100);System. out .print( arr i + );System. out .println();int max=arr 0;for ( int i =

18、1; i 10; i +)if ( maxarr i )max=arr i ;System. out .println( the max is: +max);14. 隨機產(chǎn)生十五個不重復(fù)的學(xué)號package aaa;import java.util.*;public class Test public static void main(String args ) Random rad =new Random();int arr =new int 15; / 產(chǎn)生一個數(shù)組,把 n放入int k=0; / 數(shù)組元素的個數(shù)for ( int i =0; i 15; i +)int n=rad .ne

19、xtInt(47)+1; / 產(chǎn)生 1-47 個學(xué)號boolean b =true ;for ( int j = 0; j k ; j +)只供學(xué)習(xí)與交流資料收集于網(wǎng)絡(luò),如有侵權(quán) 請聯(lián)系網(wǎng)站刪除if ( n =arr j )b=false ;break ;if ( b =true )/ 數(shù)字有效無重復(fù),輸出并放入數(shù)組,個數(shù)加一System. out .println( n );arr k = n ;k+;else i -; / 數(shù)字重復(fù),則使用 i- 把此循環(huán)無效15. 插入排序import java.util.*;public class Test public static void ma

20、in(String args ) / 插入排序Random rand =new Random();int arr = new int 10; / 放入 10個元素int temp ; / 用于保存當前使用的某元素/ 生成 10 個隨機數(shù)for ( int i =0; i arr.length; i +)arr i = rand .nextInt(40); / 生成 0-39 內(nèi)的隨機數(shù)System. out .print( arr i + );System. out .println();/ 把數(shù)組分成已排序和未排序兩部分,進行插入排序for ( int i =1; i 0; j -) / 如

21、 20 1 13temp =arr j ; / 保存當前未排序的第一個元素if ( temp arr j -1)arr j = arr j -1;arr j -1= temp ;只供學(xué)習(xí)與交流資料收集于網(wǎng)絡(luò),如有侵權(quán) 請聯(lián)系網(wǎng)站刪除elsebreak ;arr j -1= temp ;System. out .println( 結(jié)果如下: );/ 輸出結(jié)果for ( int i =0; i arr.length; i +)System. out .print( arr i + );16. 選擇排序package aa;import java.util.Random;public class T

22、est public static void main(String / 選擇排序Random rand = new Random();int arr = int minIndexnew int 10; / ;args ) 放入 10個元素/ 生成 10 個隨機數(shù)for ( int i = 0; i arr.length;arr i = rand .nextInt(40);System. out .print( arr i +System. out .println();for ( int i = 0; i arr.length-1;minIndex = i ;i +) / 生成 0-39 內(nèi)

23、的隨機數(shù) );i +) / 內(nèi)循環(huán)找最小的數(shù)的位置for ( int j = i + 1; j = arr.length-1; j +) if ( arr j arr minIndex ) minIndex = j ;if ( minIndex != i ) int t = arr i ;arr i = arr minIndex ;arr minIndex = t ;只供學(xué)習(xí)與交流資料收集于網(wǎng)絡(luò),如有侵權(quán) 請聯(lián)系網(wǎng)站刪除System. out .println( 結(jié)果如下:/ 輸出結(jié)果for ( int i = 0; i arr.length;System. out .print( arr i

24、 +17. 從前往后進行冒泡排序package aa;import java.util.Random;public class Test public static void main(String / TODOAuto-generated method stub / 冒泡排序 );i +) );args ) Random rand = new Random();int arr = new int 10; / 放入 10個元素/ 生成 10 個隨機數(shù)for ( int i = 0; i arr . length ; i +) arr i = rand .nextInt(40); / 生成 0-

25、39 內(nèi)的隨機數(shù)System. out .print( arr i + );System. out .println();for ( int i = 0; i arr . length -1; i +) / 內(nèi)層循環(huán)控制第 i 次,每兩個元素,兩兩比較進行交換for ( int j =0; j arr j +1)int t =arr j ;arr j = arr j +1;arr j +1= t ;System. out .println( 結(jié)果如下: );/ 輸出結(jié)果for ( int i = 0; i arr . length ; i +) System. out .print( arr

26、i + );18. 從后往前進行冒泡排序只供學(xué)習(xí)與交流資料收集于網(wǎng)絡(luò),如有侵權(quán) 請聯(lián)系網(wǎng)站刪除import java.util.*;public class Test public static void main(String args ) / TODOAuto-generated method stub/ 從后往前冒泡排序Random rand = new Random();int arr = new int 10; / 放入 10個元素/ 生成 10 個隨機數(shù)for ( int iarr i =System. System. out for ( int i= 0; i 10; i +)

27、rand .nextInt(40);out .print( arr i +.println();= 0; i 0; j -) if ( arr j arr j - 1) int t = arr j ;arr j = arr j - 1;arr j - 1 = t ;System. out .println( 結(jié)果如下: );/ 輸出結(jié)果for ( int i = 0; i 10; i +) System. out .print( arr i + );19. 冒泡排序(可提前結(jié)束排序)import java.util.*;public class Test public static void

28、main(String args ) Random rand = new Random();int arr = new int 10; / 放入 10個元素/ 生成 10 個隨機數(shù)int arr1 = new int 10;for ( int i = 0; i arr . length ; i +) arr i = rand .nextInt(40);System. out .print( arr i +只供學(xué)習(xí)與交流/ 生成 0-39 內(nèi)的隨機數(shù) );資料收集于網(wǎng)絡(luò),如有侵權(quán) 請聯(lián)系網(wǎng)站刪除System. out .println();/ 冒泡排序for ( int i = 0; i arr

29、 . length - 1; i +) boolean flag = true ; / 假設(shè)已經(jīng)排好順序for ( int j = 0; j arr j + 1) / 若前后兩個元素對調(diào)則排序未完成,將 flag 設(shè)為falseint t = arr j ;arr j = arr j + 1;arr j + 1 = t ;flag = false ;/ 若排序提前完成跳出循環(huán)if ( flag ) System. out .println( 第 +( i +1)+ 輪時已經(jīng)完成排序,跳出循環(huán) );break ;/ 將每輪結(jié)果輸出System. out .println( 第 + ( i +1)

30、 + 輪排序 );for ( int u = 0; u arr . length ; u +) arr1 u = arr u;System. out .print( arr u + );System. out .println();System. out .println( 結(jié)果如下: );for ( int i = 0; i arr . length ; i +) System. out .print( arr i + );20. 二維數(shù)組輸出練習(xí)public class Test public static void main(String args) / 按每行,從小到大排int arr

31、= new int 44;int k=1;只供學(xué)習(xí)與交流資料收集于網(wǎng)絡(luò),如有侵權(quán) 請聯(lián)系網(wǎng)站刪除for ( int row=0;row4;row+)for ( int col=0;col4;col+)arrrowcol=k;System.out.print(arrrowcol+ t );k+;System.out.println();/ 按每列,從小到大排k=1; / 初始 kfor ( int col=0;col4;col+)for ( int row=0;row4;row+)arrrowcol=k;k+;System.out.println();for ( int for row=0;ro

32、w4;row+)( int col=0;col4;col+)System.out.print(arrrowcol+ t );System.out.println();21. 上三角和下三角練習(xí)public class Test2 public / int int for static void main(String args ) 帶0輸出的數(shù)組 arr =new int 44;k =1;( int row =0; row 4; row +)for ( int col =0; col 4; col +)只供學(xué)習(xí)與交流資料收集于網(wǎng)絡(luò),如有侵權(quán) 請聯(lián)系網(wǎng)站刪除if ( row = col ) /r

33、ow=col 上三角arr row col = k+;elsearr row col =0;System. out .print( arr row col + t );System. out .println();/ 不帶 0 輸出的上三角/*int arr=new int44;int k=1;for(int row=0;row4;row+)for(int col=0;colrow+1;col+)arrrowcol=k;System.out.print(arrrowcol+t);k+;System.out.println(); */22. 偶數(shù)列小到大輸出,奇數(shù)列大到小輸出 public cl

34、ass Test2 public static void main(String args ) / TODOAuto-generated method stubint arr = new int 55;int k = 1;for ( int col = 0; col 5; col +) if ( col % 2 = 0) for ( int row = 0; row = 0; row -) arr row col = k+;只供學(xué)習(xí)與交流資料收集于網(wǎng)絡(luò),如有侵權(quán) 請聯(lián)系網(wǎng)站刪除for ( int row = 0; row 5; row +) for ( int col = 0; col 5;

35、col +) System. out .print( arr row col + t );System. out .println();23. 數(shù)組方式隨機抽取 4 張牌public class Test2 public static void main(String args ) int deck =new int 52;String suits = 黑桃 , 紅桃 , 方塊 , 梅花 ;String ranks = A , 2 , 3 , 4 , 5 , 6 , 7 , 8 ,9 , 10 , J , Q , K ;/ 初始化每一張牌for ( int i =0; i deck . len

36、gth deck i = i ;/ 打亂牌的次序for ( int i =0; i deck . length; i +); i +)/ 隨機產(chǎn)生一個元素下標 int index =( int )(Math. int temp =deck i ; deck i = deck index ; deck index = temp ;/ 顯示輸出前 4張牌051random ()* deck . length );for ( int i =0; i 4; i +)String suit = suits deck i /13;String rank =ranks deck i %13;System.

37、out .println( suit + +rank );24. 二維數(shù)組輸出類似楊輝三角public class Test2 public static void main(String args ) int arr = new int 1010;for ( int i =0; i 10; i +)只供學(xué)習(xí)與交流資料收集于網(wǎng)絡(luò),如有侵權(quán) 請聯(lián)系網(wǎng)站刪除for ( int j =0; j =i ; j +)if ( j =0| j =i )arr i j =1; / 開頭和末尾為 1elsearr i j = arr i -1 j -1+ arr i -1 j ; / 中間數(shù)值規(guī)律for (

38、int i =0; i 10; i +)for ( int j =0; j 10; j +)System. out .print( arr i j + t );System. out .println();25. 兩個矩陣相乘import java.util.*;public class Test public static void main(String args ) Random rand = new Random();/ 生成 2 個矩陣并隨機賦值Scanner sc = new Scanner(System. in );int arr1 = new int 25;int arr2 =

39、 new int 53;for ( int r = 0; r 2; r +) for ( int c = 0; c 5; c+) arr1 r c = rand .nextInt(5);System. out .print( arr1 r c + );System. out .println();System. out .println();for ( int r = 0; r 5; r +) for ( int c = 0; c 3; c+) arr2 r c = rand .nextInt(5);System. out .print( arr2 r c + );System. out .p

40、rintln();System. out .println();/ 相乘規(guī)則只供學(xué)習(xí)與交流資料收集于網(wǎng)絡(luò),如有侵權(quán) 請聯(lián)系網(wǎng)站刪除int arr3 = new int 23; / 創(chuàng)建一個 0矩陣等待賦值for ( int r = 0; r 2; r +) for ( int c = 0; c 3; c+) int sum = 0;for ( int u = 0; u 5; u+) sum = sum + arr1 r u * arr2 u c; / 矩陣第 r 行,第 c列每個元素相乘再相加/ 將sum賦值于 arr3 的r 行c列位置arr3 r c = sum;System. out .

41、println( 結(jié)果如下: );for ( int i = 0; i 2; i +) for ( int j = 0; j 3; j +) System. out .print( arr3 i j + t );System. out .println();26. 函數(shù)調(diào)用練習(xí)import java.util.Random;public class Test int arr =new int 55;public void Intt()Random rand =new Random();for ( int r =0; r 5; r +)for ( int c =0; c5; c +)arr r

42、c = rand .nextInt(20);System. out .print( arr r c+ t ); publicSystem. out .println();static void main(String args ) 只供學(xué)習(xí)與交流資料收集于網(wǎng)絡(luò),如有侵權(quán) 請聯(lián)系網(wǎng)站刪除Test t =new Test();t .Intt();27. 鞍點( 1)import java.util.Random;public class Test int arr =new int 55;public void Intt()Random rand =new Random();for ( int r

43、=0; r 5; r +)for ( int c =0; c5; c +)arr r c = rand .nextInt(20);System. out .print( arr r c+ t );System. out .println();public void AnDian()for ( int r =0; r 5; r +)int maxC=0;for ( int j =1; j 5; j +)if ( arr r maxC arr r j ) boolean for ( intmaxC=j ;Flag =true ;u=0; u5; u +)if ( arr u maxC arr r

44、maxC)Flag =false ;if ( Flag =true ) System. out .print( +arr r maxC);( +r +, +maxC+) 是鞍點,其值為只供學(xué)習(xí)與交流資料收集于網(wǎng)絡(luò),如有侵權(quán) 請聯(lián)系網(wǎng)站刪除public static void main(String args ) Test t =new Test();t .Intt();t .AnDian();28. 鞍點( 2)import java.util.Random;public int intclass AnDian n=3; arr =new int n n ;/ 數(shù)組初始化public void

45、 Intt()Random rand =new Random();for ( int r =0; r n; r +)for ( int c =0; cn ; c +)arr r c = rand .nextInt(50)+10;System. out .print( arr r c+ t );System. out .println();/ 從列找出最小,再從行找最大public void AD()for ( int c=0; c n; c +)int minR=0;for ( int r =0; r arr r c) boolean for ( intminR =r ;Flag =true

46、;u=0; un ; u +)if ( arr minR c b ? a : b;System. out .println( The max is: + max);public void max( int a , int b, int c ) int max = a b ? ( a c ? a : c) : ( b c ? b : c);System. out .println( The max is: + max);public void max( float a , float b ) float max = a b ? a : b ;只供學(xué)習(xí)與交流資料收集于網(wǎng)絡(luò),如有侵權(quán) 請聯(lián)系網(wǎng)站刪除

47、System. out .println( The max is: + max);public static void main(String args ) Max mt =new Max();mt .max(1, 0, 5);mt.max(1,0);mt.max( float )1.0,( float )5.20); / 強制轉(zhuǎn)換類型3. 類的繼承/Person 類public class privatePerson int age ;public public public publicStringStringname; sex ;Person()Person( int age ,Stri

48、ng name,String sex )this . age =age ; this . name=name; this . sex =sex ;public void eat() System. out .println(name+ 吃飯 );/Student 類public class Student extends Person private double scroe ;public Student()super ();public Student( double scroe )super ();this . scroe =scroe ;public void exam()只供學(xué)習(xí)與交

49、流資料收集于網(wǎng)絡(luò),如有侵權(quán) 請聯(lián)系網(wǎng)站刪除System. out .println( name+ 考試 );4. 抽象類和抽象方法/ 抽象類與抽象方法/ 父類public abstract class Pet public publicdouble doubleweight ;age ;public Pet()public Pet( double weight , doublethisthis public public. weight =weight ; . age =age ;abstractabstractvoid voideat(); cral();public void displa

50、y()System. out .println( 體重為:age ) +weight +, + 年齡為: +age ); / 子類public class Cat extends Pet public Cat()public Cat( double weight , double age )this . weight =weight ;this . age =age ;Override / 重寫public void eat() System. out .println( 貓吃魚 );Overridepublic void cral() System. out .println( 喵喵 喵 )

51、;public void work() System. out .println(只供學(xué)習(xí)與交流 貓捉老鼠 );資料收集于網(wǎng)絡(luò),如有侵權(quán) 請聯(lián)系網(wǎng)站刪除/ 測試類public class Test public static void main(String args ) / 觀察兩者區(qū)別/ 子類的引用,子類的對象,子類的獨有的方法可以使用Cat cat =new Cat(5,1);cat .eat();cat .cral();cat .work();Pet cat1 =new Cat(2,1); / 父類的引用,子類的對象是可以的,但是只繼承了父類的抽象方法cat1 .eat();cat1

52、.cral();/cat1.work() 是不存在的5. 接口/ 父類public abstract class Door public double width ;public double height ;public Door()public Door( double width , double height )this . width =width ;this . height =height ;public abstract void open();public abstract void close();public void display()System. out .prin

53、tln( width: +width +,heigth: +height );/ 子類public class ProofDoor extends Door implements ProofWater,ProofThief public ProofDoor()只供學(xué)習(xí)與交流資料收集于網(wǎng)絡(luò),如有侵權(quán) 請聯(lián)系網(wǎng)站刪除super ();public ProofDoor( double width , double height )super ( width , height );Overridepublic void proofthief() System. out .println( 防盜門防盜

54、);Overridepublic void proofwater() System. out .println( 防盜門防水 );Overridepublic void open() System. out .println( 防盜門開 );Overridepublic void close() System. out .println( 防盜門關(guān) );/ 接口 1public interface ProofWater public abstract void proofwater();/ 接口 2public interface ProofThief public abstract void

55、 proofthief();/ 測試類public class Test public static void main(String args ) 只供學(xué)習(xí)與交流args ) in );資料收集于網(wǎng)絡(luò),如有侵權(quán) 請聯(lián)系網(wǎng)站刪除ProofDoor door =new ProofDoor(2,1.5);door .display();door .open();door .proofthief();door .proofwater();door .close();6. 二分查找import java.util.Scanner;public class Test public static int

56、arr =new int 2,4,6,9,22,52;/ 非遞歸/arr 在某個矩陣中查找/x 要查找的元素/n 左邊界/m 右邊界public int intint Binary( int arr , int x , int n , int m)right =m;left =n;/ 當左游標大于右游標則結(jié)束查找while ( left arr middle )left =middle +1;else if ( xarr middle )return Binary( arr , x , middle +1, right );else if ( xarr middle )return Binar

57、y( arr , x , middle -1, right );return -1;public static void main(String args ) Scanner sc= new Scanner(System. in );int p=sc.nextInt();Test t= new Test();int a=t.Binary( arr ,p,0,5);System. out .println(a);sc.close(); 7. 全排列(遞歸)import java.util.Scanner;只供學(xué)習(xí)與交流static staticint int資料收集于網(wǎng)絡(luò),如有侵權(quán) 請聯(lián)系網(wǎng)站刪

58、除public class AllPermutation public static String arr =new String , a , b , c , d ;publicpublic/ 交換兩個值的位置public void String arr a = arr b =swap( int c =arr a ; arr b; c ;/ 全排列函數(shù)pp , ppp=0; / 保存開始元素和結(jié)束元素的下標num=0; / 計數(shù)變量a , int b)/ 從第 k個元素開始排列/ 到第 n個元素結(jié)束排列/ 每層遞歸都是第一個元素先不變/ 然后依次與第一個元素,交換位置達到不一樣的排列效果pub

59、lic void allpermutation(String arr , int k , int n)/ 當k=n ,說明待排列的元素只有一個,其他元素已經(jīng)固定/ 可以輸出當前組合if ( k = n)for ( int i =pp; i =ppp ; i + )System. out .print( arr i );System. out .println();num+; / 輸出一次組合,計數(shù)加一for ( int i =k ; i =n ; i +) publicswap( k , i ); / 交換位置,固定每層的第一個元素/ 進入下一層,即固定了前一元素,排列后面的元素allperm

60、utation( arr , k +1, n);swap( k , i ); / 還原位置static void main(String args ) AllPermutation p =new AllPermutation();Scanner sc =new Scanner(System. in );System. out .println( 從第幾個元素開始排列 : );pp=sc .nextInt();System. out .println( 從第幾個元素結(jié)束排列 : );ppp =sc .nextInt();p.allpermutation( arr , pp , ppp );Sys

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
  • 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論