JAVA定义类的三个方法编写循环找水仙花

通过定义类的三个方法来实现寻找水仙花(三种循环),水仙花数是三位数,它的各位数字的立方和等于这个三位数本身,如371=33+73+13。

首先,我们先分开写三种循环

1.For循环

public class DaffoNum {

    public static void main(String[] args) {

        for(int i=100; i<=999; i++) {
            int a = i % 10;
            int b = i / 10 % 10;
            int c = i / 100 % 10;

            if (a*a*a+b*b*b+c*c*c==i){
                System.out.println(i);
            }
        }
    }
}

2.while循环

public class DaffoNum1 {

    public static void main(String[] args) {

         int i=100;

        while (i < 1000) {

            int a = i % 10;
            int b = i / 10 % 10;
            int c = i / 100 % 10;


            if (a * a * a + b * b * b + c * c * c == i) {
                System.out.println(i);
            }
            i++;
        }
    }
}

3.do while循环

public class DaffoNum2 {

    public static void main(String[] args) {

        int i=100;

         do {
             int a = i % 10;
             int b = i / 10 % 10;
             int c = i / 100 % 10;

             if (a * a * a + b * b * b + c * c * c == i) {
                 System.out.println(i);
             }
             i++;
         }
            while (i < 1000);

        }
    }

方法是一段可以重复使用的代码,把重复使用的代码提取出来,放在一对大括号,并为这段代码起一个名字。

格式如下

修饰符 返回值类型 方法名(参数类型 参数名1,参数类型 参数名2...){

执行语句

return 返回值;

}

合体 之后如下

public class DaffoNum {

    public static void main(String[] args) {
        System.out.println("For循环");        //调用for方法
        FORxh();

        System.out.println("While循环");      //调用while方法
        whilexh();

        System.out.println("do while 循环");      //调用do while方法
        dowhilexh();

    }
//下面编写for循环的方法
    public static void FORxh(){
        for(int i=100; i<=999; i++) {
            int a = i % 10;
            int b = i / 10 % 10;
            int c = i / 100 % 10;

            if (a*a*a+b*b*b+c*c*c==i){
                System.out.println(i);
            }
        }

    }
//下面编写while循环的方法
    public static void whilexh(){
        int i=100;

        while (i < 1000) {

            int a = i % 10;
            int b = i / 10 % 10;
            int c = i / 100 % 10;


            if (a * a * a + b * b * b + c * c * c == i) {
                System.out.println(i);
            }
            i++;
        }

    }
//下面编写do while循环的方法
    public static void dowhilexh(){
        int i=100;

        do {
            int a = i % 10;
            int b = i / 10 % 10;
            int c = i / 100 % 10;

            if (a * a * a + b * b * b + c * c * c == i) {
                System.out.println(i);
            }
            i++;
        }
        while (i < 1000);
    }
}

新人编写 多多指教

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

CN001-1

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值