1. 程式人生 > >JAVA基礎入門知識之基本語法知識

JAVA基礎入門知識之基本語法知識

本文章包含了java中的基礎語法知識【程式碼的基本格式,變數,判斷語句,迴圈語句,方法,一維陣列】。基本上與C語言差不多,其中在switch語句時,java由於JDK7.0的新特性,可以接受String型別的值;而且java中的方法支援過載,而C語言不支援重;陣列的定義方式也與C語言不同。所以其他語言學的不錯的,可以只用看這幾部分。

1,java程式碼的基本格式:   修飾符 class  類名{

                                                        程式程式碼

                                                         }

     注意*^*:a,程式程式碼分為:結構定義語句 and 功能執行語句;

              b,嚴格區分大小寫;

              c,一句連續的字串不能分開在倆行中書寫    eg:System.out.println("這是第一個

                                                                                                                  java程式");

2,java中的變數:  

                        (1)基本資料型別:

                                     a,整型【byte,int,short,long】

                                     b,浮點型【float,double】

                                     c,字元型【char】

                                     d,布林型【boolean】

                          (2)型別轉換:  目標型別  變數名 = (目標型別)值

                                                           int a = 1;

                                                           float  b = (float)a;//將整型變數轉化為浮點型

3,java中的運算子:  

                                   +  -  *  /   % 

                                  ==  !=  <  >   <=   >=

                                  &  |   !

4,選擇結構語句:

                                 if(判斷條件){                                           if(判斷條件1){

                                                 執行語句1;                                                            執行語句1;

                                  }else{                                                                 }else if(判斷條件2){

                                                執行語句2;                                                               執行語句2;

                                  }                                                                          }……

                                                                                                               else if(判斷條件n){

                                                                                                                                       執行語句n;

                                                                                                                  }else{

                                                                                                                              執行語句n+1;

                                                                                                                  }

                            switch (表示式){              //表示式只能是byte,short,char,int型別                                              

                                      case 目標值1:          //注意是冒號

                                             執行語句1;

                                              break;               //別忘了break

                                          ……

                                      case 目標值n:

                                             執行語句n;

                                              break;

                                       default:

                                            執行語句n+1;

                                             break;

                                     }

   Example 01(switch 迴圈的運用)          

import java.util.Scanner;


public class Hello {

	private static int day;

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		System.out.println("請輸入一個阿拉伯數字:\n");
		Scanner in = new Scanner(System.in);
		day = 0;
		day = in.nextInt();
		switch(day){
		case 1: 								//注意冒號
			System.out.println("星期一");
			break;								//別忘了break 跳出迴圈
		case 2:
			System.out.println("星期二");
			break;
		case 3:
			System.out.println("星期三");
			break;
		case 4:
			System.out.println("星期四");
			break;
		case 5:
			System.out.println("星期五");
			break;
		case 6:
			System.out.println("星期六");
			break;
		case 7:
			System.out.println("星期日");
			break;
		default:
			System.out.println("輸入數字不正確");
			break;
		}

	}

}

5,迴圈結構語句:

                                      a,while迴圈:        while(迴圈條件){

                                                                                                 執行語句;

                                                                                                        }

Example 02 (while迴圈)

public class Hello{

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int x = 0;            //等於號倆邊有空格 可以使程式碼更好看
		while (x <= 4){
			System.out.println("x="+x);
			x++;
		}

	}

}

                                     b,do……while迴圈:    do{

                                                                                            執行語句

                                                                                      }while(迴圈條件);

Example 03 (do……while迴圈)

public class Hello{

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int x = 0;
		do{
			System.out.println("x="+x);
			x++;
		}while(x <= 4);
	}

}

example 04  (while迴圈 and do……while迴圈的區別)

public class Hello{

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int x = 0;
		do{
			System.out.println("x="+x);
			x++;
		}while(x < 0);
		System.out.println("*********************************");
		int y = 0;
		while (y < 0){
			System.out.println("y="+y);
			y++;
		}
	}

}

do……while迴圈會先執行一次,不管條件是否成立;而while迴圈必須要滿足條件才會執行。

                                           c,for迴圈:          for(A初始化表示式;B迴圈條件;C操作表示式){

                                                                                                 D執行語句;

                                                                                              }

                                                                            【1】,執行A;【2】,執行B,if 為真,執行第3步;if 為假,執行第5步; 【3】,執行D

                                                                            【4】,執行C,然後重複執行第2步; 【5】,退出迴圈。

