java判断与循环语句

来源:互联网 发布:帝国cms html5模板 编辑:程序博客网 时间:2024/05/07 13:48

java判断与循环语句


目录(?)[-]

  1. 顺序结构
  2. 选择结构
  3. 循环结构
  4. 终端语句
程序语句的三种结构
1.顺序结构
2.选择结构
3.循环结构



3.1顺序结构


3.2选择结构


举例:验证选择结构
[java] view plaincopyprint?
  1. public  class IfDemo{  
  2.     public static void main(String args[]){  
  3.         int x = 3;        //定义变量  
  4.         int y = 10;        //定义变量  
  5.         System.out.println("===比较开始==");  
  6.         if(x>y){  
  7.             System.out.println("x比y大");  
  8.         }  
  9.         if(x<y){  
  10.             System.out.println("x比y小!");  
  11.         }  
  12.     }  
  13. }  



if语句

if...else语句

[java] view plaincopyprint?
  1. public class IfElseDemo{  
  2.     public static void main(String args[]){  
  3.         int x = 3;  
  4.         if(x%2==1){  
  5.             System.out.println("x是奇数");  
  6.         }else{  
  7.             System.out.println("x是偶数");  
  8.         }  
  9.       
  10.       
  11.     }  
  12.   
  13. }  




三目运算符


[java] view plaincopyprint?
  1. public class MaxDemo{  
  2.     public static void main(String args[]){  
  3.         int max = 0;  
  4.         int x = 3;  
  5.         int y = 10;  
  6.         if(x<y){  
  7.             max = y;  
  8.         }else  
  9.         {  
  10.             max = x;  
  11.         }  
  12.         System.out.println("最大值为:" + max);  
  13.     }  
  14.   
  15. }  



使用三目运算符

[java] view plaincopyprint?
  1. public class MaxDemo{  
  2.     public static void main(String args[]){  
  3.         int max = 0;  
  4.         int x = 3;  
  5.         int y = 10;  
  6.         max  = x>y ? x : y;  //通过三目运算符  
  7.         System.out.println("最大值为:" + max);  
  8.     }  
  9. }  




if ... else if ...else 语句

[java] view plaincopyprint?
  1. public class MoreIfElseDemo{  
  2.     public static void main(String args[]){  
  3.         int x = 3;  
  4.         if(x==1){  
  5.             System.out.println("x的值是1");  
  6.         }else if(x==2){  
  7.             System.out.println("x的值是3");  
  8.         }else if(x==3){  
  9.             System.out.println("x的值是1");  
  10.         }else{  
  11.             System.out.println("x不是1,2,3中的任何一个");  
  12.         }  
  13.       
  14.     }  
  15.   
  16. }  




switch语句

[java] view plaincopyprint?
  1. public class SwitchDemo01{  
  2.   
  3.     //完成一个四则运算的功能  
  4.     public static void main(String args[]){  
  5.       
  6.         int x = 3;  
  7.         int y = 6;  
  8.         char oper = '+';  
  9.         switch(oper){  
  10.             case '+':{  
  11.                 System.out.println("x + y = " + (x + y));  
  12.                 break;  
  13.             }  
  14.             case '-':{  
  15.                 System.out.println("x - y = " + (x - y));  
  16.                 break;  
  17.             }  
  18.             case '*':{  
  19.                 System.out.println("x * y = " + (x * y));  
  20.                 break;  
  21.             }  
  22.             case '/':{  
  23.                 System.out.println("x / y = " + (x / y));  
  24.                 break;  
  25.             }  
  26.             default:{  
  27.                 System.out.println("未知的操作");  
  28.                 break;  
  29.             }  
  30.           
  31.         }  
  32.     }  
  33. }  




3.3循环结构


while循环
[java] view plaincopyprint?
  1. public class WhileDemo{  
  2.     public static void mian(String args[]){  
  3.         int x = 1;  
  4.         int sum = 0;  
  5.         while(x<=10){  
  6.             sum += x;  
  7.             x++;            //修改循环条件  
  8.         }  
  9.         System.out.println("1 --> 10 的累加效果为:" + sum);  
  10.     }  
  11. }  


do while循环

[java] view plaincopyprint?
  1. public class DoWhileDemo{  
  2.     public static void main(String args[]){  
  3.         int x = 1;  
  4.         int sum = 0;  
  5.         do{  
  6.             sum += x;  
  7.             x++;  
  8.         }while(x<=10);  
  9.         System.out.println("1 -->  10 的累加效果为:" + sum);  
  10.     }  
  11. }  



for循环


[java] view plaincopyprint?
  1. public class ForDemo{  
  2.     public static void main(String args[]){  
  3.         int sum = 0;  
  4.         for(int x=1;x<=10;x++){  
  5.             sum += x;  
  6.         }  
  7.         System.out.println("x  --> 10 的累加效果为:" + sum);  
  8.           
  9.       
  10.     }  
  11.   
  12. }  


循环嵌套操作(最好2~3层即可,不要太多,否则太占内存)

乘法表格
[java] view plaincopyprint?
  1. public class ForNesteDemo{  
  2.     public static void main(String args[]){  
  3.         for(int i=1;i<=9;i++){ //控制行  
  4.             for(int j=1;j<=9;j++){ //控制列  
  5.                 System.out.print(i+"*"+j+"="+(i*j)+"\t");  
  6.             }  
  7.             System.out.println();  
  8.         }  
  9.     }  
  10. }  




使用阶梯形状变化
public class ForNesteDemo{
    public static void main(String args[]){
        for(int i=1;i<=9;i++){ //控制行
            for(int j=1;j<=i;j++){ //控制列
                System.out.print(i+"*"+j+"="+(i*j)+"\t");
            }
            System.out.println();
        }
    }
}

3.4终端语句



break语句
可以中断for循环语句====以后的操作都不在执行了

[java] view plaincopyprint?
  1. public class BreakDemo{  
  2.     public static void main(String args[]){  
  3.         for(int i=1;i<10;i++){  
  4.             if(i==3){  
  5.             break;  
  6.             }  
  7.             System.out.println("i = " + i);  
  8.         }  
  9.     }  
  10. }  



continue语句
只中断一次循环的执行

[java] view plaincopyprint?
  1. public class ContinueDemo{  
  2.     public static void main(String args[]){  
  3.         for(int i=1;i<10;i++){  
  4.             if(i==3){  
  5.             continue;  
  6.             }  
  7.             System.out.println("i = " + i);  
  8.         }  
  9.     }  
  10. }  
0 0
原创粉丝点击