50道Java基礎(chǔ)編程練習(xí)題

50道經(jīng)典Java編程練習(xí)題加派,將數(shù)學(xué)思維運用到編程中來。抱歉哈找不到文章的原貼了均蜜,有冒犯的麻煩知會聲哈~

1.指數(shù)計算問題

有一對兔子却特,從出生后第3個月起每個月都生一對兔子,小兔子長到第三個月后每個月又生一對兔子惶看,假如兔子都不死捏顺,問每個月的兔子對數(shù)為多少?

程序分析: 兔子的規(guī)律為數(shù)列1,1,2,3,5,8,13,21....

public class Prog1{? ? public static void main(String[] args){? ? ? ? int n = 10;? ? ? ? System.out.println("第"+n+"個月兔子總數(shù)為"+fun(n));? ? }? ? private static int fun(int n){? ? ? ? if(n==1 || n==2)? ? ? ? ? return 1;? ? ? ? else? ? ? ? ? return fun(n-1)+fun(n-2);? ? }}

2.指定范圍包含的素數(shù)

判斷101-200之間有多少個素數(shù)纬黎,并輸出所有素數(shù)草丧。

程序分析:判斷素數(shù)的方法:用一個數(shù)分別去除2到sqrt(這個數(shù)),如果能被整除莹桅,則表明此數(shù)不是素數(shù),反之是素數(shù)。

