Java編程思想第四版 第四章個人練習,編程思想第四版

來源:互聯網
上載者:User

Java編程思想第四版 第四章個人練習,編程思想第四版
第四章練習1:(1)寫一個程式,列印1到100的值

public static void main(String[] args) {test(100);}public static void test(int num){int i=0;while(i<num){System.out.print(++i);if(i!=100){System.out.print("、");}}System.out.println();int j=0;do{System.out.print(++j);if(j!=100){System.out.print("、");}}while(j<100);System.out.println();for(int k=0;k<100;){System.out.print(++k);if(k!=100){System.out.print("、");}}}


練習2:(2)寫一個程式,產生25個int類型的隨機數,對於每一個隨機數,使用if-else語句來將其分類為大於、小於,或等於緊隨它而隨機產生的值。
public static void main(String[] args) {test();}public static void test(){int num[] =new int[25];for(int i=0;i<25;i++){Random r=new Random();int n=r.nextInt();num[i]=n;if(i>0){compare(num[i-1],num[i]);}}}public static void compare(int n1,int n2){//System.out.println(n1+"、"+n2+"\n n1>n2:"+(n1>n2)+"\t"+"n1==n2:"+(n1==n2)+"\t"+"n1<n2:"+(n1<n2));if(n1>n2){System.out.println(n1+"大於"+n2);}else if(n1<n2){System.out.println(n1+"小於"+n2);}else {System.out.println(n1+"等於"+n2);}}


練習3:(1)修改練習2,把代碼用一個while無限迴圈包括起來。然後運行它直至用鍵盤中斷其運行(通常是通過按Ctrl+c).
public static void main(String[] args) {test();}public static void test(){int num[] =new int[2];int i=0;boolean flag=false;while(true){Random r=new Random();int n=r.nextInt();if(flag){num[i]=n;compare(num[0],num[1]);}else{flag=true;num[i]=n;}i=i==0?1:0;}}public static void compare(int n1,int n2){//System.out.println(n1+"、"+n2+"\n n1>n2:"+(n1>n2)+"\t"+"n1==n2:"+(n1==n2)+"\t"+"n1<n2:"+(n1<n2));if(n1>n2){System.out.println(n1+"大於"+n2);}else if(n1<n2){System.out.println(n1+"小於"+n2);}else {System.out.println(n1+"等於"+n2);}}


練習4:(3)寫一個程式,使用兩個嵌套的for迴圈和取餘操作符(%)來探測和列印素數(只能被其自身和1整除,而不能被其它數字整除的整數)。
       public static void main(String[] args) {test(33);}public static boolean test(int num){for(int i=2;i<num;i++){if(num%i==0){System.out.println(num+" 不是素數");return true;}}System.out.println(num+" 是素數");return false;}



練習5:(4)重複第3章中的練習10,不要用Integer.toBinaryString()方法,而是用三元操作符和按位操作符來顯示二進位的1和0。

解答:JAVA編程思想第4版 第四章 練習5

這個問題,我也高度興趣,希望樓主得到答案以後 可以分享一下
 
誰有java編程思想第四版的練習答案

無人作答,還是加入關於JAVA的QQ群或是直接加入這書的群吧。他們會有答案。我也是愛好者,這是我的建議採納吧!
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.