




版權(quán)說(shuō)明:本文檔由用戶(hù)提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、精品文檔精品文檔實(shí)驗(yàn) 2要求輸入兩個(gè)整數(shù),輸出這兩個(gè)整數(shù)的和、差、積、商import javax.swing.JOptionPane; / program uses JOptionPane public class P2 / main method begins execution of Java application public static void main( String args )String firstNumber;/ first string entered by userString secondNumber; / second string entered by use
2、rint number1;int number2;int he,cha,ji,shang;/ read in first number from user as a string firstNumber = JOptionPane.showInputDialog( Enterfirst integer );/ read in second number from user as a string secondNumber =JOptionPane.showInputDialog( Enter second integer );/ convert numbers from type String
3、 to type int number1 = Integer.parseInt( firstNumber );number2 = Integer.parseInt( secondNumber );he = number1 + number2;cha= number1 - number2;ji = number1 * number2; shang=number1 / number2;/ display resultSystem.out.println( 和是: + he+n 差是: +cha+n 積是: + ji+n 商是:JOptionPane.showMessageDialog( null,
4、和是:” + he+n 差是:+cha+n 積是: 商是: +shang,Results, JOptionPane.PLAIN_MESSAGE );System.exit( 0 );/ terminate application with window / end method main / end class Addition實(shí)驗(yàn) 31、 編寫(xiě)一個(gè)應(yīng)用程序,要求用戶(hù)輸入一個(gè)圓的半徑(double 類(lèi)型),然后計(jì)算并輸出圓的直徑、周長(zhǎng)、面積等信息 。(把一個(gè)字符串轉(zhuǎn)換為 double 類(lèi)型數(shù)據(jù)的方法為Double.parseDouble(String s) )import javax.swin
5、g.JOptionPane;import java.text.DecimalFormat;+shang); + ji+n精品文檔精品文檔public class P3_1public static void main(String args)String r;r=JOptionPane.showInputDialog( 請(qǐng)輸入一個(gè)圓的半徑: );double radius;radius=Double.parseDouble(r);double zhijing,l,s;final double P=Math.PI;zhijing=2*radius;l=2*P*radius;s=P*Math.po
6、w(radius,2);DecimalFormat f=new DecimalFormat(0.00);/System.out.println ( 圓的半徑是: +radius+n+ 直徑是: +zhijing+n 周長(zhǎng)是:+f.format(l)+n 面積是: +f.format(s);JOptionPane.showMessageDialog(null, 圓 的 半 徑 是 : +radius+n+ 直 徑 是 :+zhijing+n 周長(zhǎng)是: +f.format(l)+n 面積是: +f.format(s),顯示結(jié)果 ,JOptionPane.INFORMATION_MESSAGE);2
7、、編寫(xiě)一個(gè)應(yīng)用程序,要求用戶(hù)輸入兩個(gè)整數(shù),然后輸出兩個(gè)整數(shù)中的最大值。import javax.swing.JOptionPane;public class P3_2public static void main(String args)String s1,s2;s1=JOptionPane.showInputDialog( 請(qǐng)輸入第一個(gè)整數(shù): );s2=JOptionPane.showInputDialog( 請(qǐng)輸入第二個(gè)整數(shù): );int n1,n2;n1=Integer.parseInt(s1);n2=Integer.parseInt(s2);int max;max=n1n2?n1:n2
8、;System.out.println (n1+ 和 +n2+ 的最大值是: +max); 實(shí)驗(yàn)四1、 輸入一個(gè)年份, 一個(gè)月份(使用一個(gè) BufferedReader ),判斷該年該月有多少天 (使用 switch, 注意要判斷 2 月是多少天)import java.io.*;精品文檔精品文檔public class P4_1public static void main(String args)throws IOExceptionString s1,s2;int year,month;BufferedReader br=new BufferedReader(new InputStream
9、Reader(System.in);System.out.print( 請(qǐng)輸入一個(gè)年份: );s1=br.readLine();System.out.print ( 請(qǐng)輸入一個(gè)月份: );s2=br.readLine();year=Integer.parseInt(s1); month=Integer.parseInt(s2);switch(month)case 1:case 3:case 5:case 7:case 8:case 10:case 12:System.out.println (year+年+month+月+有 31 天”);break;case 4:case 6:case 9:
10、case 11:System.out.println (year+年+month+月+有 30 天); break;case 2: if(year%4=0&year%100!=0)|(year%400=0)System.out.println (year+ 年 +month+ 月+ 有 29 天 ); elseSystem.out.println (year+ 年 +month+ 月+ 有 28 天 ); break;default: System.out.println ( 你輸入的月份是錯(cuò)誤的! );2、 如果一個(gè)數(shù)按反向順序放置后仍然與原數(shù)相等,稱(chēng)為回文數(shù)(如:12321 )。編
11、程:輸入一個(gè) 5 位數(shù),判斷此數(shù)是否為回文數(shù)。import java.io.*;public class P4_2public static void main(String args)throws IOException精品文檔精品文檔BufferedReader br=new BufferedReader(new InputStreamReader(System.in);System.out.print( 請(qǐng)輸入一個(gè) 5 位數(shù): );String s1=br.readLine();int n=Integer.parseInt(s1);int n1,n2,n3,n4,n5; n1=n/100
12、00;/n2=(n-10000*n1)/1000; n2=n%10000/1000; n3=n%1000/100; n4=n%100/10;n5=n%10;if(n1=n5&n2=n4)System.out.println (n+ 是回文數(shù)。 );elseSystem.out.println (n+ 不是回文數(shù)。 );實(shí)驗(yàn) 53、求 S=a+aa+aaa+aaaaa 之值,其中 a 是一個(gè)數(shù)字。例如:2+22+222+2222+22222 (此時(shí) n=5) , a 和 n 要求用戶(hù)由鍵盤(pán)輸入。(有規(guī)律的式子求和,注意找到前后兩項(xiàng)之間的關(guān)系,該題中后一項(xiàng)等于前一項(xiàng)的 10 倍加 a)im
13、port java.io.*;public class P5_1public static void main(String args)throws IOExceptionBufferedReader br=new BufferedReader(new InputStreamReader(System.in);String s1,s2;System.out.print( 請(qǐng)輸入一個(gè) 1-9 的數(shù)字: );s1=br.readLine();System.out.print( 請(qǐng)輸入項(xiàng)數(shù): );s2=br.readLine();int a,n;a=Integer.parseInt(s1);n=In
14、teger.parseInt(s2);int p=0,sum=0;for(int i=1;i=n;i+)p=p*10+a;sum+=p;for(int i=1;i=n;i+)for(int j=1;j=i;j+)精品文檔精品文檔System.out.print(a);if(in)System.out.print(+);else if(i=n)System.out.print(=);System.out.println (sum);4.編寫(xiě)一個(gè)應(yīng)用程序,要求用戶(hù)輸入 5 個(gè)整數(shù)(使用循環(huán)結(jié)構(gòu)) ,輸出這五個(gè)整數(shù)的最大值和最小值。import java.io.*;public class P5_2
15、public static void main(String args)throws IOExceptionBufferedReader br=new BufferedReader(new InputStreamReader(System.in);String s;int max=Integer.MIN_V ALUE,min=Integer.MAX_V ALUE;int i=1;while(imax)max=n;if(n=0;i-)精品文檔精品文檔s+=a.eharAt(i);return s;publie statie void reverse(String a)int n=a.length
16、();for(int i=n-1;i=0;i-)System.out.print(a.charAt(i);System.out.println ();public static void main(String args)throws IOExceptionBufferedReader br=new BufferedReader(new InputStreamReader(System.in);String s;doSystem.out.print( 請(qǐng)輸入一個(gè)字符串: ); s=br.readLine();if(s.equals(0)break;elseP6_1.reverse(s); wh
17、ile(s.equals(0)=false);2、編寫(xiě)一個(gè)靜態(tài)方法用于求任意兩個(gè)正整數(shù)的最大公約數(shù),調(diào)用此方法求16 和 24 的最大公約數(shù)。備注:求最大公約數(shù)使用輾轉(zhuǎn)相除法,我國(guó)古代數(shù)學(xué)家秦九韶 1247 年在數(shù)書(shū)九章中記 載了此方法,其處理過(guò)程如下:( 1)提供兩個(gè)數(shù) m 和 n(2) 以 n 除 m,求得余數(shù) r (r=m%n )(3)判斷 r 是否為 0,若 r=0 ,此時(shí)的 n 值即為最大公約數(shù),計(jì)算結(jié)束。若工 0,更新被除數(shù)和除數(shù),n 送 m (即 m=n),r 送 n (即 n= r),轉(zhuǎn)到(2)。public class P6_2public static int factor
18、(int n,int m)int r=m%n;while(r!=0)m=n;n=r;r=m%n;return n;public static void main(String args)精品文檔精品文檔int n=P6_2.factor(16,24);System.out.println (n);實(shí)驗(yàn)七1,編寫(xiě)遞歸方法 gcd,返回 x 和 y 的最大公約數(shù)。x 和 y 的最大公約數(shù)方法 gcd 的遞歸定義 如下:如果 y等于 0,則 gcd (x,y)就是 x;否則,gcd (x,y)就等于 gcd(y,x%y),其中%” 是求模運(yùn)算符。public class P7_1public sta
19、tic int gcd(int x,int y)if(y=0)return x;elsereturn gcd(y,x%y);public static void main(String args)int k=10,m=80;int n=gcd(k,m);System.out.println (n);2,編寫(xiě)遞歸方法getPower(int x,int y),用于計(jì)算 x 的 y 次幕,在 main 主方法中調(diào)用它求 2的 10 次冪。public class P7_2public static int getPower(int x,int y)if(y=1)return x;elseretur
20、n x*getPower(x,y-1);public static void main(String args)System.out.println (P7_2.getPower(2,10);3、n 從鍵盤(pán)輸入,n 是一個(gè)小于 10 的數(shù)。編寫(xiě)一個(gè)方法 public static void shuChu(int n)用循 環(huán)語(yǔ)句輸出如下圖所示的 n 行三角形圖形。 提示: 使用字母的 ACSII 碼輸出該字母, 字母 A 對(duì)應(yīng) 65,字母 B對(duì)應(yīng) 66ABBBCCCCCDDDDDDDimport java.io.*;public class P7_3精品文檔精品文檔public static
21、void shuChu(int n)for(int i=1;i=n;i+)for(int j=1;j=n-i;j+)System.out.print( );for(int j=1;j=2*i-1;j+)System.out.print(char)(64+i);System.out.println ();public static void main(String args)throws IOExceptionBufferedReader br=new BufferedReader(new InputStreamReader(System.in);System.out.print( 請(qǐng)輸入 n
22、的值: );String s=br.readLine();int n=Integer.parseInt(s);P7_3.shuChu(n);實(shí)驗(yàn)八1、 定義一個(gè)數(shù)組來(lái)存儲(chǔ) 12 個(gè)學(xué)生的成績(jī) 72,89,65,58,87,91,53,82,71,93,76,68 ,計(jì)算并 輸出學(xué)生的平均成績(jī)。 (要求:計(jì)算平均成績(jī)的過(guò)程定義為方法)import java.text.DecimalFormat;public class P8_1static double ave(int n)int sum=0;for(int i=0;in.length;i+)sum=sum+ni;double a=(doubl
23、e)(sum)/n.length;DecimalFormat f=new DecimalFormat(0.00);return Double.parseDouble(f.format(a);public static void main(String args)int score=72,89,65,58,87,91,53,82,71,93,76,68;System.out.println (P8_1.ave(score);2、 定義一個(gè)數(shù)組來(lái)存儲(chǔ) 12 個(gè)學(xué)生的成績(jī) 72,89,65,58,87,91,53,82,71,93,76,68 ,統(tǒng)計(jì)各 成績(jī)等級(jí)(90 分以上為 A 8089 分為
24、B 7079 分為 C 6069 分為 D 60 分以下為 E)學(xué)生人數(shù),并將其放入到數(shù)組count 中,其中:countO存 E 級(jí)的人數(shù),count1存 D 級(jí)的人數(shù),.,count4存 A 級(jí)的人數(shù)。(提示:注意要找到各分?jǐn)?shù)段同數(shù)組 count 的下標(biāo)的關(guān)系) public class P8_2public static void main(String args)精品文檔精品文檔int score=72,89,65,58,87,91,53,82,71,93,76,68;int count=new int5;for(int i=0;iscore.length;i+)if(scorei=1
25、00)count4+;else if(scorei60)count0+;elsecountscorei/10-5+;for(int i=0;icount.length;i+)System.out.println (char)(69-i)+t+counti);3、從鍵盤(pán)輸入 10 個(gè)整數(shù), 將奇數(shù)和偶數(shù)分別存入到兩個(gè)不同的數(shù)組中, 并按奇數(shù)、 偶 數(shù)交替的順序輸出這兩個(gè)數(shù)組中的所有數(shù)據(jù)(先交替輸出,如果奇數(shù)個(gè)數(shù)多,則再 輸出剩下的奇數(shù),如果偶數(shù)個(gè)數(shù)多,則再輸出剩下的偶數(shù))。(提示與要求: (1)定 義一個(gè)數(shù)組存儲(chǔ)從鍵盤(pán)輸入的 10 個(gè)整數(shù),先判斷這 10 個(gè)整數(shù)中奇數(shù)和偶數(shù)的個(gè)數(shù), 才能定義存儲(chǔ)奇數(shù)和偶數(shù)的數(shù)組的長(zhǎng)度; ( 2)把分揀奇數(shù)和偶數(shù)并交替輸出的過(guò)程 定義為方法)import java.io.*;public class P8_3public static void output(int n)int even,odd;int a=0,b=0;for(int i=0;in.length;i+)if(ni%2=0) a+;else b+;even=new inta; odd=new intb;int j=0,k=0;for(int i=0;in.length;i+)if(ni%2=0)evenj
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶(hù)所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶(hù)上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶(hù)上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶(hù)因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 激光工程師資格考核題目試題及答案
- 藥劑類(lèi)考試技術(shù)提升試題及答案
- 藥物副作用的臨床管理試題及答案
- 藥品研發(fā)政策的影響因素試題及答案
- 激光技術(shù)的市場(chǎng)分析試題及答案
- 藥劑類(lèi)考試分析報(bào)告試題及答案
- 藥學(xué)士考試試題及答案
- 考研鋼琴筆試題目及答案
- 塔吊技能考試題及答案
- 文化產(chǎn)業(yè)管理證書(shū)考查范圍試題及答案
- 兼職勞務(wù)協(xié)議合同模板
- 2025年河南機(jī)電職業(yè)學(xué)院高職單招語(yǔ)文2019-2024歷年真題考點(diǎn)試卷含答案解析
- 2025年浙江長(zhǎng)征職業(yè)技術(shù)學(xué)院?jiǎn)握芯C合素質(zhì)考試題庫(kù)及答案1套
- 湖南新高考教學(xué)教研聯(lián)盟暨長(zhǎng)郡二十校聯(lián)盟2025屆高三年級(jí)第二次聯(lián)考?xì)v史試題及答案
- (二模)東北三省三校2025年高三第二次聯(lián)合模擬考試 英語(yǔ)試卷(含答案解析)
- 2025年吉林交通職業(yè)技術(shù)學(xué)院?jiǎn)握新殬I(yè)技能考試題庫(kù)新版
- 提高感染性休克集束化治療完成率工作方案
- 滸墅關(guān)鎮(zhèn)社區(qū)家長(zhǎng)學(xué)校工作臺(tái)帳(模板)
- 電子科技大學(xué)自主招生軟件工程碩士招生簡(jiǎn)章 —校外培養(yǎng)點(diǎn)
- 安全生產(chǎn)標(biāo)準(zhǔn)化創(chuàng)建工作啟動(dòng)會(huì)(PPT 87頁(yè))
- 【原創(chuàng)】幼兒園教師簡(jiǎn)筆畫(huà)創(chuàng)意與技能技巧
評(píng)論
0/150
提交評(píng)論