


下載本文檔
版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
1、實驗 9-1 Method 的使用 (二 )1. 將一個整數逆序輸出輸入一個正整數 repeat (0<repeat<10),做 repeat 次下列運算: 輸入一個整數,將它逆序輸出。要求定義并調用函數 reverse(number),它的功能是返回 number 的逆序數。例如 reverse(12345)的返 回值是 54321。例:括號內是說明輸入4 (repeat=4)123456 -100 -2 99輸出654321-1-299import class Test50009public static void main(String args) int ri,repeat
2、; long n, res;Scanner in=new Scanner; repeat=(); for(ri=1;ri<=repeat;ri+) n=();res=reverse(n);static long reverse(long number)int flag=1;long a=0,digit; if(number<0) flag=-1; number=-number; while(number>0) digit=number%10; / 分離出個位數字 a=a*10+digit; / 形成當前的逆序數 number=number/10;return flag*a;2
3、. 十進制轉換二進制輸入一個正整數 repeat (0<repeat<10),做 repeat 次下列運算:輸入 1 個正整數 n,將其轉換為二進制后輸出。要求定義并調用函數 dectobin(n) ,它的功能是輸出 n 的二進制。例如,調用 dectobin(10),輸出 1010 輸出語句: /t 為某位二進制數例:括號內是說明輸入:3 (repeat=3)151000輸出:111111001000import class Test50010public static void main(String args)int ri,repeat;int i,n;Scanner in=
4、new Scanner;repeat=(); for(ri=1;ri<=repeat;ri+) n=();dectobin(n);static void dectobin(int n)String t="" / 保存二進制數 do t=n%2+t; /n 除 2 后的余數拼接到 t 的前面 n=n/2; / 獲得除 2 后的商while(n>0);/ 本方法無返回值,需要在方法體中輸出結果說明:本題中方法 dectobin(n) 的輸出雖然與要求有所出入,但上傳是正確的3. 用函數求三個數的最大值輸入一個正整數 repeat (0<repeat<10
5、),做 repeat 次下列運算:輸入三個整數 a、 b 和 c,輸出其中較大的數。要求定義和調用函數 max(a, b, c)找出 a、b、c 中較大 的數,函數形參 a、b 和 c 的類型是 int 。輸入輸出示例:括號內是說明輸入3(repeat=3)輸入:5 8 9(a=5, b=8)-1 -10-5 (a=-1, b=-10)1 1 1(a=1, b=1)輸出: max(5,8,9)=9 max(-1,-10,-5)=-1 max(1,1,1)=1import class Test50011 public static void main(String args) int ri, r
6、epeat;int a,b,c,maximun;Scanner in=new Scanner; repeat=();for (ri=1; ri<= repeat; ri+) a=();b=();c=();maximun=maximun(a,b,c);"max("+a+","+b+","+c+")="+maximun);static int maximun(int a,int b,int c) int max; max=a;if(max<b)max=b;if(max<c)max=c;return m
7、ax;實驗 9-2 一維數組的使用4. 求平均值 輸入一個正整數 repeat (0<repeat<10),做 repeat 次下列運算: 輸入一個正整數 n (1<n 1,0)再輸入 n 個整數,輸出平均值。例:括號內是說明輸入2 (repeat=2)3 1 2 -65 12 2 5 4 0 輸出 aver= aver=import class Test60001public static void main(String args)int ri, repeat;int i, n, sum,a; float aver;Scanner in=new Scanner;repea
8、t=();for(ri=1; ri<=repeat; ri+)n=();a=new intn; for(i=0; i<n; i+) ai=(); / 這個循環輸入數組各元素sum=0;for(i=0; i<n; i+) / 這個循環實現累加 sum+=ai;aver=(float)sum/n; / 求平均值,注意要先把 sum 轉換成 float ,再計算"aver="+aver);5. 求最大值及其下標0 開始)輸入一個正整數 repeat (0<repeat<10),做 repeat 次下列運算:輸入一個正整數 n (1<n 1,0)
9、再輸入 n 個整數,輸出最大值極其下標(設最大值惟一,下標從 例:括號內是說明輸入3 (repeat=3)3 1 6 4 3 10 8 1(最大值 6 的下標是 1)(最大值 10 的下標是 0)(最大值 5 的下標是 2)5 1 2 5 4 0 輸出 max=6,index=1 max=10,index=0 max=5,index=2 import class Test60002public static void main(String args) int ri, repeat;int i, index, n, a;Scanner in=new Scanner; repeat=();for
10、(ri=1; ri<=repeat; ri+) n=(); a=new intn ; for(i=0; i<n; i+) ai=();index=0; / 用 index 保存最大數的下標,開始假設 a0 是最大數 for(i=1; i<n; i+) if(aindex<ai) index=i;/ai 與當前最大數 aindex比較,若 ai更大, index變為 i"max="+aindex+",index="+index);6. 逆序輸出 輸入一個正整數 repeat (0<repeat<10),做 repeat
11、次下列運算: 輸入一個正整數 n (1<n 1,0)再輸入 n 個整數,按逆序輸出這些數。例:括號內是說明 輸入2 (repeat=2)4 10 8 1 25 1 2 5 4 0 輸出2 1 8 100 4 5 2 1import class Test60003public static void main(String args) int ri, repeat;int i, n, temp,a;Scanner in=new Scanner; repeat=();for(ri=1; ri<=repeat; ri+) n=();a=new intn; for(i=0; i<n;
12、 i+) ai=();for(i=0; i<n/2; i+) /ai 與 an-1-i 交換,注意交換次數 temp=ai;ai=an-1-i; an-1-i=temp;for(i=0; i<n; i+);7. 交換最小值和最大值輸入一個正整數 repeat (0<repeat<10),做 repeat 次下列運算:輸入一個正整數 n,再輸入 n 個整數,將最小值與第一個數交換,最大值與最后一個數交換,然后輸出 交換后的 n 個數。例:括號內是說明 輸入3 (repeat=3)5 4 3 5 1 24 1 5 6 75 5 4 3 2 1輸出1 3 2 4 51 5 6 71 4 3 2 5 import class Test60004public static void main(String args) int ri, repeat;int i, index, n, t,a;Scanner in=new Scanner; repeat=();for(ri=1; ri<=repeat; ri+) n=();a=new intn;for(i=0; i<n; i+) ai=();index=0; / 找最小數for(i=0; i<n; i+)if(aindex>ai) index=i;t
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
- 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025-2030中國家庭安全攝像頭行業市場發展趨勢與前景展望戰略研究報告
- 2025-2030中國姿勢圖測試裝置行業市場發展趨勢與前景展望戰略研究報告
- 2025-2030無水氯化鈣行業發展分析及投資戰略研究報告
- 2025-2030低能量食品行業市場發展分析及前景趨勢與投資研究報告
- 2025-2030中國鉀長石市場供需調查與發展趨勢預測研究報告
- 2025-2030中國新型城鎮化建設行業市場深度調研及投資研究報告
- 2025-2030中國導染劑行業投資前景研究及銷售戰略分析研究報告
- 2025-2030中國垃圾回收平臺市場前景預測及發展模式研究研究報告
- 室外熱適應行為導向下關中民居院落空間模式研究-以薛錄村典型院落為例
- 庫存管理分包合同
- 檔案管理實務基礎試題及答案
- 2025空壓機節能升級合同能源管理(EMC)項目合同
- 上海楊浦區社區工作者考試真題2024
- 2024桂林臨桂區中小學教師招聘考試試題及答案
- 2025年入團相關考試題型及答案
- 2023-2024學年北京市西城區德勝中學七年級(下)期中數學試卷
- 質控工具在護理管理中的應用
- 一年級不等式填數練習題
- 2025年糧油保管員職業技能資格知識考試題(附答案)
- 皮膚病靶向治療專家共識(2025版)解讀課件
- DB37-T 3274.3-2023 日光溫室建造技術規范 第3部分:山東VI型
評論
0/150
提交評論