第六节(运算符、控制语句、方法初步)下

/*
    for循环
        语法:
        
        for(表达式1;表达式2;表达式3){
            java语句;    
        }
        
        表达式1: 初始化值,最先执行,只执行一次
        表达式2:必须是 boolean类型的表达式
        
        for循环开始执行,最先执行表达式1,只执行一次
        然后需要判断表达式2的结果,如果是true,则执行java语句;
        再执行表达式3,然后再判断表达式2的结果,直到表达式2的结果是false,循环结束
        
*/

public class ForTest01{

    public static void main(String[] args){
    
    /*    
        for(int i = 1; i <= 3; i++ ){
            System.out.println("i = " + i);
        }
    */        
        // 在控制台输出偶数
        for(int j = 0; j < 10; j+=2){
            System.out.println("j = "+j);
        }
        
        for(int k = 10; k > 0; k--){
            System.out.println("k = " + k);    
        }
        
        // 外层循环
                for(int i = 5; i > 0; i--){
                    System.out.println("外层循环: i = " + i+"\n\n");
                    // 内层循环
                    for(int j = 0; j < i; j++){
                        System.out.println("内层循环:j = " + j+"\t\t");
                    }
                }    
        
    }

}
例题:

/*
    使用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*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    
    

*/

public class ForTest03{
    
    public static void main(String[] args){
    
        // 外层循环行
        for(int i = 1; i <= 9; i++){
            // 内层循环列
            for(int j = 1; j <= i; j++){
                System.out.print(j + "*" + i + "=" + (i*j)+"\t");        
            }
            System.out.print("\n");
        }    

    }


}
2public class DoWhileTest01{

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

}

/*
continue; */ public class ContinueTest01{ public static void main(String[] args){ /* for(int i = 0; i < 10 ; i++){ if(i == 3){ // 可用来结束本次循环,直接进入下一次循环,继续执行 continue; } System.out.println("i = " + i); } */ for(int i = 0; i < 10 ; i++){ if(i == 3){ // 结束当前整个循环 break; } System.out.println("i = " + i); } } }
《3》函数
/*
关于方法的重载 以下 程序不适用方法重载,缺点是什么 1. 程序开发者需要记忆更多的方法名 2. 程序代码不美观(优雅)。 什么时候需要方法重载? 1. 发生在同一类中 2. 方法名相同 3. 参数列表不同(类型、个数、顺序) 4. */ public class MethodTest05{ // 主方法 public static void main(String[] args){ // 对于程序来说,需要记忆三个不同的方法名 System.out.println("int类型:"+Ming2.sumInt(10,20)); System.out.println("double类型:"+Ming2.sumDouble(1.0,2.0)); System.out.println("long类型:"+Ming2.sumLong(10L,20L)); } } class Ming2{ public static int sumInt(int a,int b){ return a + b; } public static double sumDouble(double a,double b){ return a + b; } public static long sumLong(long a,long b){ return a + b; } }



public class MethodTest02{

    // 自定义一个方法,完成整数的求和
    // 先定义成一个 public static
    public static void mySum(int a,int b){
        int c = a + b;
        System.out.println("c = "+c);
    }



    // 入口
    public static void main(String[] args){
        
        // 注意:加有static的方法,调用的时候必须采用“类名.” 的方式调用
        
        MethodTest02.mySum(10,20);
        
        MethodTest02.mySum(50,100);
        
    }
    
    

}

 

/*
    定义方法的语法:
        
        [方法修饰列表] 返回值类型  方法名(方法参数列表){
                
            方法体
        
        }
        
        1. 方法修饰列表是可以选项,现在暂时先写成 public static
        2. 返回值类型 可以是java中的任意类型(基本数据类型,引用数据类型)
        3. 如果该方法执行后,没有任何返回值 就用 void
        4. 方法名只要是合法的标识符就可以的
        5. 方法的形式参数列表,可以有参数,也可以没有参数,如果有多个用“,”逗号隔开

*/

public class MethodTest03{


    // public static 修饰符
    // void 无返回值
    // arry 方法名
    // 没有形式参数
    public static void Ming(){
        System.out.println("我是最帅气的ming");    
    }

    // public static 修饰符
    // void 无返回值
    // arry1 方法名 
    // int a,boolean b 是形式参数列表
    public static void Ming1(int a,boolean b){
        int i = 0;
        if(b){
            i = a;
        }    
        System.out.println("i = "+i);
    }

    // 该方法有返回值,程序必须使用return 语句返回
    public static int Ming2(int a,int b){
        
        int c = a + b;
        return c; // 程序执行到return 这行,arry2这个方法就执行结束,并返回值c
        
        // return语句下边不能再有其他的代码,因为根本就不能执行到,也不能编译通过

    }

    public static void main(String[] args){
        
        MethodTest03.Ming();        
        MethodTest03.Ming1(10,true);
        
    //    int num = MethodTest03.arry2(20,50);
    
        System.out.println("num = " + MethodTest03.Ming2(20,50));
        
        
    }
    
    
    

}

 


/*

*/
public class MethodTest04{
    public static void main(String[] args){       
        Arry.m();     
        // 直接调用方法名:前提是 必须在当前类,类名就可以省掉
        //MethodTest04.T1();
        T1();
    }


    public static void T1(){
        System.out.println("我是xiaoming的 !");
    }
}


class Arry{  
    public static void m(){
        System.out.println("我是ming的 !");    
    }
}

 

 

 

posted @ 2015-01-15 16:27  哥的笑百度不到  阅读(148)  评论(0编辑  收藏  举报