Java循环for, while和do...while & Java决策制定

    xiaoxiao2024-04-22  7

    package com.baiyimao;

    public class demo {

    public static void main(String[] args) {

       

    System.out.println("   Java循环for, while和do...while   ");

    emo obj =new demo();

    obj.basicUse();

    obj.extentionFuctions();//for循环在Java中增强版

    obj.breakUseMethod();//break关键字

    obj.continueUseMethod();//continue关键字

    //Java决策制定: if 语句  、   switch 语句

    obj.if_use();//if use

    obj.switch_use();

    }

    public void basicUse() {

    //while

    int x = 10;

    while (x < 20) {

    System.out.println("value of :" +x);

    x ++;

    System.out.println("  ");

    }

    // do...while

    do {

    System.out.println("value of :" +x);

    x ++;

    System.out.println("  ");

    }while (x < 30);//注意 不能省略 ;

    //for

    for (intj = 10 ;j < 20;j++ ) {

    System.out.println("value of :" +j);

    j ++;

    System.out.println("  ");

    }

         }

    //for循环在Java中增强版

    public void extentionFuctions () {

    System.out.println(" 扩展 ");

    /*

    * 声明: 新声明块变量,这是一种与正在访问数组中的元素兼容的。

    *     变量将是可利用的块内并且它的值将是相同的作为当前的数组元素。

    * 表达: 这个计算结果完成需要循环数组。表达式可以是一个数组变

    * 量或方法调用返回一个数组。

    */

    //for 循环在java 中增强版(主要是用于数组)

    int  [ ]numbers = {10,20,30,40,50};

    for (intx  :numbers) {

    System.out.println(x);

    System.out.println(", ");

    }

    System.out.println(" ------------------------- ");

    String [] names  = {"java","object-C","c++","c"};

    for(Stringname:names) {

    System.out.println(name);

    System.out.println(", ");

    }

    /*  打印出结果:

     扩展 

    10

    20

    30

    40

    50

     ------------------------- 

    java

    object-C

    c++

    c

    ,  

    */

    }

    //break关键字

    public void breakUseMethod () {

    /*

    * 关键字break是用来停止整个循环。 break关键字必须使

    * 用任何循环或switch语句中。关键字break将停止最内层

    * 循环的执行,并开始执行的下一行代码的程序段后。

    */

    System.out.println(" -----------break关键字------------- ");

    int  [ ]numbers = {10,20,30,40,50};

    for (intx  :numbers) {

    if(x == 30) {

    break;

    }

    System.out.println(x);

    System.out.println(", ");

    }

    /*

     ------------------------- 

    10

    20

    */

    }

    //continue关键字

    public void continueUseMethod () {

    /*

    * continue关键字可以在任一环的控制结构可以使用。它使循环立即跳转到循环的下一次迭代.

    * 在for循环中,continue关键字会导致流程控制的立即跳转到更新语句。

    * 在一个while循环或do/while循环,流控制的立即跳转到布尔表达式。

    */

    System.out.println(" -----------continue关键字-------------- ");

    int  [ ]numbers = {10,20,30,40,50};

    for (intx  :numbers) {

    if(x == 30) {

    continue;

    }

    System.out.println(x);

    System.out.println(", ");

    }

    /*

     ------------------------- 

    10

    20

    */

    }

    //if 

    public void if_use() {

    int x = 10;

    if( x < 20 ){

            System.out.print("This is if statement"); 

    }

    /*

    if(1) {//这种方法错误 () 里面必须是bool 类型 不过在c 或者 OC 语言中是可以的 这是区别

    }

    */

    if( x < 20 ){

            System.out.print("This is if statement  x < 20 "); 

             

    } else {

            System.out.print("This is else statement  x > 20 "); 

    }

    }

    public void switch_use() {

    System.out.println("---------switch----------------");

    /*

    * 1.在switch语句中使用的变量只能是一个字节,short,int和或char。

    * 2.可以switch 有一个任何数量的case语句。每个案例后面进行比较的值和一个冒号。

    * 3.对于 case 的值必须是相同的数据类型作为开关变量,它必须是一个常量或文字。

    * 4.当被打开了变量等于的情况下,下列那 case 语句将执行,直到 break 语句为止。

    * 5.当达到一个break语句,switch 终止,并且控制流程跳转到下一行下面 switch语句。

    * 6.不是每一个 case 需要包含break。如果没有出现break,控制流将贯穿到后面的 case

    直到 break 为止。

    * 7.switch语句可以有一个可选默认 case ,它必须出现在 switch 的结束。缺省情况

    下,可用于执行任务时,没有case是true。没有break 是必须的,使用 default 

    */

    char grade = 'D';//学分 

    switch (grade) {

    case 'A':

    System.out.println("Excellent");

    break;

    case 'B':

    case 'C':

    System.out.println("well done");

    break;

    case 'D':

    System.out.println("you pass");

    case 'F':

    System.out.println("Better try again");

    break;

    default:

    System.out.println("Invalid grade");

    }

    //char grade = 'C';//学分  well done

    //char grade = 'D';//学分  you pass  Better try again

    }

    }

    转载请注明原文地址: https://ju.6miu.com/read-1288240.html
    最新回复(0)