您的位置:首页 > 编程语言 > Java开发

java 从零开始,学习笔记之基础入门<Switch>(八)

2013-11-05 09:32 916 查看
witch 语句是一类分支语句

  语法格式为

 

Switch(case 值){

Case 1: 操作1;break;

Case 2: 操作2;break;

Case 3: 操作3;break;

Case 4: 操作4;break;

………………

 

 

package com.ibm.four;
 
publicclass SwitchDemo {
    //第二类分支语句Switch语句
    //语法格式
    /*
     *
     *
     *
     *
     * */
//  public static void main(String[]
args) {
//     int a=0;
//     switch(a){
//     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 5: System.out.println("5");break;
//     case 6: System.out.println("6");break;
//     case 7: System.out.println("7");break;
//     case 8: System.out.println("8");break;
//     case 9: System.out.println("9");break;
//     case 10: System.out.println("10");break;
//     default:System.out.println("0");break;
//    
//     }
//    
//  }

}

For 循环

用来执行反复执行的操作!

For(int i  = 初始值;i<最大范围;每次循环增加量 ){

//循环一次所做的操作}

 

package com.ibm.four;
 
public class ForDemo {
    //
输出1-10
这10个数
    public static void main(String[] args) {
       for (int i = 1; i <= 10; i++) {
           System.out.println(i);
       }
       //
输出1+2+3+4+...+100
       // 0+1=1
       // 1+2=3
       // 3+3=6
       // 6+4=10
       int sum = 0;//
结果值
       for (int i = 1; i <= 100; i++) {
           sum = sum + i;
       }
       System.out.println(sum);
      
    //输出乘法口诀
       /* 1*1=1
        * 1*2=2 2*2=4
        * 1*3=3 2*3=6 3*3=9
        * .
        * .
        * .
        * .
        * 1*8=8 2*8=16 3*8=24 4*8=32 5*8=40 6*8=48 7*8=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
        * */
      
      
       for(int 
i=1;i<=9;i++){
            for(int 
j=1;j<=i;j++){
             System.out.print(j+"*"+i+"="+j*i+"\t"); 

            }
           System.out.println();
       }
      
 
    }
}
 

While循环

语法格式

While(条件){

//循环执行的操作

}

 

package com.ibm.four;
 
public class WhileDemo {
    public static void main(String[] args) {
       //1采用while循环输出1-10
       int i=1;
       while(i<=10){
           System.out.println(i);
           i++;
       }
      
      
       //输出10-1
       int j=10;
       while(j>=1){
           System.out.println(j);
           j=j-1;
       }
      
       //输出1-100的和
        int 
k=1;
        int 
sum=0;
        while(k<=100){
            sum=sum+k;
            k++;
        }
        System.out.println(sum);
      
        
        //如果while循环中的条件恒成立,那么此时的while循环是一死循环
        while(true){
            System.out.println("---------------------------------------------");
        }
        //一个方法中只能存在一个死循环
      
    }
 
}
 

Dowhile循环

Do{

 //循环体里面的操作

}while(条件)

采用dowhile输出1到10
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