public class Prog2{? ? public static void main(String[] args){? ? ? ? int m = 1;? ? ? ? int n = 1000;? ? ? ? int count = 0;? ? ? ? //統(tǒng)計素數(shù)個數(shù)? ? ? ? for(int i=m;i

3.水仙花數(shù)

打印出所有的"水仙花數(shù)"诈泼,所謂"水仙花數(shù)"是指一個三位數(shù)懂拾,其各位數(shù)字立方和等于該數(shù)本身。例如:153是一個"水仙花數(shù)"铐达,因為153=1的三次方+5的三次方+3的三次方岖赋。

程序分析:利用for循環(huán)控制100-999個數(shù),每個數(shù)分解出個位瓮孙,十位唐断,百位。

public class Prog3{? ? public static void main(String[] args){? ? ? ? for(int i=100;i<1000;i++){? ? ? ? ? ? if(isLotus(i))? ? ? ? ? ? ? System.out.print(i+" ");? ? ? ? }? ? ? ? System.out.println();? ? }? ? //判斷水仙花數(shù)? ? private static boolean isLotus(int lotus){? ? ? ? int m = 0;? ? ? ? int n = lotus;? ? ? ? int sum = 0;? ? ? ? m = n/100;? ? ? ? n? -= m*100;? ? ? ? sum = m*m*m;? ? ? ? m = n/10;? ? ? ? n -= m*10;? ? ? ? sum += m*m*m + n*n*n;? ? ? ? if(sum==lotus)? ? ? ? ? ? return true;? ? ? ? else? ? ? ? ? ? return false;? ? ? ? }}

4.分解質(zhì)因數(shù)

將一個正整數(shù)分解質(zhì)因數(shù)杭抠。例如:輸入90,打印出90=2*3*3*5脸甘。

程序分析:對n進(jìn)行分解質(zhì)因數(shù),應(yīng)先找到一個最小的質(zhì)數(shù)k偏灿,然后按下述步驟完成:

(1)如果這個質(zhì)數(shù)恰等于n丹诀,則說明分解質(zhì)因數(shù)的過程已經(jīng)結(jié)束,打印出即可翁垂。(2)如果n<>k铆遭,但n能被k整除,則應(yīng)打印出k的值沿猜,并用n除以k的商,作為新的正整數(shù)n,重復(fù)執(zhí)行第一步枚荣。(3)如果n不能被k整除,則用k+1作為k的值,重復(fù)執(zhí)行第一步啼肩。public class Prog4{? ? public static void main(String[] args){? ? ? ? int n = 13;? ? ? ? decompose(n);? ? }? ? private static void decompose(int n){? ? ? ? System.out.print(n+"=");? ? ? ? for(int i=2;i

5.條件運算符使用

利用條件運算符的嵌套來完成此題:學(xué)習(xí)成績>=90分的同學(xué)用A表示橄妆,60-89分之間的用B表示,60分以下的用C表示疟游。

程序分析:(a>b)?a:b這是條件運算符的基本例子呼畸。

public class Prog5{? ? public static void main(String[] args){? ? ? ? int n = -1;? ? ? ? try{? ? ? ? ? ? n = Integer.parseInt(args[0]);? ? ? ? }catch(ArrayIndexOutOfBoundsException e){? ? ? ? ? ? System.out.println("請輸入成績");? ? ? ? ? ? return;? ? ? ? }? ? ? ? grade(n);? ? }? ? //成績等級計算? ? private static void grade(int n){? ? ? ? if(n>100 || n<0)? ? ? ? ? System.out.println("輸入無效");? ? ? ? else{? ? ? ? ? String str = (n>=90)?"分,屬于A等":((n>60)?"分颁虐,屬于B等":"分蛮原,屬于C等");? ? ? ? ? System.out.println(n+str);? ? ? ? }? ? }}

6.公約數(shù)和公倍數(shù)

輸入兩個正整數(shù)m和n,求其最大公約數(shù)和最小公倍數(shù)另绩。

程序分析:利用輾除法儒陨。

public class Prog6{? ? public static void main(String[] args){? ? ? ? int m,n;? ? ? ? try{? ? ? ? ? ? m = Integer.parseInt(args[0]);? ? ? ? ? ? n = Integer.parseInt(args[1]);? ? ? ? }catch(ArrayIndexOutOfBoundsException e){? ? ? ? ? ? System.out.println("輸入有誤");? ? ? ? ? ? return;? ? ? ? }? ? ? ? max_min(m,n);? ? }? ? //求最大公約數(shù)和最小公倍數(shù)? ? private static void max_min(int m, int n){? ? ? ? int temp = 1;? ? ? ? int yshu = 1;? ? ? ? int bshu = m*n;? ? ? ? if(n

7.統(tǒng)計字符串中類型個數(shù)

輸入一行字符,分別統(tǒng)計出其中英文字母笋籽、空格蹦漠、數(shù)字和其它字符的個數(shù)。

程序分析:利用while語句,條件為輸入的字符不為'\n'.

import java.util.Scanner;public class Prog7_1{? ? public static void main(String[] args){? ? ? ? System.out.print("請輸入一串字符:");? ? ? ? Scanner scan = new Scanner(System.in);? ? ? ? String str = scan.nextLine();//將一行字符轉(zhuǎn)化為字符串? ? ? ? scan.close();? ? ? ? count(str);? ? }? ? //統(tǒng)計輸入的字符數(shù)? ? private static void count(String str){? ? ? ? String E1 = "[\u4e00-\u9fa5]";//漢字? ? ? ? String E2 = "[a-zA-Z]";? ? ? ? String E3 = "[0-9]";? ? ? ? String E4 = "\\s";//空格? ? ? ? int countChinese = 0;? ? ? ? int countLetter = 0;? ? ? ? int countNumber = 0;? ? ? ? int countSpace = 0;? ? ? ? int countOther = 0;? ? ? ? char[] array_Char = str.toCharArray();//將字符串轉(zhuǎn)化為字符數(shù)組? ? ? ? String[] array_String = new String[array_Char.length];//漢字只能作為字符串處理? ? ? ? for(int i=0;i list = new ArrayList();? ? ? ? char[] array_Char = str.toCharArray();? ? ? ? for(char c:array_Char)? ? ? ? ? list.add(String.valueOf(c));//將字符作為字符串添加到list表中? ? ? ? Collections.sort(list);//排序? ? ? ? for(String s:list){? ? ? ? ? ? int begin = list.indexOf(s);? ? ? ? ? ? int end = list.lastIndexOf(s);? ? ? ? ? ? //索引結(jié)束統(tǒng)計字符數(shù)? ? ? ? ? ? if(list.get(end)==s)? ? ? ? ? ? ? System.out.println("字符‘"+s+"’有"+(end-begin+1)+"個");? ? ? ? }? ? }}

8.求s=a+aa+aaa+aaaa+aa...a的值

求s=a+aa+aaa+aaaa+aa...a的值车海,其中a是一個數(shù)字笛园。例如2+22+222+2222+22222(此時共有5個數(shù)相加),幾個數(shù)相加有鍵盤控制。

程序分析:關(guān)鍵是計算出每一項的值研铆。

import java.util.Scanner;public class Prog8{? ? public static void main(String[] args){? ? ? ? System.out.print("求s=a+aa+aaa+aaaa+...的值埋同,請輸入a的值:");? ? ? ? Scanner scan = new Scanner(System.in).useDelimiter("\\s*");//以空格作為分隔符? ? ? ? int a = scan.nextInt();? ? ? ? int n = scan.nextInt();? ? ? ? scan.close();//關(guān)閉掃描器? ? ? ? System.out.println(expressed(2,5)+add(2,5));? ? }? ? //求和表達(dá)式? ? private static String expressed(int a,int n){? ? ? ? StringBuffer sb = new StringBuffer();? ? ? ? StringBuffer subSB = new StringBuffer();? ? ? ? for(int i=1;i

9.指定范圍的完數(shù)

一個數(shù)如果恰好等于它的因子之和,這個數(shù)就稱為"完數(shù)"棵红。例如6=1+2+3.編程找出1000以內(nèi)的所有完數(shù)凶赁。

public class Prog9{? ? public static void main(String[] args){? ? ? ? int n = 10000;? ? ? ? compNumber(n);? ? }? ? //求完數(shù)? ? private static void compNumber(int n){? ? ? ? int count = 0;? ? ? ? System.out.println(n+"以內(nèi)的完數(shù):");? ? ? ? for (int i = 1; i <= 10000; i++) {? ? ? ? ? ? int temp = 0;// 定義因子之和變量? ? ? ? ? ? for (int n = 1; n < i / 2 + 1; n++) {? ? ? ? ? ? ? ? if (i % n == 0) {? ? ? ? ? ? ? ? ? ? temp += n;// 能被整除的除數(shù)則被加到temp中? ? ? ? ? ? ? ? }? ? ? ? ? ? }? ? ? ? ? ? if (temp == i) {// 如果因子之和與原數(shù)相等的話,說明是完數(shù)? ? ? ? ? ? ? ? System.out.println(i + " ");// 輸出完數(shù)? ? ? ? ? ? }? ? ? ? }? ? }}

10.反指數(shù)計算

一球從100米高度自由落下逆甜,每次落地后反跳回原高度的一半虱肄;再落下,求它在 第10次落地時交煞,共經(jīng)過多少米咏窿?第10次反彈多高?

import java.util.Scanner;public class Prog10{? ? public static void main(String[] args){? ? ? ? System.out.print("請輸入小球落地時的高度和求解的次數(shù):");? ? ? ? Scanner scan = new Scanner(System.in).useDelimiter("\\s");? ? ? ? int h = scan.nextInt();? ? ? ? int n = scan.nextInt();? ? ? ? scan.close();? ? ? ? distance(h,n);? ? }? ? //小球從h高度落下错敢,經(jīng)n次反彈后經(jīng)過的距離和反彈的高度? ? private static void distance(double h,int n){? ? ? ? double length = 0;? ? ? ? for(int i=0;i

11.組合

有1翰灾、2、3稚茅、4個數(shù)字纸淮,能組成多少個互不相同且無重復(fù)數(shù)字的三位數(shù)?都是多少亚享?

程序分析:可填在百位咽块、十位、個位的數(shù)字都是1欺税、2侈沪、3、4晚凿。組成所有的排列后再去 掉不滿足條件的排列亭罪。

public class Prog11{? ? public static void main(String[] args){? ? ? ? int count = 0;? ? ? ? int n = 0;? ? ? ? for(int i=1;i<5;i++){? ? ? ? ? ? for(int j=1;j<5;j++){? ? ? ? ? ? ? ? if(j==i)? ? ? ? ? ? ? ? ? continue;? ? ? ? ? ? ? ? for(int k=1;k<5;k++){? ? ? ? ? ? ? ? ? ? if(k!=i && k!=j){? ? ? ? ? ? ? ? ? ? ? ? n = i*100+j*10+k;? ? ? ? ? ? ? ? ? ? ? System.out.print(n+" ");? ? ? ? ? ? ? ? ? ? ? if((++count)%5==0)? ? ? ? ? ? ? ? ? ? ? System.out.println();? ? ? ? ? ? ? ? ? ? }? ? ? ? ? ? ? ? }? ? ? ? ? ? }? ? ? ? }? ? ? ? System.out.println();? ? ? ? System.out.println("符合條件的數(shù)共:"+count+"個");? ? }}

12.梯度計算

企業(yè)發(fā)放的獎金根據(jù)利潤提成。利潤(I)低于或等于10萬元時歼秽,獎金可提10%应役;利潤高于10萬元,低于20萬元時燥筷,低于10萬元的部分按10%提成箩祥,高于10萬元的部分,可可提成7.5%肆氓;20萬到40萬之間時袍祖,高于20萬元的部分,可提成5%谢揪;40萬到60萬之間時高于40萬元的部分蕉陋,可提成3%捐凭;60萬到100萬之間時,高于60萬元的部分寺滚,可提成1.5%柑营,高于100萬元時,超過100萬元的部分按1%提成村视,從鍵盤輸入當(dāng)月利潤I,求應(yīng)發(fā)放獎金總數(shù)酒奶?

程序分析:請利用數(shù)軸來分界蚁孔,定位。注意定義時需把獎金定義成長整型惋嚎。

import java.io.*;public class Prog12{? ? public static void main(String[] args){? ? ? ? System.out.print("請輸入當(dāng)前利潤:");? ? ? ? long profit = Long.parseLong(key_Input());? ? ? ? System.out.println("應(yīng)發(fā)獎金:"+bonus(profit));? ? }? ? //接受從鍵盤輸入的內(nèi)容? ? private static String key_Input(){? ? ? ? String str = null;? ? ? ? BufferedReader bufIn = new BufferedReader(new InputStreamReader(System.in));? ? ? ? try{? ? ? ? ? ? str = bufIn.readLine();? ? ? ? }catch(IOException e){? ? ? ? ? ? e.printStackTrace();? ? ? ? }finally{? ? ? ? ? ? try{? ? ? ? ? ? ? ? bufIn.close();? ? ? ? ? ? }catch(IOException e){? ? ? ? ? ? ? ? e.printStackTrace();? ? ? ? ? ? }? ? ? ? }? ? ? ? return str;? ? }? ? //計算獎金? ? private static long bonus(long profit){? ? ? ? long prize = 0;? ? ? ? long profit_sub = profit;? ? ? ? if(profit>1000000){? ? ? ? ? ? profit = profit_sub-1000000;? ? ? ? ? ? profit_sub = 1000000;? ? ? ? ? ? prize += profit*0.01;? ? ? ? }? ? ? ? if(profit>600000){? ? ? ? ? ? profit = profit_sub-600000;? ? ? ? ? ? profit_sub = 600000;? ? ? ? ? ? prize += profit*0.015;? ? ? ? }? ? ? ? if(profit>400000){? ? ? ? ? ? profit = profit_sub-400000;? ? ? ? ? ? profit_sub = 400000;? ? ? ? ? ? prize += profit*0.03;? ? ? ? }? ? ? ? if(profit>200000){? ? ? ? ? ? profit = profit_sub-200000;? ? ? ? ? ? profit_sub = 200000;? ? ? ? ? ? prize += prize*0.05;? ? ? ? }? ? ? ? if(profit>100000){? ? ? ? ? ? profit = profit_sub-100000;? ? ? ? ? ? profit_sub = 100000;? ? ? ? ? ? prize += profit*0.075;? ? ? ? }? ? ? ? prize += profit_sub*0.1;? ? ? ? return prize;? ? }}

13.求未知數(shù)

一個整數(shù)杠氢,它加上100后是一個完全平方數(shù),再加上168又是一個完全平方數(shù)另伍,請問該數(shù)是多少鼻百?

程序分析:在10萬以內(nèi)判斷,先將該數(shù)加上100后再開方摆尝,再將該數(shù)加上268后再開方温艇,如果開方后的結(jié)果滿足如下條件,即是結(jié)果堕汞。

public class Prog13{? ? public static void main(String[] args){? ? ? ? int n=0;? ? ? ? for(int i=0;i<100001;i++){? ? ? ? ? ? if(isCompSqrt(i+100) && isCompSqrt(i+268)){? ? ? ? ? ? ? ? n = i;? ? ? ? ? ? ? ? break;? ? ? ? ? ? }? ? ? ? }? ? ? ? System.out.println("所求的數(shù)是:"+n);? ? }? ? //判斷完全平方數(shù)? ? private static boolean isCompSqrt(int n){? ? ? ? boolean isComp = false;? ? ? ? for(int i=1;i

14.日期計算

輸入某年某月某日勺爱,判斷這一天是這一年的第幾天?

程序分析:以3月5日為例讯检,應(yīng)該先把前兩個月的加起來琐鲁,然后再加上5天即本年的第幾天,特殊情況人灼,閏年且輸入月份大于3時需考慮多加一天围段。

import java.util.Scanner;public class Prog14{? ? public static void main(String[] args){? ? ? ? Scanner scan = new Scanner(System.in).useDelimiter("\\D");//匹配非數(shù)字? ? ? ? System.out.print("請輸入當(dāng)前日期(年-月-日):");? ? ? ? int year = scan.nextInt();? ? ? ? int month = scan.nextInt();? ? ? ? int date = scan.nextInt();? ? ? ? scan.close();? ? ? ? System.out.println("今天是"+year+"年的第"+analysis(year,month,date)+"天");? ? }? ? //判斷天數(shù)? ? private static int analysis(int year, int month, int date){? ? ? ? int n = 0;? ? ? ? int[] month_date = new int[] {0,31,28,31,30,31,30,31,31,30,31,30};? ? ? ? if((year%400)==0 || ((year%4)==0)&&((year%100)!=0))? ? ? ? ? month_date[2] = 29;? ? ? ? for(int i=0;i

15.排序

輸入三個整數(shù)x,y,z,請把這三個數(shù)由小到大輸出投放。

程序分析:我們想辦法把最小的數(shù)放到x上奈泪,先將x與y進(jìn)行比較,如果x>y則將x與y的值進(jìn)行交換跪呈,然后再用x與z進(jìn)行比較段磨,如果x>z則將x與z的值進(jìn)行交換,這樣能使x最小耗绿。

import java.util.Scanner;public class Prog15{? ? public static void main(String[] args){? ? ? ? Scanner scan = new Scanner(System.in).useDelimiter("\\D");? ? ? ? System.out.print("請輸入三個數(shù):");? ? ? ? int x = scan.nextInt();? ? ? ? int y = scan.nextInt();? ? ? ? int z = scan.nextInt();? ? ? ? scan.close();? ? ? ? System.out.println("排序結(jié)果:"+sort(x,y,z));? ? }? ? //比較兩個數(shù)的大小? ? private static String sort(int x,int y,int z){? ? ? ? String s = null;? ? ? ? if(x>y){? ? ? ? ? ? int t = x;? ? ? ? ? ? x = y;? ? ? ? ? ? y = t;? ? ? ? }? ? ? ? if(x>z){? ? ? ? ? ? int t = x;? ? ? ? ? ? x = z;? ? ? ? ? ? z = t;? ? ? ? }? ? ? ? if(y>z){? ? ? ? ? ? int t = z;? ? ? ? ? ? z = y;? ? ? ? ? ? y = t;? ? ? ? }? ? ? ? s = x+" "+y+" "+z;? ? ? ? return s;? ? }}

16.冒泡排序

輸出9*9口訣苹支。

程序分析:分行與列考慮,共9行9列误阻,i控制行债蜜,j控制列晴埂。

public class Prog16{? ? public static void main(String[] args){? ? ? ? for(int i=1;i<10;i++){? ? ? ? ? ? for(int j=1;j

17.反推計算

猴子吃桃問題:猴子第一天摘下若干個桃子,當(dāng)即吃了一半寻定,還不癮儒洛,又多吃了一個 第二天早上又將剩下的桃子吃掉一半,又多吃了一個狼速。以后每天早上都吃了前一天剩下的一半零一個琅锻。到第10天早上想再吃時,見只剩下一個桃子了向胡。求第一天共摘了多少恼蓬。

程序分析:采取逆向思維的方法,從后往前推斷僵芹。

public class Prog17{? ? public static void main(String[] args){? ? ? ? int m = 1;? ? ? for(int i=10;i>0;i--)? ? ? ? m = 2*m + 2;? ? ? System.out.println("小猴子共摘了"+m+"桃子");? ? }}

18.數(shù)組計算

兩個乒乓球隊進(jìn)行比賽处硬,各出三人。甲隊為a,b,c三人拇派,乙隊為x,y,z三人荷辕。已抽簽決定比賽名單。有人向隊員打聽比賽的名單件豌。a說他不和x比疮方,c說他不和x,z比,請編程序找出三隊賽手的名單苟径。

import java.util.ArrayList;public class Prog18{? ? String a,b,c;//甲隊成員? ? public static void main(String[] args){? ? ? ? String[] racer = {"x","y","z"};//乙隊成員? ? ? ? ArrayList arrayList = new ArrayList();? ? ? ? for(int i=0;i<3;i++)? ? ? ? ? for(int j=0;j<3;j++)? ? ? ? ? ? for(int k=0;k<3;k++){? ? ? ? ? ? ? ? Prog18 prog18 = new Prog18(racer[i],racer[j],racer[k]);? ? ? ? ? ? ? ? if(!prog18.a.equals(prog18.b) && !prog18.a.equals(prog18.c) && !prog18.b.equals(prog18.c) &&? ? ? ? ? ? ? ? ? !prog18.a.equals("x") && !prog18.c.equals("x") && !prog18.c.equals("z"))? ? ? ? ? ? ? ? ? arrayList.add(prog18);? ? ? ? ? ? }? ? ? ? ? for(Object obj:arrayList)? ? ? ? ? ? System.out.println(obj);? ? }? ? //構(gòu)造方法? ? private Prog18(String a,String b,String c){? ? ? ? this.a = a;? ? ? ? this.b = b ;? ? ? ? this.c = c;? ? }? ? public String toString(){? ? ? ? return "a的對手是"+a+"? "+"b的對手是"+b+"? "+"c的對手是"+c;? ? }}

19.打印出如下圖案(菱形)

*? ***? ****** ********? ******? ***? ? *

程序分析:先把圖形分成兩部分來看待案站,前四行一個規(guī)律,后三行一個規(guī)律棘街,利用雙重 for循環(huán)蟆盐,第一層控制行,第二層控制列遭殉。

public class Prog19{? ? public static void main(String[] args){? ? ? ? int n = 5;? ? ? ? printStar(n);? ? }? ? //打印星星? ? private static void printStar(int n){? ? ? ? //打印上半部分? ? ? ? for(int i=0;i=n-i && j<=n+i)? ? ? ? ? ? ? ? System.out.print("*");? ? ? ? ? }? ? ? ? ? System.out.println();? ? ? ? }? ? ? ? //打印下半部分? ? ? ? for(int i=1;i=i && j<2*n-i-1)? ? ? ? ? ? ? ? System.out.print("*");? ? ? ? ? ? }? ? ? ? ? ? System.out.println();? ? ? ? }? ? }}

20.數(shù)列求和

有一分?jǐn)?shù)序列:2/1石挂,3/2,5/3险污,8/5痹愚,13/8,21/13...求出這個數(shù)列的前20項之和蛔糯。

程序分析:請抓住分子與分母的變化規(guī)律拯腮。

public class Prog20{? ? public static void main(String[] args){? ? ? ? double n1 = 1;? ? ? ? double n2 = 1;? ? ? ? double fraction = n1/n2;? ? ? ? double Sn = 0;? ? ? ? for(int i=0;i<20;i++){? ? ? ? ? double t1 = n1;? ? ? ? ? double t2 = n2;? ? ? ? ? n1 = t1+t2;? ? ? ? ? n2 = t1;? ? ? ? ? fraction = n1/n2;? ? ? ? ? Sn += fraction;? ? ? ? }? ? ? ? System.out.print(Sn);? ? }}

21.求1+2!+3!+...+20!的和

程序分析:此程序只是把累加變成了累乘。

public class Prog21{? ? public static void main(String[] args){? ? ? ? long sum = 0;? ? ? ? for(int i=0;i<20;i++)? ? ? ? ? sum += factorial(i+1);? ? ? ? System.out.println(sum);? ? }? ? //階乘? ? private static long factorial(int n){? ? ? ? int mult = 1;? ? ? ? for(int i=1;i

22.利用遞歸方法求5!蚁飒。

程序分析:遞歸公式:fn=fn_1*4!

public class Prog22{? ? public static void main(String[] args){? ? ? ? System.out.println(fact(10));? ? }? ? //遞歸求階乘? ? private static long fact(int n){? ? ? ? if(n==1)? ? ? ? ? return 1;? ? ? ? else? ? ? ? ? return fact(n-1)*n;? ? }}

23.遞歸計算

有5個人坐在一起动壤,問第五個人多少歲?他說比第4個人大2歲淮逻。問第4個人歲數(shù)琼懊,他說比第3個人大2歲阁簸。問第三個人,又說比第2人大兩歲哼丈。問第2個人启妹,說比第一個人大兩歲。最后問第一個人醉旦,他說是10歲饶米。請問第五個人多大?

程序分析:利用遞歸的方法车胡,遞歸分為回推和遞推兩個階段咙崎。要想知道第五個人歲數(shù),需知道第四人的歲數(shù)吨拍,依次類推,推到第一人(10歲)网杆,再往回推羹饰。

public class Prog23{? ? public static void main(String[] args){? ? ? ? System.out.println(getAge(5,2));? ? }? ? //求第m位同志的年齡? ? private static int getAge(int m,int n){? ? ? ? if(m==1)? ? ? ? ? return 10;? ? ? ? else? ? ? ? ? return getAge(m-1,n)+n;? ? ? ? ? ? }}

24.倒序打印

給一個不多于5位的正整數(shù),要求:一碳却、求它是幾位數(shù)队秩,二、逆序打印出各位數(shù)字昼浦。

public class Prog24{? ? public static void main(String[] args){? ? ? ? int n = Integer.parseInt(args[0]);? ? ? ? int i = 0;? ? ? ? int[] a = new int[5];? ? ? ? do{? ? ? ? ? ? a[i] = n%10;? ? ? ? ? n /= 10;? ? ? ? ? ++i;? ? ? ? }while(n!=0);? ? ? ? System.out.print("這是一個"+i+"位數(shù)馍资,從個位起,各位數(shù)字依次為:");? ? ? ? for(int j=0;j

25.回文數(shù)

一個5位數(shù)关噪,判斷它是不是回文數(shù)鸟蟹。即12321是回文數(shù),個位與萬位相同使兔,十位與千位相同建钥。

import java.io.*;public class Prog25{? ? public static void main(String[] args){? ? ? ? int n = 0;? ? ? ? System.out.print("請輸入一個5位數(shù):");? ? ? ? BufferedReader bufin = new BufferedReader(new InputStreamReader(System.in));? ? ? ? try{? ? ? ? ? n = Integer.parseInt(bufin.readLine());? ? ? ? }catch(IOException e){? ? ? ? ? ? e.printStackTrace();? ? ? ? }finally{? ? ? ? ? ? try{? ? ? ? ? ? ? bufin.close();? ? ? ? ? ? }catch(IOException e){? ? ? ? ? ? ? ? e.printStackTrace();? ? ? ? ? ? }? ? ? ? }? ? ? ? palin(n);? ? }? ? private static void palin(int n){? ? ? ? int m = n;? ? ? ? int[] a = new int[5];? ? ? ? if(n<10000 || n>99999){? ? ? ? ? ? System.out.println("輸入的不是5位數(shù)!");? ? ? ? ? ? return;? ? ? ? }else{? ? ? ? ? for(int i=0;i<5;i++){? ? ? ? ? ? ? a[i] = n%10;? ? ? ? ? ? ? n /= 10;? ? ? ? ? }? ? ? ? ? if(a[0]==a[4] && a[1]==a[3])? ? ? ? ? ? System.out.println(m+"是一個回文數(shù)");? ? ? ? ? else? ? ? ? ? ? System.out.println(m+"不是回文數(shù)");? ? ? ? }? }}

26.匹配單詞

請輸入星期幾的第一個字母來判斷一下是星期幾虐沥,如果第一個字母一樣熊经,則繼續(xù) 判斷第二個字母。

程序分析:用情況語句比較好欲险,如果第一個字母一樣镐依,則判斷用情況語句或if語句判斷第二個字母。

import java.io.*;public class Prog26{? ? public static void main(String[] args){? ? ? ? String str = new String();? ? ? BufferedReader bufIn = new BufferedReader(new InputStreamReader(System.in));? ? ? System.out.print("請輸入星期的英文單詞前兩至四個字母):");? ? ? try{? ? ? ? ? str = bufIn.readLine();? ? ? }catch(IOException e){? ? ? ? ? e.printStackTrace();? ? ? }finally{? ? ? ? ? try{? ? ? ? ? ? ? bufIn.close();? ? ? ? ? }catch(IOException e){? ? ? ? ? ? ? e.printStackTrace();? ? ? ? ? }? ? ? }? ? ? week(str);? ? }? ? private static void week(String str){? ? ? ? int n = -1;? ? ? ? if(str.trim().equalsIgnoreCase("Mo") || str.trim().equalsIgnoreCase("Mon") || str.trim().equalsIgnoreCase("Mond"))? ? ? ? ? n = 1;? ? ? ? if(str.trim().equalsIgnoreCase("Tu") || str.trim().equalsIgnoreCase("Tue") || str.trim().equalsIgnoreCase("Tues"))? ? ? ? ? n = 2;? ? ? ? if(str.trim().equalsIgnoreCase("We") || str.trim().equalsIgnoreCase("Wed") || str.trim().equalsIgnoreCase("Wedn"))? ? ? ? ? n = 3;? ? ? ? if(str.trim().equalsIgnoreCase("Th") || str.trim().equalsIgnoreCase("Thu") || str.trim().equalsIgnoreCase("Thur"))? ? ? ? ? n = 4;? ? ? ? if(str.trim().equalsIgnoreCase("Fr") || str.trim().equalsIgnoreCase("Fri") || str.trim().equalsIgnoreCase("Frid"))? ? ? ? ? n = 5;? ? ? ? if(str.trim().equalsIgnoreCase("Sa") || str.trim().equalsIgnoreCase("Sat") || str.trim().equalsIgnoreCase("Satu"))? ? ? ? ? n = 2;? ? ? ? if(str.trim().equalsIgnoreCase("Su") || str.trim().equalsIgnoreCase("Sun") || str.trim().equalsIgnoreCase("Sund"))? ? ? ? ? n = 0;? ? ? ? switch(n){? ? ? ? ? ? case 1:? ? ? ? ? ? ? System.out.println("星期一");? ? ? ? ? ? ? break;? ? ? ? ? ? case 2:? ? ? ? ? ? ? System.out.println("星期二");? ? ? ? ? ? ? break;? ? ? ? ? ? case 3:? ? ? ? ? ? ? System.out.println("星期三");? ? ? ? ? ? ? break;? ? ? ? ? ? case 4:? ? ? ? ? ? ? System.out.println("星期四");? ? ? ? ? ? ? break;? ? ? ? ? ? case 5:? ? ? ? ? ? ? System.out.println("星期五");? ? ? ? ? ? ? break;? ? ? ? ? ? case 6:? ? ? ? ? ? ? System.out.println("星期六");? ? ? ? ? ? ? break;? ? ? ? ? ? case 0:? ? ? ? ? ? ? System.out.println("星期日");? ? ? ? ? ? ? break;? ? ? ? ? ? default:? ? ? ? ? ? ? System.out.println("輸入有誤天试!");? ? ? ? ? ? ? break;? ? ? ? }? ? }}

27.求100之內(nèi)的素數(shù)

public class Prog27{? ? public static void main(String[] args){? ? ? ? int n = 100;? ? ? ? System.out.print(n+"以內(nèi)的素數(shù):");? ? ? ? for(int i=2;i

28.對10個數(shù)進(jìn)行排序

程序分析:可以利用選擇法槐壳,即從后9個比較過程中,選擇一個最小的與第一個元素交換秋秤, 下次類推宏粤,即用第二個元素與后8個進(jìn)行比較脚翘,并進(jìn)行交換。

public class Prog28{? ? public static void main(String[] args){? ? ? ? int[] a = new int[]{31,42,21,50,12,60,81,74,101,93};? ? ? ? for(int i=0;i<10;i++){? ? ? ? ? ? for(int j=0;ja[j+1]){? ? ? ? ? ? ? ? ? ? int temp = a[j];? ? ? ? ? ? ? ? ? ? a[j] = a[j+1];? ? ? ? ? ? ? ? ? ? a[j+1] = temp;? ? ? ? ? ? ? ? }? ? ? ? ? ? }? ? ? ? }? ? ? ? for(int i=0;i

29.求一個3*3矩陣對角線元素之和

程序分析:利用雙重for循環(huán)控制輸入二維數(shù)組绍哎,再將a[i][i]累加后輸出来农。

public class Prog29{? ? public static void main(String[] args){? ? ? ? int[][] a = new int[][] {{100,2,3,},{4,5,6},{17,8,9}};? ? ? ? matrSum(a);? ? }? ? private static void matrSum(int[][] a){? ? ? ? int sum1 = 0;? ? ? ? int sum2 = 0;? ? ? ? for(int i=0;i

30.比較排序

有一個已經(jīng)排好序的數(shù)組。現(xiàn)輸入一個數(shù)崇堰,要求按原來的規(guī)律將它插入數(shù)組中沃于。

程序分析:首先判斷此數(shù)是否大于最后一個數(shù),然后再考慮插入中間的數(shù)的情況海诲,插入后此元素之后的數(shù)繁莹,依次后移一個位置。

import java.util.Scanner;public class Prog30{? ? public static void main(String[] args){? ? ? ? int[] A = new int[]{0,8,7,5,9,1,2,4,3,12};? ? ? ? int[] B = sort(A);? ? ? ? print(B);? ? ? ? System.out.println();? ? ? ? System.out.print("請輸入10個數(shù)的數(shù)組:");? ? ? ? Scanner scan = new Scanner(System.in);? ? ? ? int a = scan.nextInt();scan.close();? ? ? ? int[] C = insert(a,B);? ? ? ? print(C);? ? }? ? //選擇排序? ? private static int[] sort(int[] A){? ? ? ? int[] B = new int[A.length];? ? ? ? for(int i=0;iA[j]){? ? ? ? ? ? ? ? ? ? int temp = min;? ? ? ? ? ? ? ? ? ? min = A[j];? ? ? ? ? ? ? ? ? ? A[j] = temp;? ? ? ? ? ? ? ? }? ? ? ? ? ? ? ? B[i] = min;? ? ? ? ? ? }? ? ? ? }? ? ? ? B[A.length-1] = A[A.length-1];? ? ? ? return B;? ? }? ? //打印? ? private static void print(int[] A){? ? ? ? for(int i=0;i0;i--)? ? ? ? ? if(a>A[i]){? ? ? ? ? ? ? B[i+1] = a;? ? ? ? ? ? for(int j=0;j<=i;j++)? ? ? ? ? ? ? B[j] = A[j];? ? ? ? ? ? ? for(int k=i+2;k

31.將一個數(shù)組逆序輸出特幔。

程序分析:用第一個與最后一個交換咨演。

public class Prog31{? ? public static void main(String[] args){? ? ? ? int[] A = new int[]{1,2,3,4,5,6,7,8,9,};? ? ? ? print(A);? ? ? ? System.out.println();? ? ? ? int[] B = reverse(A);? ? ? ? print(B);? ? }? ? private static int[] reverse(int[] A){? ? ? ? for(int i=0;i

32.取一個整數(shù)a從右端開始的4~7位。

程序分析:可以這樣考慮:

(1)先使a右移4位蚯斯。(2)設(shè)置一個低4位全為1,其余全為0的數(shù)薄风。可用~(~0<<4)(3)將上面二者進(jìn)行&運算拍嵌。import java.util.Scanner;public class Prog32{? ? public static void main(String[] msg){? ? ? ? //輸入一個長整數(shù)? ? ? ? Scanner scan = new Scanner(System.in);? ? ? ? long l = scan.nextLong();? ? ? ? scan.close();? ? ? ? //以下截取字符? ? ? ? String str = Long.toString(l);? ? ? ? char[] ch = str.toCharArray();? ? ? ? int n = ch.length;? ? ? ? if(n<7)? ? ? ? ? System.out.println("輸入的數(shù)小于7位遭赂!");? ? ? ? else? ? ? ? ? System.out.println("截取的4~7位數(shù)字:"+ch[n-7]+ch[n-6]+ch[n-5]+ch[n-4]);? ? ? ? }? ? ? }

33.打印出楊輝三角形(要求打印出10行如下圖)

程序分析:

1

1 1

1 2 1

1 3 3 1

1 4 6 4 1

1 5 10 10 5 1

public class Prog33{? ? public static void main(String[] args){? ? ? ? int[][] n = new int[10][21];? ? ? ? n[0][10] = 1;? ? ? ? for(int i=1;i<10;i++)? ? ? ? ? for(int j=10-i;j<10+i+1;j++)? ? ? ? ? ? n[i][j] = n[i-1][j-1]+n[i-1][j+1];? ? ? ? for(int i=0;i<10;i++){? ? ? ? ? ? for(int j=0;j<21;j++){? ? ? ? ? ? ? ? if(n[i][j]==0)? ? ? ? ? ? ? ? ? System.out.print("? ");? ? ? ? ? ? ? ? else{? ? ? ? ? ? ? ? if(n[i][j]<10)? ? ? ? ? ? ? ? ? System.out.print("? "+n[i][j]);//空格為了美觀需要? ? ? ? ? ? ? ? else if(n[i][j]<100)? ? ? ? ? ? ? ? ? System.out.print(" "+n[i][j]);? ? ? ? ? ? ? ? ? else? ? ? ? ? ? ? ? ? ? System.out.print(n[i][j]);? ? ? ? ? ? ? }? ? ? ? ? ? }? ? ? ? ? ? System.out.println();? ? ? ? }? ? }}

34.輸入3個數(shù)a,b,c,按大小順序輸出横辆。

程序分析:利用指針方法撇他。

import java.util.Scanner;public class Prog34{? ? public static void main(String[] args){? ? ? ? System.out.print("請輸入3個數(shù):");? ? ? ? Scanner scan = new Scanner(System.in).useDelimiter("\\s");? ? ? ? int a = scan.nextInt();? ? ? ? int b = scan.nextInt();? ? ? ? int c = scan.nextInt();? ? ? ? scan.close();? ? ? ? if(a

35.選擇排序

輸入數(shù)組,最大的與第一個元素交換狈蚤,最小的與最后一個元素交換困肩,輸出數(shù)組。

import java.util.Scanner;public class Prog35{? ? public static void main(String[] args){? ? ? ? System.out.print("請輸入一組數(shù):");? ? ? ? Scanner scan = new Scanner(System.in).useDelimiter("\\s");? ? ? ? int[] a = new int[50];? ? ? ? int m = 0;? ? ? ? while(scan.hasNextInt()){? ? ? ? ? ? a[m++] = scan.nextInt();? ? ? ? }? ? ? ? scan.close();int[] b = new int[m];? ? ? ? for(int i=0;i

36.交換位置

有n個整數(shù)炫惩,使其前面各數(shù)順序向后移m個位置僻弹,最后m個數(shù)變成最前面的m個數(shù)

import java.util.Scanner;public class Prog36{? ? public static void main(String[] args){? ? ? ? final int N = 10;? ? ? ? System.out.print("請輸入10個數(shù)的數(shù)組:");? ? ? ? Scanner scan = new Scanner(System.in);? ? ? ? int[] a = new int[N];? ? ? ? for(int i=0;i

37.排序問題

有n個人圍成一圈,順序排號他嚷。從第一個人開始報數(shù)(從1到3報數(shù))蹋绽,凡報到3的人退出圈子,問最后留下的是原來第幾號的那位筋蓖。

import java.util.Scanner;public class Prog37{? ? public static void main(String[] args){? ? ? ? System.out.print("請輸入一個整數(shù):");? ? ? ? Scanner scan = new Scanner(System.in);? ? ? ? int n = scan.nextInt();? ? ? ? scan.close();? ? ? ? //定義數(shù)組變量標(biāo)識某人是否還在圈內(nèi)? ? ? ? boolean[] isIn = new boolean[n];? ? ? ? for(int i=0;i1){? ? ? ? ? ? if(isIn[index]){? ? ? ? ? ? ? ? countNum++;? ? ? ? ? ? ? ? if(countNum==3){? ? ? ? ? ? ? ? ? ? countNum = 0;? ? ? ? ? ? ? ? ? ? isIn[index] = false;? ? ? ? ? ? ? ? ? ? inCount--;? ? ? ? ? ? ? ? }? ? ? ? ? ? }? ? ? ? ? ? index++;? ? ? ? ? ? if(index==n)? ? ? ? ? ? ? index = 0;? ? ? ? }? ? ? ? for(int i=0;i

38.計算字符串總長度

寫一個函數(shù)卸耘,求一個字符串的長度,在main函數(shù)中輸入字符串粘咖,并輸出其長度蚣抗。

import java.util.Scanner;public class Prog38{? ? public static void main(String[] args){? ? ? ? System.out.print("請輸入一串字符:");? ? ? ? Scanner scan = new Scanner(System.in).useDelimiter("\\n");? ? ? ? String strIn = scan.next();? ? ? ? scan.close();? ? ? ? char[] ch = strIn.toCharArray();? ? ? ? System.out.println(strIn+"共"+(ch.length-1)+"個字符");? ? }}

39.求和

編寫一個函數(shù),輸入n為偶數(shù)時瓮下,調(diào)用函數(shù)求1/2+1/4+...+1/n,當(dāng)輸入n為奇數(shù)時翰铡,調(diào)用函數(shù)1/1+1/3+...+1/n(利用指針函數(shù))

import java.util.Scanner;public class Prog39{? ? public static void main(String[] args){? ? ? ? System.out.print("請輸入一個整數(shù):");? ? ? ? Scanner scan = new Scanner(System.in);? ? ? ? int n = scan.nextInt();? ? ? ? scan.close();? ? ? ? if(n%2==0)? ? ? ? ? System.out.println("結(jié)果:"+even(n));? ? ? ? else? ? ? ? ? System.out.println("結(jié)果:"+odd(n));? ? }? ? //奇數(shù)? ? static double odd(int n){? ? ? ? double sum = 0;? ? ? ? for(int i=1;i

40.字符串排序钝域。

public class Prog40{? ? public static void main(String[] args){? ? ? ? String[] str = {"abc","cad","m","fa","f"};? ? ? ? for(int i=str.length-1;i>=1;i--){? ? ? ? ? ? for(int j=0;j<=i-1;j++){? ? ? ? ? ? ? ? if(str[j].compareTo(str[j+1])<0){? ? ? ? ? ? ? ? ? ? String temp = str[j];? ? ? ? ? ? ? ? ? ? str[j] = str[j+1];? ? ? ? ? ? ? ? ? ? str[j+1] = temp;? ? ? ? ? ? ? ? }? ? ? ? ? ? }? ? ? ? }? ? ? ? for(String subStr:str)? ? ? ? ? System.out.print(subStr+" ");? ? }}

41.遞歸

海灘上有一堆桃子,五只猴子來分锭魔。第一只猴子把這堆桃子憑據(jù)分為五份例证,多了一個,這只猴子把多的一個扔入海中迷捧,拿走了一份织咧。第二只猴子把剩下的桃子又平均分成五份,又多了一個漠秋,它同樣把多的一個扔入海中笙蒙,拿走了一份,第三庆锦、第四捅位、第五只猴子都是這樣做的,問海灘上原來最少有多少個桃子搂抒?

public class Prog41{? ? public static void main(String[] args){? ? ? ? int n;? ? ? ? n = fun(0);? ? ? ? System.out.println("原來有"+n+"個桃子");? ? }? ? private static int fun(int i){? ? ? ? if(i==5)? ? ? ? ? return 1;? ? ? ? else? ? ? ? ? return fun(i+1)*5+1;? ? }}

42.809??=800??+9*??+1

其中??代表的兩位數(shù),8??的結(jié)果為兩位數(shù)绿渣,9??的結(jié)果為3位數(shù)。求??代表的兩位數(shù)燕耿,及809*??后的結(jié)果。

public class Prog42{? ? public static void main(String[] args){? ? ? ? int n = 0;? ? ? ? boolean flag = false;? ? ? ? for(int i=10;i<100;i++)? ? ? ? ? if(809*i==800*i+9*i+1){? ? ? ? ? ? ? flag = true;? ? ? ? ? ? ? n = i;? ? ? ? ? ? ? break;? ? ? ? ? }? ? ? ? if(flag)? ? ? ? ? System.out.println(n);? ? ? ? else? ? ? ? ? System.out.println("無符合要求的數(shù)姜胖!");? ? }}

43.求0—7所能組成的奇數(shù)個數(shù)誉帅。

public class Prog43{? ? public static void main(String[] args){? ? ? ? int count = 0;? ? ? ? //聲明由數(shù)字組成的數(shù)? ? ? ? int n = 8;? ? ? ? //一位數(shù)? ? ? ? count = n/2;? ? ? ? //兩位數(shù)? ? ? ? count += (n-1)*n/2;? ? ? ? //三位數(shù)? ? ? ? count += (n-1)*n*n/2;? ? ? ? //四位數(shù)? ? ? ? count += (n-1)*n*n*n/2;? ? ? ? //五位數(shù)? ? ? ? count += (n-1)*n*n*n*n/2;? ? ? ? //六位數(shù)? ? ? ? count += (n-1)*n*n*n*n*n/2;? ? ? ? //七位數(shù)? ? ? ? count += (n-1)*n*n*n*n*n*n/2;? ? ? ? System.out.println("0-7所能組成的奇數(shù)個數(shù):"+count);? ? }}

44.一個偶數(shù)總能表示為兩個素數(shù)之和。

import java.util.Scanner;public class Prog44{? ? public static void main(String[] args){? ? ? ? System.out.print("請輸入一個偶數(shù):");? ? ? ? Scanner scan = new Scanner(System.in);? ? ? ? int n = scan.nextInt();? ? ? ? scan.close();? ? ? ? if(n%2!=0){? ? ? ? ? System.out.println("您輸入的不是偶數(shù)右莱!");? ? ? ? ? return;? ? ? ? }? ? ? ? twoAdd(n);? ? }? ? //偶數(shù)分解為素數(shù)之和? ? private static void twoAdd(int n){? ? ? ? for(int i=2;i

45.判斷一個素數(shù)能被幾個9整除

import java.util.Scanner;public class Prog45{? ? public static void main(String[] args){? ? ? ? System.out.print("請輸入一個數(shù):");? ? ? Scanner scan = new Scanner(System.in);? ? ? long l = scan.nextLong();? ? ? long n = l;? ? ? scan.close();? ? ? int count = 0;? ? ? while(n>8){? ? ? ? ? n /= 9;? ? ? ? ? count++;? ? ? }? ? ? System.out.println(l+"能被"+count+"個9整除蚜锨。");? ? }}

46.兩個字符串連接程序

public class Prog46{? ? public static void main(String[] args){? ? ? ? String str1 = "lao lee";? ? ? String str2 = "牛刀";? ? ? String str = str1+str2;? ? ? System.out.println(str);? ? }}

47.打印練習(xí)

讀取7個數(shù)(1—50)的整數(shù)值,每讀取一個值慢蜓,程序打印出該值個數(shù)的*亚再。

import java.util.Scanner;public class Prog47{? ? public static void main(String[] args){? ? ? ? System.out.print("請輸入7個整數(shù)(1-50):");? ? ? ? Scanner scan = new Scanner(System.in);? ? ? ? int n1 = scan.nextInt();? ? ? ? int n2 = scan.nextInt();? ? ? ? int n3 = scan.nextInt();? ? ? ? int n4 = scan.nextInt();? ? ? ? int n5 = scan.nextInt();? ? ? ? int n6 = scan.nextInt();? ? ? ? int n7 = scan.nextInt();? ? ? ? scan.close();? ? ? ? printStar(n1);? ? ? ? printStar(n2);? ? ? ? printStar(n3);? ? ? ? printStar(n4);? ? ? ? printStar(n5);? ? ? ? printStar(n6);? ? ? ? printStar(n7);? ? }? ? static void printStar(int m){? ? ? ? System.out.println(m);? ? ? ? for(int i=0;i

48.加密算法

某個公司采用公用電話傳遞數(shù)據(jù),數(shù)據(jù)是四位的整數(shù)晨抡,在傳遞過程中是加密的氛悬,加密規(guī)則如下:每位數(shù)字都加上5,然后用和除以10的余數(shù)代替該數(shù)字,再將第一位和第四位交換耘柱,第二位和第三位交換如捅。

public class Prog48{? ? public static void main(String[] args){? ? ? ? int n = 1234;? ? ? ? int[] a = new int[4];? ? ? ? for(int i=3;i>=0;i--){? ? ? ? ? a[i] = n%10;? ? ? ? ? n /= 10;? ? ? ? }? ? ? ? for(int i=0;i<4;i++)? ? ? ? ? System.out.print(a[i]);? ? ? ? System.out.println();? ? ? ? for(int i=0;i

49.計算字符串中子串出現(xiàn)的次數(shù)

public class Prog49{? ? public static void main(String[] args){? ? ? ? String str = "I come from County DingYuan Province AnHui.";? ? ? ? char[] ch = str.toCharArray();? ? ? ? int count = 0;? ? ? ? for(int i=0;i

50.求平均數(shù)

有五個學(xué)生,每個學(xué)生有3門課的成績调煎,從鍵盤輸入以上數(shù)據(jù)(包括學(xué)生號镜遣,姓名,三門課成績)士袄,計算出平均成績悲关,將原有的數(shù)據(jù)和計算出的平均分?jǐn)?shù)存放在磁盤文件"stud"中谎僻。

import java.io.*;public class Prog50{? ? //定義學(xué)生模型? ? String[] number = new String[5];? ? String[] name = new String[5];? ? float[][] grade = new float[5][3];? ? float[] sum = new float[5];? ? public static void main(String[] args) throws Exception{? ? ? ? Prog50 stud = new Prog50();? ? ? ? stud.input();? ? ? ? stud.output();? ? }? ? //輸入學(xué)號、姓名寓辱、成績? ? void input() throws IOException{? ? ? ? BufferedReader br = new BufferedReader(new InputStreamReader(System.in));? ? ? ? //錄入狀態(tài)標(biāo)識? ? ? ? boolean isRecord = true;? ? ? ? while(isRecord){? ? ? ? ? ? try{? ? ? ? ? ? ? for(int i=0;i<5;i++){? ? ? ? ? ? ? ? ? System.out.print("請輸入學(xué)號:");? ? ? ? ? ? ? ? ? number[i] = br.readLine();? ? ? ? ? ? ? ? ? System.out.print("請輸入姓名:");? ? ? ? ? ? ? ? ? name[i] = br.readLine();? ? ? ? ? ? ? ? ? for(int j=0;j<3;j++){? ? ? ? ? ? ? ? ? ? ? System.out.print("請輸入第"+(j+1)+"門課成績:");? ? ? ? ? ? ? ? ? ? ? grade[i][j] = Integer.parseInt(br.readLine());? ? ? ? ? ? ? ? ? }? ? ? ? ? ? ? ? ? System.out.println();? ? ? ? ? ? ? ? ? sum[i] = grade[i][0]+grade[i][1]+grade[i][2];? ? ? ? ? ? ? }? ? ? ? ? ? ? ? isRecord = false;? ? ? ? ? ? }catch(NumberFormatException e){? ? ? ? ? ? ? ? System.out.println("請輸入一個數(shù)字艘绍!");? ? ? ? ? }? ? ? ? }? ? }? ? //輸出文件? ? void output() throws IOException{? ? ? ? FileWriter fw = new FileWriter("E://java50//stud.txt");? ? ? ? BufferedWriter bw = new BufferedWriter(fw);? ? ? ? ? ? bw.write("No.? "+"Name? "+"grade1? "+"grade2? "+"grade3? "+"average");? ? ? ? bw.newLine();? ? ? ? for(int i=0;i<5;i++){? ? ? ? ? bw.write(number[i]);? ? ? ? ? bw.write("? "+name[i]);? ? ? ? ? for(int j=0;j<3;j++)? ? ? ? ? ? bw.write("? "+grade[i][j]);? ? ? ? ? bw.write("? "+(sum[i]/5));? ? ? ? ? bw.newLine();? ? ? ? }? ? ? ? bw.close();? ? }}

點此【IT學(xué)習(xí)傳送門】免費課程試聽

原作者姓名:小路飛

原出處:掘金

原文鏈接:50道Java基礎(chǔ)編程練習(xí)題

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市讶舰,隨后出現(xiàn)的幾起案子鞍盗,更是在濱河造成了極大的恐慌,老刑警劉巖跳昼,帶你破解...
    沈念sama閱讀 218,755評論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件般甲,死亡現(xiàn)場離奇詭異,居然都是意外死亡鹅颊,警方通過查閱死者的電腦和手機敷存,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,305評論 3 395
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來堪伍,“玉大人锚烦,你說我怎么就攤上這事〉酃停” “怎么了涮俄?”我有些...
    開封第一講書人閱讀 165,138評論 0 355
  • 文/不壞的土叔 我叫張陵,是天一觀的道長尸闸。 經(jīng)常有香客問我彻亲,道長,這世上最難降的妖魔是什么吮廉? 我笑而不...
    開封第一講書人閱讀 58,791評論 1 295
  • 正文 為了忘掉前任苞尝,我火速辦了婚禮,結(jié)果婚禮上宦芦,老公的妹妹穿的比我還像新娘宙址。我一直安慰自己,他們只是感情好调卑,可當(dāng)我...
    茶點故事閱讀 67,794評論 6 392
  • 文/花漫 我一把揭開白布抡砂。 她就那樣靜靜地躺著,像睡著了一般恬涧。 火紅的嫁衣襯著肌膚如雪舀患。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,631評論 1 305
  • 那天气破,我揣著相機與錄音聊浅,去河邊找鬼。 笑死,一個胖子當(dāng)著我的面吹牛低匙,可吹牛的內(nèi)容都是我干的旷痕。 我是一名探鬼主播,決...
    沈念sama閱讀 40,362評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼顽冶,長吁一口氣:“原來是場噩夢啊……” “哼欺抗!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起强重,我...
    開封第一講書人閱讀 39,264評論 0 276
  • 序言:老撾萬榮一對情侶失蹤绞呈,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后间景,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體佃声,經(jīng)...
    沈念sama閱讀 45,724評論 1 315
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,900評論 3 336
  • 正文 我和宋清朗相戀三年倘要,在試婚紗的時候發(fā)現(xiàn)自己被綠了圾亏。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,040評論 1 350
  • 序言:一個原本活蹦亂跳的男人離奇死亡封拧,死狀恐怖志鹃,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情泽西,我是刑警寧澤曹铃,帶...
    沈念sama閱讀 35,742評論 5 346
  • 正文 年R本政府宣布,位于F島的核電站捧杉,受9級特大地震影響铛只,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜糠溜,卻給世界環(huán)境...
    茶點故事閱讀 41,364評論 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望直撤。 院中可真熱鬧非竿,春花似錦、人聲如沸谋竖。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,944評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽蓖乘。三九已至锤悄,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間嘉抒,已是汗流浹背零聚。 一陣腳步聲響...
    開封第一講書人閱讀 33,060評論 1 270
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人隶症。 一個月前我還...
    沈念sama閱讀 48,247評論 3 371
  • 正文 我出身青樓政模,卻偏偏與公主長得像,于是被迫代替她去往敵國和親蚂会。 傳聞我的和親對象是個殘疾皇子淋样,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,979評論 2 355