简单的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));
    }
}

 

转载于:https://www.cnblogs.com/ld199848/p/8523403.html

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值