Example 05 (for迴圈)

public class Hello{

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int sum = 0;
		for (int i=1;i <= 4;i++){
			sum += i;
		}
		System.out.println("sum="+sum);
	}

}

                                     d,跳轉語句(break ,continue):     【1】break 是跳出迴圈,如果有巢狀迴圈時,只能跳出內層迴圈;

                                                                                                           【2】continue 是終止本次迴圈,執行下一次迴圈;

Example 06(break & continue 的比較)

public class Hello{

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		System.out.println("break & continue 的比較:\n");
		for (int x=1;x <= 5;x++){
			if (x==3)
				break;
			System.out.println("x="+x);
		}
		System.out.println("--------------------------");
		for (int y=1;y <= 5;y++){
			if (y==3)
				continue;
			System.out.println("y="+y);
		}
		System.out.println("****************************");
		System.out.println("continue語句的應用 求1-100之內奇數和:\n");
		int sum = 0;
		for (int i=1;i <= 100;i++){
			if (i%2==0)
				continue;
			sum += i;
		}
		System.out.println("sum="+sum);
	}

}

7,方法:(* ^ *:感覺這個和其他語言的函式類似)

Example 07 (方法的定義與使用:修飾符 返回值型別 方法名(引數型別 引數名1,……引數型別 引數名n){

                                                                           執行語句;

                                                                       return 返回值;

                                                                          }                             

                           )

public class Hello{

	/**
	 * @param args
	 */
	//定義一個printRectangle的方法,接受倆個引數
	public static void printRectangle(int height,int width){
		for(int i=0;i<height;i++){
			for(int j=0;j<width;j++){
				System.out.print("*");
			}
			System.out.print("\n");
		}
		System.out.print("\n");
	}
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		printRectangle(3,5);        //呼叫printRectangle()方法
		printRectangle(2,4);
		printRectangle(6,10);
	}

}

Example 08 (方法的過載 :可以定義多個名稱相同的方法,但是引數型別或個數必須不同

public class Hello{

	/**
	 * @param args
	 */
	public static int add(int x,int y){
		return x+y;
	}
	
	public static int add(int x,int y,int z){
		return x+y+z;
	}
	
	public static double add(double x,double y){
		return x+y;
	}
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int sum1 = add(1,2);
		int sum2 = add(1,2,3);
		double sum3 = add(1.2,1.3);
		System.out.println("sum1="+sum1);
		System.out.println("sum2="+sum2);
		System.out.println("sum3="+sum3);
	}

}

Example 09: (方法的遞迴)

public class Hello{

	/**
	 * @param args
	 */
	public static int getSum(int n){
		if (n==1)            //滿足條件 遞迴結束
			return 1;
		int temp = getSum(n-1);
		return temp+n;
	}
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int sum = getSum(4);
		System.out.println("sum="+sum);
	}

}

8,陣列:(定義:型別  [ ]  陣列名 = { 元素1,……};)

Example 10:(陣列遍歷)

public class Hello{

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int [] arr = {1,2,3,4,5};
		for(int i=0;i<arr.length;i++){
			System.out.println("arr["+i+"]="+arr[i]);
		}
	}

}

Example 11:(陣列最值)

public class Hello{

	/**
	 * @param args
	 */
	public static int getMax(int [] arr){
		int max = arr[0];
		for(int i=1;i<arr.length;i++){
			if(arr[i] > max)
				max = arr[i];
		}
		return max;
	}
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int [] arr = {4,1,6,3,8,9};
		int max = getMax(arr);
		System.out.println("max="+max);
	}
}

Example 11:(陣列排序)

public class Hello{

	/**
	 * @param args
	 */
	public static void printArray(int [] arr){
		for(int i=0;i<arr.length;i++){
			System.out.print(arr[i]+" ");
		}
		System.out.print("\n");
	}
	
	public static void bubbleSort(int [] arr){
		for(int i=0;i<arr.length-1;i++){
			for(int j=0;j<arr.length-i-1;j++){
				if (arr[j] > arr[j+1]){
					int temp = arr[j];
					arr[j] = arr[j+1];
					arr[j+1] = temp;
				}
			}
			System.out.print("第"+(i+1)+"輪排序後: ");
			printArray(arr);
		}
	}
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int [] arr = {25,24,12,76,101,96,28};
		System.out.print("氣泡排序前:");
		printArray(arr);
		bubbleSort(arr);
		System.out.print("氣泡排序後:");
		printArray(arr);
	}
}