一起Talk Android吧(第七回:Java综合小练习)
2017-03-04 09:50
369 查看
各位看官们,大家好,上一回中咱们说的是Java中函数的例子,这一回咱们说的例子是Java综合小练习。闲话休提, 言归正转。让我们一起Talk Android吧!
看官们,前面章回中介绍了这么多Java的内容,但是没有写一行代码,估计大家的手都痒痒了,这一回中,我们会对前面介绍过的内容进行综合练习,让大家一起动手去实战。
我们通过上面的代码对前面的内容进行了综合演示。该代码属于基础类型的代码,相信大家可以看明白,如果有不太明白的地方可以参考代码中的注释。下面是程序的运行结果,请大家参考:
各位看官,关于Java综合小练习的例子咱们就介绍到这里,欲知后面还有什么例子,且听下回分解!
看官们,前面章回中介绍了这么多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综合小练习的例子咱们就介绍到这里,欲知后面还有什么例子,且听下回分解!
相关文章推荐
- 一起Talk Android吧(第十三回:Java面向对象综合练习)
- 一起Talk Android吧(第九回:Java中的封装:一)
- 一起Talk Android吧(第十四回:Java中的异常)
- 一起Talk Android吧(第二十六回:Java包装类)
- 一起Talk Android吧(第十回:Java中的封装:二)
- 一起Talk Android吧(第四回:Java中的运算符)
- 一起Talk Android吧(第二十三回:Java多线程编程一)
- 一起Talk Android吧(第二回:Java版Hello World)
- 一起Talk Android吧(第十二回:Java中的多态)
- 一起Talk Android吧(第二十回:Java常用类之Date续)
- 一起Talk Android吧(第十六回:Java常用类之String)
- 一起Talk Android吧(第六回:Java中的函数)
- 一起Talk Android吧(第十八回:Java常用类String VS StringBuffer)
- 一起Talk Android吧(第十九回:Java常用类之Date)
- 一起Talk Android吧(第三回:Java中的变量类型)
- 一起Talk Android吧(第二十四回:Java多线程编程二)
- 一起Talk Android吧(第二十一回:Java常用类之文件操作一)
- 一起Talk Android吧(第五回:Java中的程序结构)
- 一起Talk Android吧(第八回:Java中的面向对象)
- 一起Talk Android吧(第十一回:Java中的继承)