一起Talk Android吧(第七回:Java综合小练习)

95 阅读1分钟

各位看官们,大家好,上一回中咱们说的是Java中函数的例子,这一回咱们说的例子是Java综合小练习。闲话休提, 言归正转。让我们一起Talk Android吧!

看官们,前面章回中介绍了这么多Java的内容,但是没有写一行代码,估计大家的手都痒痒了,这一回中,我们会对前面介绍过的内容进行综合练习,让大家一起动手去实战。

public class HelloWorld {

    public static void main(String args[])
    {
        // different type of value
        int intValue = 1;
        long logValue = 1;
        short shotValue = 1;
        byte byteValue = 1;

        float flotValue = 1;
        double dobValue = 1.0;

        char chValue = 'A';
        String strValue = "Hello";

        boolean boolValue = true;

        int array[] = new int[10];

        System.out.println("intValue:  "+intValue);     
        System.out.println("logValue:  "+logValue);     
        System.out.println("shotValue: "+shotValue);        
        System.out.println("byteValue: "+byteValue);        
        System.out.println("flotValue: "+flotValue);
        System.out.println("dobValue:  "+dobValue);     
        System.out.println("chValue:   "+chValue);      
        System.out.println("strValue:  "+strValue);     

        System.out.println("------------ function------------");
        show(intValue);
        show(logValue);
        show(flotValue);
        show(strValue);
        System.out.println("------------ function------------");

        // change different type of value
//      intValue = logValue; // this change is wrong, and it caused a compilation error.
        logValue = intValue;


        //using kinds of operator
        intValue = 1+1;
        System.out.println("Operator + intValue:  "+intValue);      

        intValue = 1*9;
        System.out.println("Operator * intValue:  "+intValue);      

        intValue += 1;
        System.out.println("Operator += intValue:  "+intValue);     

        intValue = 8>>2;
        System.out.println("Operator >> intValue:  "+intValue);     

        //using kinds of structure
        if(intValue == 1)
        {
            System.out.println("intValue equals 1");
        }
        else if(intValue < 1)
        {
            System.out.println("intValue is less then 1");
        }
        else
        {
            System.out.println("intValue is larger then 1");
        }

        if(boolValue && intValue > 1)
        {
            System.out.println("boolValue is: "+boolValue);
            System.out.println("intValue is:  "+intValue);      
        }

        for(int i=0; i<10;++i)
        {
            array[i] = i+1;
        }

        System.out.print("array:  ");
        for(int v:array)
        {
            System.out.print(v+"  ");
        }
        System.out.println();       

        intValue = 0;
        while(intValue++ < 5)
        {
            show(strValue);         
        }

    }

    //function overload
    public static void show(int v)
    {
        System.out.println("intValue:  "+v);        
    }

    public static void show(long v)
    {
        System.out.println("logValue:  "+v);        
    }

    public static void show(short v)
    {
        System.out.println("shotValue:  "+v);       
    }

    public static void show(byte v)
    {
        System.out.println("byteValue:  "+v);       
    }

    public static void show(float v)
    {
        System.out.println("flotValue:  "+v);       
    }

    public static void show(double v)
    {
        System.out.println("dobValue:  "+v);        
    }

    public static void show(char v)
    {
        System.out.println("chValue:  "+v);     
    }

    public static void show(String v)
    {
        System.out.println("strValue:  "+v);        
    }
}

我们通过上面的代码对前面的内容进行了综合演示。该代码属于基础类型的代码,相信大家可以看明白,如果有不太明白的地方可以参考代码中的注释。

下面是程序的运行结果,请大家参考:

intValue:  1
logValue:  1
shotValue: 1
byteValue: 1
flotValue: 1.0
dobValue:  1.0
chValue:   A
strValue:  Hello
------------ function------------
intValue:  1
logValue:  1
flotValue:  1.0
strValue:  Hello
------------ function------------
Operator + intValue:  2
Operator * intValue:  9
Operator += intValue:  10
Operator >> intValue:  2
intValue is larger then 1
boolValue is: true
intValue is:  2
array:  1  2  3  4  5  6  7  8  9  10  
strValue:  Hello
strValue:  Hello
strValue:  Hello
strValue:  Hello
strValue:  Hello

各位看官,关于Java综合小练习的例子咱们就介绍到这里,欲知后面还有什么例子,且听下回分解!