星期六, 4月 30, 2011

2011.04.30 SL100-1,2

//JDK-編輯java程式使用
//JRE-跑JAVA程式使用
//編輯語法軟體JCreator
//以建立專案Project 管理java原始檔
// Basic JAVA Application 針對程式
// Basic JAVA Applet針對網頁
// Empty Project空白專案

//JAVA中JAVA File 檔名很重要(分大小寫)
//*.java==>原始程式
//*.class==>編譯後的class檔

//物件導向中重要觀念 "Encapsultion 封裝"


public class Test
//定義class java語法須有一個public class,public class Name 須與檔名依樣
//public class Name 即是編譯後產生的"*.class" 檔名
{
public static void main(String[] args)
//設定function物件導向
{
System.out.println("Hello"); //輸出字串
//println 輸出字串至螢幕斷行 print輸出字串至螢幕不斷行
System.out.println('1'); //輸出字元

int a=100; //指定變數
System.out.println(a);

a=200; //重新指定變數內容
System.out.println(a);

System.out.println(1+1); //運算式
System.out.println(7/2); //結果會為3,若兩數皆為整數則除出來會是整數
System.out.println(7.0/2);//若要算出結果為3.5須使使用7.0/2才會是浮點數
System.out.println("aaa" + a); //字元運算可加邊數
System.out.println("aaa" + "bbb"); //字元運算串雙字串

}
}


//資料表示方法;
/*常數Consof-程式執行中不能被改變的資料,直接將資料內容寫入程式
("Hello")字串常數以雙引號表示*/

//type 資料型別(常數)
//文字
//字元-'a' 'b' '中'
//字串-"abc"
//數字
//整數- 125
//浮點數- 125.0
//布林boolean
//true
//false


//變數Variable-程式執行中可以被改變的資料,需要經過宣告
//型別名稱 變數名稱1,2,....; (語法)
//#int a;
//#a=100;
//#a=75 >int 需要使用正確型別-整數=int
//#a=75.2 >double 需要使用正確型別-浮點數=double
//#a=75.2f >float 需要使用正確型別-浮點數加上才等於float
//#int a=100;
//型別名稱可用名稱
//整數;
//byte 1byte -128至127
//short 2byte -32768至32767
//int 4byte
//long 8byte
//浮點數
//float 4byte
//double 8byte
//字元
//char 2byte
//布林
//boolean 2byte
//變數名稱規則
//首字必須為英文字或$或 _ 且不可以使用保留字
//同一變數不可再同一區塊被宣告兩次
//變數為一記憶體空間
//變數資料可以隨時被重新利用

//運算元operator
//1.算數運算元 (1+1)
// +
// -
// *
// / 求商
// % 求餘數

//2.字串運算 ("123"+"123") 字串相加
// + 字串相加 只須有一端為字串

//3.指定運算元 =左邊一定要是變數,且只能有一變數,指定變數專用
// =
// += a += b; > a=a+b;
// -=
// *=
// /=
// %=

//強制轉型
//int a,b;
//double c;
//a=7;
//b=2;
//c=a/b; 原先算出為3.0
//c=(double)a/b;利用強制轉行在運算事前加入(型別)即可算處3.5
//強制轉型lab
public class Test2
{
public static void main(String[] args)
{
int a,b;
double c;
a=7;
b=2;
c=(double)a/b; //運算式前加入型態

System.out.println("c=" + c);
}
}

//累加運算 ++ a++/++a > a=a+1 a++ 為後加 ++a為先加
//累減運算 --

//累加運算lab
public class Test3
{
public static void main(String[] args)
{
int a,b,c;
a=7;
b=5;
//c=a+++b;語法不能這樣下
c=a+ ++b; //可以改為此
//c=++a+b; 或改為此
//c=a+b++;

System.out.println("a=" + a);
System.out.println("b=" + b);
System.out.println("c=" + c);
}
}


//關係運算元 true or false 判斷左右是否符合條件
// >
// >=
// <
// <=
// != 不等於
// ==
//邏輯運算
// ! Not
// &&(短路) 而且 &(非短路)
// ||(短路) 或者 |(非短路)
//短路-當前面以運算以不成立則不執行後面運算
//非短路-不管前面運算成不成立後面運算皆會執行

//lab
public class Test4
{
public static void main(String[] args)
{
System.out.println(true && false); //
System.out.println(true & false);
System.out.println(true || false);
System.out.println(true | false);

int a=1,b=2;
//System.out.println(a++ > 1 && b++ <2);
使用&&為短路,故前段運算是式等於false則不會執行後段運算式
System.out.println(a++ > 1 & b++ <2);
//使用&為不短路,則不論前段運算式是否為ture皆會執行後段運算式
System.out.println("a=" +a);
System.out.println("b=" +b);
}
}

沒有留言: