簡單的Java邏輯小代碼(打擂台,冒泡排序,水仙花數,迴文數,遞迴),java迴文

來源:互聯網
上載者:User

簡單的Java邏輯小代碼(打擂台,冒泡排序,水仙花數,迴文數,遞迴),java迴文
1.打擂台

簡單的小代碼,打擂台。紀念下過去,祝福下新人。

 1 public static void main(String[] args){ 2     int[] ld = {1,4,2,10,8,9,5}; 3     int max = ld[0]; 4     for(int i = 1;i<ld.length;i++){ 5         if(ld[i]>max){ 6             max=ld[i]; 7         } 8     } 9     System.out.print(max);10 }
2.冒泡排序

還是簡單的小代碼,冒泡排序。紀念下過去,祝福下新人。

 1 public static void main(String[] args){ 2     int[] ld = {6,10,2,4,11}; 3     int aa; 4     for(int i = 0;i<ld.length-1;i++){ 5         for(int j = 0;j<ld.length-1-i;j++){ 6             if(ld[j]>ld[j+1]){ 7                 aa=ld[j]; 8                 ld[j]=ld[j+1]; 9                 ld[j+1]=aa;10             }11         }12     }13     for(int a = 0;a<5;a++){14         System.out.println(ld[a]);15     }16 }
3.水仙花數

還是簡單的小代碼,水仙花數。紀念下過去,祝福下新人。

public static void main(String[] args) {    int i = 0;    System.out.print("水仙花數為:");    for(int a = 100;a<=999;a++){        int b = a%10;        int c = a/10%10;        int d = a/100;        int all = b*b*b+c*c*c+d*d*d;        if(all==a){            System.out.print(all+",");            i++;        }    }    System.out.println("一共有"+i+"個");}
4.迴文數

還是簡單的小代碼,迴文數。紀念下過去,祝福下新人。

public static void main(String[] args) {    String b = "哈呵呵哈";    for(int i = 0;i<b.length()/2;i++){        char first = b.charAt(0+i);        char list = b.charAt(b.length()-1-i);        if(first!=list){            System.out.print("不是迴文數");            break;        }else if(first==list&&i==(b.length()/2-1)){            System.out.print("是迴文數");        }    }}
5.遞迴

還是簡單的小代碼,遞迴。紀念下過去,祝福下新人。

public class Dg{    public int g(int d){        if(d==1){            return d;        }else{            return d*g(d-1);        }    }}
public class Test {    public static void main(String[] args){        Dg d = new Dg();        System.out.print(d.g(6));    }}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.