流程控制/循环

Swtich

当一个case 成立,从这个case向后穿透所有case,包括default代码,直到结束,或者遇到break跳出 

只能判断:

byte/short/char/int

Enum

Jdk1.7之后可以判断 String

只能是字面值不能是变量或表达式

 

百分制转五档分制

package day0304;

 

import java.util.Scanner;

 

public class Test1 {

    public static void main(String[] args) {

        System.out.print("输入百分数:");

        int i = new Scanner(System.in).nextInt();

        f(i);

    }

 

    private static void f(int i) {

 

        if (i < 0 || i > 100) {

            System.out.println("输入分数有误");

            return;

        }

 

        String r = "";

        switch (i / 10) {

            case 10:

            case 9:

                r = "A";

                break;

            case 8:

            case 7:

                r = "B";

                break;

            case 6:

                r = "C";

                break;

            case 5:

            case 4:

            case 3:

            case 2:

                r = "E";

                break;

            case 1:

            case 0:

                r = "F";

                break;

        }

        System.out.println("五分制:" + r);

    }

}

for循环

 for(变量赋值; 执行条件; 变量修改) {

   …

}

 

例如打印1到10:

for(int i=1; i<=10; i++) {

System.out.println(i);

}

package day0305;

 

public class Test1 {

    public static void main(String[] args) {

        f1();

        f2();

        f3();

        f4();

        f5();

    }

 

    private static void f1() {

        for(int i=1;i<=10;i++) System.out.print(i+",");

    }

    private static void f2() {

        for(int i=10;i>=1;i--) System.out.print(i+",");

    }

    private static void f3() {

        for(int i=2;i<=10;i+=2) System.out.print(i+",");

    }

    private static void f4() {

        for(int i=8;i<=88888888;i=i*10+8) System.out.print(i+",");

    }

    private static void f5() {

        //死循环

        for(int i=1;;i++) System.out.println(i);

    }

}

Π的值

 Π/2=1/1-1/3+1/5-1/7+1/9…

package day0305_求Π值;

 

import java.util.Scanner;

 

public class Test1 {

    public static void main(String[] args) {

        System.out.print("请输入计算次数:");

        int i = new Scanner(System.in).nextInt();

 

        f(i);

    }

 

    private static void f(int i) {

        double result = 0;

        for (int n = 1, a = 1, b = 1; n <= i; n++, a *= -1, b += 2) {

            result += (double) a / b;

        }

        System.out.println(result * 4);

    }

}

鸡兔同笼

鸡兔共35,94只脚,问鸡和兔各有几只?

package day0304;

 

public class Test1 {

    public static void main(String[] args) {

        for (int j = 0, t = 35; j <= 35; j++, t--) {

            if (j * 2 + t * 4 == 94) System.out.println("鸡:" + j + "兔:" + t);

        }

    }

}

质数

输入一个值判断是否是质数

package day0308;

 

import java.util.Scanner;

 

public class Test1 {

    public static void main(String[] args) {

        System.out.print("请输入一个质数:");

        int i = new Scanner(System.in).nextInt();

        f(i);

    }

 

    private static void f(int i) {

        double max = Math.sqrt(i) + 1;

        for (int j = 2; j < max; j++) {

            if (i % j == 0) {

                System.out.println("不是质数");

                return;

            }

        }

        System.out.println("是质数");

    }

}

 

 

循环嵌套

package day0305;

 

import java.util.Scanner;

 

public class Test2 {

    public static void main(String[] args) {

        f1();

        System.out.print("请输入要打印的行数:");

        int i = new Scanner(System.in).nextInt();

        f2(i);

        System.out.print("请输入要打印的行数:");

        i = new Scanner(System.in).nextInt();

        f3(i);

    }

 

    private static void f1() {

        for (int i = 1; i <= 3; i++) {

            for (char j = 'a'; j <= 'c'; j++)

                System.out.println(i + "," + j);

        }

    }

 

    private static void f2(int i) {

        for (int n = 1; n <= i; n++) {

            for (int nn = 1; nn <= i; nn++) System.out.print("*");

            System.out.println();

        }

    }

 

    private static void f3(int i) {

        for (int n = 1; n <= i; n++) {

            for (int nn = 1; nn <= n; nn++) System.out.print("*");

            System.out.println();

        }

    }

}