for循环语句全示例

代码

if-else语句

import java.util.Scanner;

public class demo01 {
    public static void main(String[] args) {
        int x = 0;
        Scanner sc = new Scanner(System.in);
        x = sc.nextInt();
        if (x <= 1) {

            System.out.println(x);
        } else if (x > 1 & x < 10) {
            System.out.println(3 * x - 2);
        } else {
            System.out.println(4 * x);
        }
    }

}

for循环语句

1+3+7+…+(2n-1)

/* * 1+3+7+...+(2^n-1) */
public class demo02 {
    public static void main(String[] args) {
        int sum = 0;
        int a = 1;
        for (int i = 1; i <= 20; i++) {
            // int a=0;
            // a=a*2+1;
            // sum=sum+a;
            a = a * 2;
            sum = sum + a - 1;
        }
        System.out.println(sum);
    }

}

输出1+2+4+…+10000(忽略末位数为3的数)

/* * 1+2+4+...+10000忽略末位数为3的数 */
public class demo03 {
    public static void main(String[] args) {
        int sum = 0;
        for (int i = 1; i <= 10000; i++) {
            if (i % 10 == 3) {
                continue;
            }
            sum = sum + i;
        }
        System.out.println(sum);
    }
}

打印金字塔

import java.util.Scanner;
/* * 打印金字塔 */
public class demo04 {
    public static void main(String[] args) {
        int count=0;
        Scanner sc = new Scanner(System.in);
        System.out.print("请输入你要打印的排数:");
        count = sc.nextInt();
        for(int i=1;i<=count ;i++){
            for(int j=0;j<=count-i;j++){
                System.out.print(" ");
            }
            for(int j=1;j<=2*i-1;j++){
                System.out.print("*");  
            }
            System.out.print("\n");
        }
    }

}

打印九九乘法表

/* * 打印九九乘法表 */
public class demo05 {
    public static void main(String[] args) {
        System.out.println("九九乘法表");
        for(int i=1;i<=9 ;i++){
            for(int j=1;j<=i;j++){
                System.out.print(j+"x"+i+"="+i*j+"\t");
            }
            System.out.println(" ");
        }
    }
}

输出1—1000以内的素数

/* * 输出1——1000以内的素数 */
public class demo06 {
    public static void main(String[] args) {
        for (int i = 1; i <= 1000; i++) {
            int count = 0;// 被整除的次数
            for (int j = 1; j <= Math.sqrt(i); j++) {
                if (i % j == 0) {
                    count++;
                }
            }
            if (count <= 2) {
                System.out.println(i);
            }
        }
    }
}

另外一种比较好的方法:

public class demo07 {
    public static void main(String[] args) {
        for (int i = 1; i <= 1000; i++) {
            boolean isSushu=true;

            for (int j = 2; j <= Math.sqrt(i); j++) {
                if (i % j == 0) {
                    isSushu =false;
                    break;
                }
            }
            if (isSushu) {
                System.out.println(i);
            }
        }
    }
}

求1—1000以内的完全数

/* * 求1——1000以内的完全数 */
public class demo08 {
    public static void main(String[] args) {
        for (int i = 1; i <= 1000; i++) {
            int sum = 0;
            for (int j = 1; j <i; j++) {
                if (i % j == 0) {
                    sum = sum + j;// 此时j是i的因子
                }
            }
            if (sum == i) {
                System.out.println(i);
            }
        }
    }
}

生兔子问题

/* * 现有一对成熟的兔子,一对成熟的兔子每个月生一对兔子,小兔子三个月长成为成熟的兔子 * 求20个月后的兔子总对数 */
public class demo09 {
    public static void main(String[] args) {
        int a = 1;
        int b = 2;
        int c = 3;
        int temp;
        for (int i =4; i <= 20; i++) {
            temp = a + c;
            a = b;
            b = c;
            c = temp;
            System.out.println("第"+i+"个月生的兔子对数为"+temp+"对");
        }
    }
}

拓展

打印实心菱形

import java.util.Scanner;

public class demo11 {
    public static void main(String[] args) {
        int count = 0;
        Scanner sc = new Scanner(System.in);
        System.out.print("请输入你要打印的总行数:");
        count = sc.nextInt();

        System.out.println("实心菱形:");//count为总行数,i为第几行数
        for (int i = 1; i <= count; i++) {
            if (i <= count / 2 + 1) {
                for (int j = 1; j <= count / 2 + 1 - i; j++) {
                    System.out.print(" ");
                }
                for (int j = 1; j <= i; j++) {
                    System.out.print("* ");
                }
                System.out.println();
            } else {
                for (int j = 1; j <= (i - (count / 2 + 1)); j++){
                    System.out.print(" ");
                }
                for(int j = 1; j <= (2*(count / 2 + 1) - i);j++){
                    System.out.print("* ");
                }
                System.out.println();
            }
        }
    }
}

打印空心菱形

import java.util.Scanner;

public class demo10 {
    public static void main(String[] args) {
        int count = 0;
        Scanner sc = new Scanner(System.in);
        System.out.print("请输入你要打印的总行数:");
        count = sc.nextInt();

        System.out.println("空心菱形:");//i为第几行数,count为总行数
        for (int i = 1; i <= (count / 2 + 1); i++) {
            for (int j = 1; j <= ((count / 2 + 1) - i); j++) {
                System.out.print(" ");
            }
            System.out.print("*");
            for (int j = 1; j <= ((i - 2) * 2 + 1); j++) {
                System.out.print(" ");
            }
            if (i != 1) {
                System.out.print("*");
            }
            System.out.println();
        }
        for (int i = (count / 2); i >= 1; i--) {
            for (int j = 1; j <= ((count / 2 + 1) - i); j++) {
                System.out.print(" ");
            }
            System.out.print("*");
            for (int j = 1; j <= ((i - 2) * 2 + 1); j++) {
                System.out.print(" ");
            }
            if (i != 1) {
                System.out.print("*");
            }
            System.out.println();
        }
    }
}

你可能感兴趣的:(for循环)