46設(shè)計(jì)網(wǎng)站官網(wǎng)快速開發(fā)網(wǎng)站的應(yīng)用程序
此系列文章收錄大量Java經(jīng)典代碼題(也可以算是leetcode刷題指南),希望可以與大家一起努力學(xué)好Java。3、2、1,請(qǐng)看!
目錄
1.創(chuàng)建學(xué)生成績(jī)表
2.冒泡排序
3.模擬彩票中獎(jiǎng)
4.楊輝三角
1.創(chuàng)建學(xué)生成績(jī)表
輸入n個(gè)學(xué)生的成績(jī),并打印成績(jī)高于平均分的學(xué)生。
分析:在這個(gè)問(wèn)題中,口個(gè)學(xué)生的成績(jī)需要同時(shí)存在,將其保存在一個(gè)數(shù)組中。n在程序
運(yùn)行時(shí)從鍵強(qiáng)輸人 (程序運(yùn)行時(shí)決定到底有幾個(gè)學(xué)生)。在聲明數(shù)組時(shí)只出現(xiàn)了一個(gè)引用,此時(shí)不必也不能確定數(shù)組的大小;在程序運(yùn)行的過(guò)程中,用new關(guān)鍵字動(dòng)態(tài)創(chuàng)建數(shù)組對(duì)象,此時(shí)即可以傳人已知的數(shù)組大小n。
public static void main(String[] args) throws Exception {Scanner scn=new Scanner(System.in);System.out.println("請(qǐng)輸入學(xué)生數(shù)量");int n=scn.nextInt();System.out.println("請(qǐng)輸入學(xué)生的成績(jī)");double []score=new double[n];double sum=0;for(int i=0;i<score.length;i++){double m=scn.nextDouble();score[i]=m;sum+=score[i];}double ave=sum/score.length;System.out.println("平均成績(jī)?yōu)?#34;+ave);for(int i=0;i<score.length;i++){if(score[i]>ave){System.out.println(score[i]);}}}
2.冒泡排序
所謂冒泡排序在這里就不多贅述了,代碼如下:
public static void main(String[] args) throws Exception {Scanner scn=new Scanner(System.in);System.out.println("請(qǐng)輸入數(shù)據(jù)數(shù)量");int n=scn.nextInt();int []array=new int[n];for(int i=0;i<array.length;i++){array[i]=(int)(Math.random()*100);System.out.print(array[i]+" ");}int temp=0;for(int i=0;i<array.length-1;i++){for(int j=0;j<array.length-i-1;j++){if(array[j]>array[j+1]){temp=array[j];array[j]=array[j+1];array[j+1]=temp;}} }System.out.println("排序后結(jié)果為:");for(int i=0;i<array.length;i++)System.out.print(array[i]+" ");}
3.模擬彩票中獎(jiǎng)
中國(guó)福利彩票的雙色球開獎(jiǎng)規(guī)則:從編號(hào)是01~33的紅色球中選取6個(gè),從編號(hào)是01~16的藍(lán)色球中選取一個(gè)。
分析:模擬這個(gè)抽獎(jiǎng)過(guò)程,將紅色球和藍(lán)色球各自保存在一個(gè) boolean 數(shù)組中,數(shù)組元素下標(biāo)代表球號(hào)(從下標(biāo)1開始使用),數(shù)組元素取值true/false 代表該球是否被選中(初始均為false)。抽獎(jiǎng)過(guò)程中生成隨機(jī)數(shù)代表開獎(jiǎng)球在數(shù)組中的編號(hào),如果該球尚未被選出,則將其選中標(biāo)記置為 true。
public static void main(String[] args) throws Exception {Scanner scn=new Scanner(System.in);boolean []red=new boolean[34];//紅球,編號(hào)1~33,默認(rèn)值為:falseboolean []blue=new boolean[17];//籃球,編號(hào)為1~16System.out.println("準(zhǔn)備開獎(jiǎng)");int cont=0;while (cont<6) {int m=(int)(Math.random()*33)+1;//隨機(jī)生成數(shù)字if(red[m]==false)//未被標(biāo)記則標(biāo)記{red[m]=true;//進(jìn)行標(biāo)記cont++;}}int n=(int)(Math.random()*16)+1;if(blue[n]==false)blue[n]=true;System.out.println("紅色球編號(hào)為:");for(int i=0;i<red.length;i++){if(red[i]==true)System.out.println((i<10?"0"+i:i)+" ");} System.out.println("藍(lán)色球編號(hào)為:");for(int i=0;i<blue.length;i++){if(blue[i]==true)System.out.println((i<10?"0"+i:i));}}
4.楊輝三角
如下面圖片的數(shù)學(xué)模型即為楊輝三角
分析:可以使用Java不規(guī)則數(shù)組實(shí)現(xiàn),在程序運(yùn)行過(guò)程中動(dòng)態(tài)創(chuàng)建數(shù)組
public static void main(String[] args) throws Exception {Scanner scn=new Scanner(System.in);System.out.println("輸入要打印楊輝三角的行數(shù)");int n=scn.nextInt();int [][]tri=new int[n][];tri[0]=new int[1];tri[0][0]=1;//第一行為1for(int i=1;i<tri.length;i++){tri[i]=new int[i+1];tri[i][0]=tri[i][i]=1;//每一行兩端為1for(int j=1;j<i;j++){tri[i][j]=tri[i-1][j-1]+tri[i-1][j];}}//輸出楊輝三角for(int i=0;i<tri.length;i++){for(int j=0;j<tri[i].length;j++)System.out.printf("%4d",tri[i][j]);System.out.println();}}
這篇文章到此結(jié)束,感謝各位的閱讀和所提出的寶貴意見(jiàn),大家也可以試著自己動(dòng)手編寫代碼。如果覺(jué)得這篇文章寫的還可以或者對(duì)您有幫助,麻煩點(diǎn)贊收藏加轉(zhuǎn)發(fā)!!!