零基础学Android开发之Java语言学习02-基本语法

第一个Java程序

下面看一个简单的Java程序,它将打印字符串 Hello World

//com.henancaiyun 包,主要用来解决同名问题。每一个类都要指定在一个包下面。
package com.henancaiyun;

//导入ClassA
import com.henancaiyun.ClassA;

//HelloWord类
public class HelloWord {
 
 //Main静态函数
 public static void main(String[] args )
 {
  /* 这是第一个Java程序
      *它将打印Hello World
      * 这是一个多行注释的示例
  */
  //字符串变量helloWord
  String helloWord="Hello Word";
  //系统输出Hello Word字符
  System.out.println(helloWord);
  
 }

}

------------------------------------------------------------------------------------------

//com.henancaiyun 包,主要用来解决同名问题。每一个类都要指定在一个包下面。
package com.henancaiyun;

import java.sql.Date;

//HelloWord类
public class HelloWord {
 
 
 //Main静态函数
 public static void main(String[] args )
 {
  /* 这是第一个Java程序
      *它将打印Hello World
      * 这是一个多行注释的示例
  */
  //字符串变量helloWord
  String helloWord="Hello Word";
  
  //系统输出Hello Word字符
  System.out.println(helloWord);
  
  CheckStudey();
  
 }
 
 //数据类型
 public static void DataTypeStudey()
 {
  byte h=10;
  short i=20;
  int j=30;
  long k=30;
  float l=0.000000000000000000000000000000000000000000001f;
  double m=0.0000000000000000000000000000000000000000000000000000000000000000000000000001;
  boolean flag=true;
  char c='1';
  String str="I am Li bao qing";
  int [] intArray=new int []{1,2,3,4,5};
  intArray[0]=0;
  String [] strArray=new String []{"1","2","3"};
  System.out.println(intArray[0]);

 }
 //变量
 public static void VeriableStudey()
 {
  int a=1;
 
  final int b=10;
  
  a=10;
  a=11;
  
  System.out.println(a);
  
 }
 
     //运算符
  public static void ComputeChatStudy()
  {
   /*算术运算符
   + 加法 - 相加运算符两侧的值 A + B等于30
   - 减法 - 左操作数减去右操作数 A – B等于-10
   * 乘法 - 相乘操作符两侧的值 A * B等于200
   / 除法 - 左操作数除以右操作数 B / A等于2
   % 取模 - 左操作数除右操作数的余数 B%A等于0
   + + 自增 - 操作数的值增加1 B + +等于21
   -- 自减 -- 操作数的值减少1 B - -等于19
   */
   int a=1;
   int b=2;
   int c=a+b;
   c++;
   
   System.out.println(c);


   /*关系运算符
    * == 检查如果两个操作数的值是否相等,如果相等则条件为真。 (A == B)为假(非真)。
     != 检查如果两个操作数的值是否相等,如果值不相等则条件为真。 (A != B) 为真。
     >  检查左操作数的值是否大于右操作数的值,如果是那么条件为真。 (A> B)非真。
     <  检查左操作数的值是否小于右操作数的值,如果是那么条件为真。 (A <B)为真。
     > = 检查左操作数的值是否大于或等于右操作数的值,如果是那么条件为真。 (A> = B)为假。
     <= 检查左操作数的值是否小于或等于右操作数的值,如果是那么条件为真。 (A <= B)为真。
   */
   
   
   boolean flag=(a==b);
   
   System.out.println(flag);
   
   /*逻辑运算符
   && 称为逻辑与运算符。当且仅当两个操作数都为真,条件才为真。 (A && B)为假。
   | | 称为逻辑或操作符。如果任何两个操作数任何一个为真,条件为真。 (A | | B)为真。
   ! 称为逻辑非运算符。用来反转操作数的逻辑状态。如果条件为true,则逻辑非运算符将得到false。
   */
   boolean flag2=(a==b&&b==c);
   
   System.out.println(flag);
   

   /*赋值运算符
   = 简单的赋值运算符,将右操作数的值赋给左侧操作数 C = A + B将把A + B得到的值赋给C
   + = 加和赋值操作符,它把左操作数和右操作数相加赋值给左操作数 C + = A等价于C = C + A
   - = 减和赋值操作符,它把左操作数和右操作数相减赋值给左操作数 C - = A等价于C = C -A
   * = 乘和赋值操作符,它把左操作数和右操作数相乘赋值给左操作数 C * = A等价于C = C * A
   / = 除和赋值操作符,它把左操作数和右操作数相除赋值给左操作数 C / = A等价于C = C / A
    * */
            int h=0;
   h+=2;//(h=h+2)
   System.out.println(flag);
   /*条件运算符
   * 条件运算符也被称为三元运算符。该运算符有3个操作数,并且需要判断布尔表达式的值。该运算符的主要是决定哪个值应该赋值给变量。
      * */
     int i=(h==0?1:0);
     String name=(h==0?"lbq":"lxq");
     System.out.println(flag);
   
  }
  //循环语句
     public static void CircleStudey()
  {
      //while循环
       int x = 10;
          while( x < 20 ) {
             System.out.print("while循环of x : " + x );
             x++;
             System.out.print("\n");
          }
         
          //do…while循环
          x = 10;
          do{
             System.out.print("do…while循环  x : " + x );
             x++;
             System.out.print("\n");
          }while( x < 20 );
         
          //for循环
          for(int i = 10; i < 20; i = x+1) {
          
           if(i==10)
            continue;
                 if(i==12)
                  break;
              System.out.print("for循环of x : " + i );
              System.out.print("\n");
           }

 

  }
    
   //判断语句
     public static void CheckStudey()
  {
      int x = 10;
       //if语句
         if( x < 20 ){
            System.out.print("这是 if 语句");
         }
         //if...else语句
         if( x < 20 ){
             System.out.print("这是 if 语句");
          }else{
             System.out.print("这是 else 语句");
          }
         //if...else if...else语句
         if( x == 10 ){
             System.out.print("Value of X is 10");
          }else if( x == 20 ){
             System.out.print("Value of X is 20");
          }else if( x == 30 ){
             System.out.print("Value of X is 30");
          }else{
             System.out.print("This is else statement");
          }
         //switch语句
         char grade = 'B';

         switch(grade)
         {
            case 'A' :
               System.out.println("Excellent!");
               break;
            case 'B' :
            case 'C' :
               System.out.println("Well done");
               break;
            case 'D' :
               System.out.println("You passed");
            case 'F' :
               System.out.println("Better try again");
               break;
            default :
               System.out.println("Invalid grade");
         }
         System.out.println("Your grade is " + grade);

  }
    
     //方法学习
     public static void Method()
     {
      int a=1;
      int b=2;
      int maxValue=max(a,b);
      
      System.out.println(maxValue);
     }
    
     /** 返回两个整型变量数据的较大值 */
     public static int max(int num1, int num2) {
        int result;
        if (num1 > num2)
           result = num1;
        else
           result = num2;

        return result;
     }
 
     /*
    public static void ClassStudey()
    {
     //一条狗
     Dog hashiShiQi=new Dog();
     hashiShiQi.breed="Hashiqi";
     hashiShiQi.age=1;
     hashiShiQi.color="gray";
     //又一条狗
     Dog muYangQuan=new Dog();
     muYangQuan.breed="Hashiqi";
     muYangQuan.age=1;
     muYangQuan.color="gray";
     
    }
  */
  
}

 

posted @ 2016-09-20 00:06  达博  阅读(321)  评论(0编辑  收藏  举报