if else,switch以及

if-else语句

对于简单的比较两个或几个已知数的大小,就可以用到if-else来选择:

public class TestDemo {
    public static void max(int a,int b) {
        if(a>b){
            System.out.println("a>b");
        }else if(a==b){
            System.out.println("a=b");
        }else{
            System.out.println("a<b");
        }
    }
    public static int max2(int a,int b) {
        if(a>b){
            System.out.println("a>b");
            return a;
        }else if(a==b){
            System.out.println("a=b");
            return -1;
        }else{
            System.out.println("a<b");
            return b;
        }
    }
    public static void max3(int a,int b,int c) {
        int i1 = max2(a,b);
        int i2 = max2(c,i1);
        System.out.println(i2);
        //return max2(max2(a,b),c); 相当于以上三行代码
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int a = 10;
        int b = 20;
        max(a,b);
        max3(10,20,30);
    }
}

以及可以用if-else语句来实现一些小的问题:
(1)求当前数字是否为素数

public class TestDemo1 {

    public static boolean IsPrime(int n) {
            for(int i=2;i<n;i++){
                if(n%i==0){
                    return false;
                }
            }
            return true;
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        boolean c=IsPrime(7);
        System.out.println(c);
    }

}

这里写图片描述

switch语句

public class TestDemo2 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int i = 2;
        //switch可以使用的参数类型?
        //boolean b = true;//不可以作为switch参数
        //String name = "jiahanlin";
        //byte b2 = 10;
        //char ch = 'a';
        //short sh = 100;
        //final int i2 = 1;
        //long lh = 10L;//不可以作为switch参数
        //float f = 12.5f;//不可以作为switch参数
        //double d = 12.25d;//不可以作为switch参数

        switch(i){
        case 1:
            System.out.println("1:");
            break;
        case 2:
            System.out.println("2:");
            break;
        case 3:
            System.out.println("3:");
            break;
        case 4:
            System.out.println("4:");
            break;
        /*case "jiahanlin":
            System.out.println("jiahanlin");
            break;*/
        case 10:
            System.out.println("10:");
            break;
        default:
            System.out.println("input error!");
            break;
        }
    }

例题1
选择条件打印输出
A:80分以上
B:70-80
C:60-70
D:不用学习了

public class TestDemo3 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        char ch = 'A';
        switch(ch){
        case 'A':
            System.out.println("80分以上");
            break;
        case 'B':
            System.out.println("70-80分");
            break;
        case 'C':
            System.out.println("60-70分");
            break;
        case 'D':
            System.out.println("50-60");
            break;
        default:
            System.out.println("不用学习了");
            break;
        }
    }

}

这里写图片描述
例题
for循环求
(1)1+2+3+……+n(方法sum)
(2)5!(方法jiecheng)
(3)9x9乘法表(方法muti)
(4)求100以内奇数和以及偶数和(方法sum2)
(5)斐波那契数列第40项(方法)

public class TestDemo4 {

    public static int sum(int n) {
        int add = 0;
        for(int i=1;i<=n;i++){
            add+=i;
        }
        return add;
    }

    public static int jiecheng(int n) {
        int add = 1;
        for(int i=1;i<=n;i++){
            add*=i;
        }
        return add;
    }

    public static void muti(int n) {
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=i;j++)
            {
                System.out.print(j+"*"+i+"="+(j*i)+"   ");
            }
            System.out.println();
        }
    }

    public static void sum2(int n) {
        int add1=0;
        int add2=0;
        int add3=0;
        for(int i=1;i<=n;i++){
            add1+=i;
        }
        System.out.println("sum1: "+add1);

        for(int j=2;j<=n;j+=2){
            add2+=j;
        }
        System.out.println("sum2: "+add2);

        for(int m=1;m<=n;m+=2){
            add3+=m;
        }
        System.out.println("sum3: "+add3);

    }

    public static int Fibonacci(int n) {
        int f1 = 1;
        int f2 = 1;
        int fsum=0;
        for(int i=2;i<n;i++){
            fsum = f1+f2;
            f1=f2;
            f2=fsum;
        }
        return fsum;
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        System.out.println(sum(10));
        System.out.println("----------------");
        System.out.println(jiecheng(5));
        System.out.println("----------------");
        muti(9);
        System.out.println("----------------");
        sum2(100);
        System.out.println("----------------");
        System.out.println(Fibonacci(40));
    }
}

其运行结果而为:
这里写图片描述

break和continue的区别

break是结束整个循环体
continue是结束单次循环(跳过直接下一步)

public class TestDemo5 {
    public static int fun(int n) {
        int tmp = 0;
        int i;
/*      for(i = 1;tmp<=1000;i++){
            tmp = tmp + i;
        }
        return i-1;
*/
        for(i = 1;;i++){
            tmp = tmp + i;
            if(tmp>n){
                break;
            }
        }
        return i;
    }

    public static void fun2(int n) {
        int i = 0;
        for(i = 1;i<=n;i++){
            if(i%15!=0){
                continue;
            }
            System.out.println(i);
        }
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        System.out.println(fun(1000));
        System.out.println("----------------");
        fun2(100);
    }

}

从打印的结果可以看出break和continue区别在哪里:
这里写图片描述

交换两个数的数值

public static void swap(int[] n)
{
    int temp  = n[0];
    n[0] = n[1];
    n[1] = temp;
public static void main(String[] args) {
    int a=10;
    int b=20;
    int[] n ={a,b};
    swap(n);
    System.out.println("a:"+n[0]+"   b:"+n[1]);
}

猜你喜欢

转载自blog.csdn.net/qq_39475906/article/details/79894972
今日推荐