3、Java的If语句与For循环

来源:互联网 发布:苹果6忽略网络怎么恢复 编辑:程序博客网 时间:2024/06/07 02:20
一 语句
  • 条件语句:根据不同的条件,执行不同的语句
    • if
    • if .. else
    • if .. else if
    • if .. else if .. else if.. else
    • switch
  • 循环语句:重复执行某些动作
    • for
    • while
    • do .. while

1.1 if语句
单分支if语句的基本格式为:

if (condition) {
     statement;
}

双分支的if语句基本格式为:

if (condition1) {
     statement1;
} else if (condition2) {
     statement2;
}

多分支的if语句基本格式为:

if (condition1) {
     statement1;
} else if (condition2) {
     statement2;
} else if (condition3) {
     statement4
}...
else {
     statement;
}

如下面一段代码

public class TestIf {
    public static void main(String[] args) {
        int i = 20;
        if (i < 20) {
            System.out.println("<20");
        } else if (i < 40) {
            System.out.println("<40");
        } else if (i < 60) {
            System.out.println("<60");
        } else
            System.out.println(">=60");
    }
}


1.2 for循环
for循环为如下形式:

for (表达式1; 表达式2; 表达式3) {
     statement; ... ;
}
执行过程:
执行过程如下如所示:

首先计算表达式1,接着执行表达式2,若表达式2的值=true,则执行statement,接着计算表达式3,再判断表达式2的值;依次重复下去,直接表达式2的值为false

如下面一段代码

public class TestFor {
    public static void main(String[] args) {
        long result = 0;
        long f = 1;
        for (int i = 1; i <= 10; i++) {
            f = f * i;
            result += f;
        }
        System.out.println("result=" + result);
    }
}
上面这段代码表示计算1!+2!+3!+...+10!


练习:编写程序,用一个for循环计算1+3+5+7+...+99的值,并输出结果

public class OddSum {
    public static void main(String[] args) {
        int sum = 0;
        for (int i = 1; i <= 99; i++) {
            if (i % 2 == 1) {
                sum += i;
            }
        }
        System.out.println("sum:" + sum);
    }
}
或者

public class OddSum {
    public static void main(String[] args) {
        int sum = 0;
        for (int i = 1; i <= 99; i+=2) {
            sum += i;
        }
        System.out.println("sum:" + sum);
    }
}


1.3 while & do while语句
1.3.1 while语句格式如下形式

while (condition) {
     statement;
}
其执行过程如下图所示:

先判断逻辑表达式的值。若为true,则执行后面的语句,然后再次判断条件并反复执行,知道条件不成立为止

1.3.2 do while语句格式如下形式:
一般很少用do...while
do {
     statement;
}
while (condition);

执行过程如下

先执行语句,再判断逻辑表达式的值,若为true,再执行语句,否则结束循环



使用while循环,完成从0到9的输出

public class TestWhile {
    public static void main(String[] args) {
        int i = 0;
        while (i < 10) {
            System.out.println(i);
            i++;
        }
    }
}


1.4 break和continue语句
1.4.1 break
break语句用于终止某个语句块的执行。用在循环语句体重,可以强制退出循环。

例如:循环打印1到10,但是当i打印到3的时候,直接退出循环

public class TestBreak {
    public static void main (String[] args) {
        int stop = 4;
        for (int i=1; i<=10; i++) {
            if (i == stop) {
                break;
            }
            System.out.println("i=" + i);
        }
    }
}

1.4.2 continue
continue语句用在循环语句体中,用于终止某次循环的过程,跳过循环体中continue语句下面未执行的循环,开始下一次的循环过程

例如:打印1到6,但是跳过4

public class TestContinue {
    public static void main(String[] args) {
        int skip = 4;
        for (int i=1;i<=6;i++) {
            if (i==skip) {
                continue;
            }
            System.out.println("i=" + i);
        }
    }
}




二 循环语句案例
2.1 利用循环完成以下内容
利用for循环和while循环,输出1-100内前5个可以被3整除的数

public class PracticeFor {
    public static void main(String[] args) {
        int count = 1;
        for (int i=1;i<=100;i++) {
            if (i%3==0 && count<=5) {
                System.out.println("i=" + i);
                count++;
            }
        }
    }
}

public class PracticeWhile {
    public static void main(String[] args) {
        int count = 0;
        int i = 1;
        while (i<=100) {
            if (i%3==0) {
                System.out.println("i=" + i);
                count++;
            }
            if (count==5) {
                break;
            }
            i++;
        }
    }
}

