零基礎學Android開發之Java語言學習02-基本文法

來源:互聯網
上載者:User

標籤:

第一個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";
     
    }
  */
  
}

 

零基礎學Android開發之Java語言學習02-基本文法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.