Java-while语句案例题

while语句-案例题

···java

1、求1-100的和

public class tt1 {
    public static void main(String[] args) {
        //求1-100的和
        int i = 1, j = 0;
        while (i <= 100) {
            j = i + j;
            i++;
        }
        System.out.println("1-100的和为:" + j);
    }
}

2、求1-100奇数的和

public class tt2 {
    public static void main(String[] args) {
        //求1-100奇数的和
        int i = 1, j = 0;
        while (i % 2 != 0 && i <= 100) {
            j = i + j;
            i += 2;
        }
        System.out.println("1-100奇数和为:" + j);
    }
}

3、求1-100偶数的和

public class tt2 {
    public static void main(String[] args) {
        //求1-100奇数的和
        int i = 1, j = 0;
        while (i % 2 != 0 && i <= 100) {
            j = i + j;
            i += 2;
        }
        System.out.println("1-100奇数和为:" + j);
    }
}

4、求1-100中10的倍数的数值

public class tt4 {
    public static void main(String[] args) {
        //求1-100中10的倍数的数值
        int i = 1;
        while (i <= 100) {
            if (i % 10 == 0) {
                int j = i;
                System.out.println(j);
            }
            i++;
        }
    }
}

5、求1-100中奇数和与偶数和的差

public class tt5 {
    public static void main(String[] args) {
        //求1-100中奇数和与偶数和的差
        int i = 1, j = 0, z = 0;
        while (i <= 100) {
            if (i % 2 != 0) {
                j = i + j;
            } else {
                z = i + z;
            }
            i++;
        }
        System.out.println("1-100中奇数和与偶数和的差为:" + (j - z));

    }
}

6、求1-100中5的倍数的数值的个数

public class tt6 {
    public static void main(String[] args) {
        //求1-100中5的倍数的数值的个数
        int i = 0, j = 0;
        while (i <= 100) {
            if (i % 5 == 0) {
                j++;
            }
            i++;
        }
        System.out.println(j);
    }
}

7、求1-100中偶数的个数

public class tt7 {
    public static void main(String[] args) {
        //求1-100中偶数的个数
        int i = 0, j = 0;
        while (i <= 100) {
            if (i % 2 == 0) {
                j++;
            }
            i++;
        }
        System.out.println(j);
    }
}

8、求1-100中能被5和4整除的数值

public class tt8 {
    public static void main(String[] args) {
        //求1-100中能被5和4整除的数值
        int i = 1;
        while (i <= 100) {
            if (i % 5 == 0 && i % 4 == 0) {
                int j = i;
                System.out.println(j);
            }
            i++;
        }
    }
}

9、求1-100中的奇数,且能被3整除的数

public class tt9 {
    public static void main(String[] args) {
        //求1-100中的奇数,且能被3整除的数
        int i = 1;
        while (i <= 100) {
            if (i % 2 != 0 && i % 3 == 0) {
                int j = i;
                System.out.println(j);
            }
            i++;
        }
    }
}

10、求1-100中的偶数,且能被8整除的偶数的个数

public class tt10 {
    public static void main(String[] args) {
        //求1-100中的偶数,且能被8整除的偶数的个数
        int i = 1, j = 0;
        while (i <= 100) {
            if (i % 2 == 0 && i % 8 == 0) {
                j++;
            }
            i++;
        }
        System.out.println(j);
    }
}

···

                                                        仅供参考并非更好!
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值