2.2 输出101到200内的质数

public class PrimeNum {
    public static void main(String[] args) {
        for (int i=101;i<=200;i+=2) {
            boolean f = true;
            for (int j=2;j<i;j++) {
                if (i%j==0) {
                    f = false;
                    break;
                }
            }
            if (!f) {
                continue;
            }
            System.out.println(i);
        }
    }
}


2.3 利用for循环写程序
输出1-100之间能被5整除的数,且每行输出3个

public class TestFive {
    public static void main(String[] args) {
        for (int i=1;i<=100;i++) {
            if (i%5==0) {
                System.out.print(i+" ");
            }
            if (i%15==0) {
                System.out.println();
            }
        }
    }
}

2.3 利用for循环打印九九乘法表
九九乘法表如下形式:

1*1=1
1*2=2    2*2=4
1*3=3    2*3=6    3*3=9
1*4=4    2*4=8    3*4=12    4*4=16
1*5=5    2*5=10    3*5=15    4*5=20    5*5=25
1*6=6    2*6=12    3*6=18    4*6=24    5*6=30    6*6=36
1*7=7    2*7=14    3*7=21    4*7=28    5*7=35    6*7=42    7*7=49
1*8=8    2*8=16    3*8=24    4*8=32    5*8=40    6*8=48    7*7=56    8*8=64
1*9=9    2*9=18    3*9=27    4*9=36    5*9=45    6*9=54    7*9=63    8*9=72    9*9=81


public class Multiplication {
    public static void main(String[] args) {
        for (int i=1;i<=9;i++) {
            for (int j=1;j<=i;j++) {
                System.out.print(j+"x"+i+"="+(j*i)+"\t");
            }
            System.out.println();
        }
    }
}











三 switch语句及方法
switch语句功能与if..else if...else if语句功能类似。多条件,其格式如下:

switch() {
     case xx:
          ...
     case xx:
          ...
     default:
          ...
}
  • 小心case穿透,推荐使用break语句
  • 多个case可以合并到一起
  • default可以省略,但不推荐省略
  • Java中的switch语句只能探测int类型值

如下小程序

public class TestSwitch {
    public static void main(String[] args) {
        int i = 18;
        switch (i) {
            case 8 :
                System.out.println("2");
                break;
            case 2 :
                System.out.println("8");
                break;
            case 9 :
                System.out.println("9");
                break;
            default:
                System.out.println("error");
        }
    }
}
当i为8时,打印为2;当i为2时,打印8;当i为9时,打印为9;其他情况打印为error


3.1 方法
Java的方法类似于其他语言的函数,是一段用来完成特定功能的代码片段,声明格式:

[修饰符1  修饰符2 ...] 返回值类型 方法名(形式参数列表) {
     Statement;
}
  • 形式参数:在方法被调用时用于接收外界输入的数据
  • 实参:调用方法时实际传给方法的数据
  • 返回值:方法在执行完毕后返还给调用它的环境的数据
  • 返回值类型:事先约定的返回值的数据类型,如无返回值,必须给出返回值类型void

  1. Java语言中使用调用方法:对象名.方法名(实参列表)
  2. 实参的数目、数据类型和次序必须和所调用方法声明的形参列表匹配
  3. return语句终止方法的运行并指定要返回的数据

Java中进行函数调用中传递参数时,遵循值传递的原则:
  • 基本类型传递的是该数据值本身。引用类型传递的是对对象的引用,而不是对象本身

如下一段小程序

public class TestMethod {
    public static void main(String[] args) {
        m();               //调用m()方法
        m2(5);               //调用m2()方法,并指定实参,即int i = 5
        m3('3',4);
        m4(4,6);
        int i = m4(4,6);
        System.out.println(i);
    }

    public static void m() {          //void表示没有任何的返回值
        //return;     //不能出现return,因为出现return表示终止方法运行,      
                                  后面语句出现问题
        System.out.println("ok");
        System.out.println("Hello");
    }

    public static void m2(int i) {          //int i为形参,调用时为实参
        if (i > 3) {
            return;
        }
        System.out.println(i);
        
    }

    public static void m3(int i, int j) {
        System.out.println(i + j);
    }

    public static int m4(int i, int j) {
        return i > j ? i : j;
    }
}
阅读全文
0 0
原创粉丝点击