第6天

循环
一、for循环

 

 

package 循环;

public class for循环 {
    public static void main(String[] args) {
        int sum = 0;
        for (int i = 1; i <= 100; i++) {
            if (i % 2 == 0) {
                sum += i;
            }
        }
        System.out.println(sum);
    }
}

结果

2550

二、while循环

package 循环;

import java.util.Scanner;

public class while循环 {
    public static void main(String[] args) {
        // 回文数
        Scanner sc = new Scanner(System.in);
        int i = sc.nextInt();
        int j = i;
        int x;
        int sum = 0;
        while (i != 0) {
            x = i % 10;
            i = i / 10;
            sum = sum * 10 + x;
        }
        if (sum == j) {
            System.out.println(j + "为回文数字");
        } else {
            System.out.println(j + "不是回文数字");
        }
    }
}

三、do-while循环

 

posted @ 2023-07-07 11:30  七安。  阅读(8)  评论(0编辑  收藏  举报