顯示具有 Java SE::Java 基礎語法 標籤的文章。 顯示所有文章
顯示具有 Java SE::Java 基礎語法 標籤的文章。 顯示所有文章

2015年9月16日 星期三

Method 的可變長度引數

方法中,可變長度的引數宣告:
public class Statistics {
    public float average(int... nums){
    
        int sum = 0;
        for (int x : nums){
            sum += x;
        }
        return ((float)sum / nums.length);
    }
}

使用方式:
public class Demo {
    public static void main(String[] args){
    
        Statistics abc = new Statistics();
        int[] nums = {32,67,98,34,76};
        System.out.println("Average: " + abc.average(nums));
    }
}

2015年8月31日 星期一

Overload 的語法

Java 的 Overload 語法:

import java.util.Scanner;

public class blenderDemo{

 public static void main(String[] args){
 
  Scanner scanner = new Scanner(System.in);
  Blender myBlender = new Blender();
  
  System.out.print("請選擇水果種類:1) 蘋果  2) 香蕉 3) 芒果");
  int fruit = scanner.nextInt();
  
  switch(fruit){
   case 1:
    System.out.printf("蘋果汁:%d CC",myBlender.makeJuice(1));
    break;
   case 2:
    System.out.printf("香蕉牛奶:%d CC",myBlender.makeJuice(2,100));
    break;
   case 3:
    System.out.printf("芒果冰沙:%.2f CC",myBlender.makeJuice(3,100,200));
    break;
  } 
 }
}

public class Blender{

 public int makeJuice(int fruit){
  int juice = 0;
  switch(fruit){
   case 1:
    juice = 150;
    break;
   case 2:
    juice = 300;
    break;
   case 3:
    juice = 400;
    break;
  }   
  return juice;
 }
 
 public int makeJuice(int fruit, int milk){
  return (this.makeJuice(fruit)+ milk);
 }
 
 public double makeJuice(int fruit, int milk, int ice){
  return (this.makeJuice(fruit,milk)+ ice)/2.0;
 }
}

2015年8月28日 星期五

Java 迴圈語法

Java 迴圈語法:while 、do/while 、for 迴圈

While 迴圈簡單範例:
public class TaxDemo{

 public static void main(String[] args){

  double balance = 500;
  double taxRate = 0.07;
  int years = 0;
  
  while (balance <= 1000){
  
   balance = (balance * (1+0.07));
   years++;
  
  }
  System.out.printf("Year %d: %.2f",years,balance);
 }
} 

修改一下 Elevator 的程式:(其他部份,請參考這裡!)
public class Elevator{
...omit....

//判斷是否到達所須要的樓層
 public void goToFloor(int desiredFloor){
  while (this.currentFloor != desiredFloor){
   if (this.currentFloor > desiredFloor){
    this.goDown();
   } else {
    this.goUp();
   }
  }
  System.out.println("Arrived..");
  this.openDoor();
 }

..... omit ......


執行的程式也一併修改一下:
import java.util.Scanner;

public class ElevatorDemo{

 public static void main(String[] args){
 
  Elevator myElevator = new Elevator();
  Scanner scanner = new Scanner(System.in);
  
  System.out.print("請選擇樓層(1~10):");
  myElevator.doorOpen = true;
  myElevator.goToFloor(scanner.nextInt()); 
  
 }
}


for 迴圈用法:(99乘法表)
public class table99{

 public static void main(String[] args){
 
  for (int i = 1,j = 1; i < 10 ; i=(j==9)?(i+1):(i),j=(j==9)?(1):(j+1)){
   System.out.printf("%d*%d=%d\t",i,j,(i*j));
   if (j==9){
    System.out.println();
   }
  }
 }
}

continue 與 break 也來湊熱鬧:
import java.util.*;

public class ScoreClassDemo{

 public static void main(String[] args){
 
  Scanner scanner = new Scanner(System.in);
  ArrayList pass = new ArrayList();
  ArrayList noPass = new ArrayList();
  
  int account = 0;
  int score = 0;
  
  while (true){
   System.out.printf("請輸入第 %d 位學生成績:",(account + 1));
   score = scanner.nextInt();
   if ((score > 100)||(score <= -2)){
    System.out.println("重新輸入");
    continue;
   } else if (score == -1){
    break;
   } else if ( score >= 60){
    pass.add(score);
   } else {
    noPass.add(score);
   }
   account++;
  }
  System.out.println("及格人數: " + pass.size());
 }
}

2015年8月24日 星期一

Java 決策判斷語法

Java 的關係運算子:== 、!= 、 > 、 >= 、 < 、 <= 、equals() ... 判斷的語法: if 、 if/else 、switch
先來點熱身的:
public class Employees{
 
 public String name1 = "Fred Smith";
 public String name2 = "Joseph Smith";
 
 public static void main(String[] args){
  Employees myEmployee = new Employees();
  myEmployee.areNamesEqual();
 }
 
 public void areNamesEqual(){
 
  //比較兩者是否為同一物件
  if ( name1 == name2 ){
   System.out.println("Same Object.");
  } else {
   System.out.println("Different Object.");
  }
 
  //比較物件內容值是否相同
  if (name1.equals(name2)){
   System.out.println("Same Name.");
  } else {
   System.out.println("Different Name.");
  }
 }
}
以電梯的類別來示範 if/else 以及 nested if/else 用法:
public class Elevator{

 public boolean doorOpen = false;
 public int currentFloor = 1;
 public final int TOP_FLOORS = 10;
 public final int MIN_FLOORS = 1;
 
 //開門的動作
 public void openDoor(){
  System.out.println("Opening Door ... ");
  doorOpen = true;
  System.out.println("Door is opened!");
 }
 
 //關門的動作
 public void closeDoor(){
  System.out.println("Closing Door ... ");
  doorOpen = false;
  System.out.println("Door is closed!");
 }
 
 //電梯向上
 public void goUp(){
  if ( currentFloor >= TOP_FLOORS){
   System.out.println("Cannot go Up!");
  } else {
    if (doorOpen){
 closeDoor();
    }

   System.out.println("Going Up one floor !");
   currentFloor++;
   System.out.println("Floor: " + currentFloor);
  }
 }

//電梯向下
 public void goDown(){
  if ( currentFloor <= MIN_FLOORS){
   System.out.println("Cannot go Down!");
  } else {

   if (doorOpen){
 closeDoor();
   }

   System.out.println("Going Down one floor !");
   currentFloor--;
   System.out.println("Floor: " + currentFloor);
  }
 }
}
執行看看....
public class ElevatorDemo{

 public static void main(String[] args){
 
  Elevator myElevator = new Elevator();
  
  myElevator.openDoor();
  myElevator.closeDoor();
  myElevator.goDown();
  myElevator.goUp();
  myElevator.goUp();
 }
}

一種 Low Low 的範例:
import java.util.Scanner;

public class MonthDemo{

 public static void main(String[] args){
 
  Scanner scanner = new Scanner(System.in);
  System.out.print("請輸入月份:");
  int month = scanner.nextInt();
  if ( month == 1 || month == 3 ||month == 5 ||
   month == 7 || month == 8 || month == 10 ||
   month == 12 ){
    System.out.println("本月份有31天!");
   } else if (month == 2){
    System.out.println("本月份有28天!");
   } else if (month == 4 || month == 6 ||
   month == 9 || month == 11){
    System.out.println("本月份有30天!");
   }else{
    System.out.println("invalid days !");
  }
  
 }
}

一種好的範例:
import java.util.Scanner;

public class SwitchDemo{

 public static void main(String[] args){
 
  Scanner scanner = new Scanner(System.in);
  System.out.print("請輸入月份:");
  int month = scanner.nextInt();
  switch (month){
   case 1:
   case 3:
   case 5:
   case 7:
   case 8:
   case 10:
   case 12:
    System.out.println("本月份有31天!");
    break;
   case 2:
    System.out.println("本月份有28天!");
    break;
   case 4:
   case 6:
   case 9:
   case 11:
    System.out.println("本月份有30天!");
    break;
   default:
    System.out.println("invalid days !");
  }
  
 }
}

2015年8月19日 星期三

Java 基本運算子的使用

Java 的算術運算子:+ - * / %
public class Person{

 public int ageYears = 32;
 
 public void calculateAge(){
 
  int ageDays = ageYears * 365;
  long ageSeconds = ageYears * 365 * 24L * 60 * 60;
 
  System.out.println("You are " + ageDays + " days old.");
  System.out.println("You are " + ageSeconds + " seconds old.");
 }
}
執行它:
public class PersonDemo{

 public static void main(String[] args){
 
  Person peter = new Person();
  peter.calculateAge();
 
 }
}

配合 java.util.Scanner 物件來做為輸入數量的參考:
import java.util.Scanner;

public class Demo{

 public static void main(String[] args){
 
  Shirt myShirt = new Shirt();
  myShirt.shirtID = 100;
  myShirt.colorCode = 'B';
  myShirt.price = 45.12;
  myShirt.description = "45周年紀念衫";
  
  myShirt.displayInformation();
 
  Scanner scanner = new Scanner(System.in);
  System.out.print("請輸入購買件數:");
  int input = scanner.nextInt();
  System.out.println("總價:" + input*myShirt.price);
 
 }
}

Java 基本資料型態

Java 變數的資料型態分兩種:基本資料型態類別資料型態

基本資料型態:
  • 整數型態:byte (8 bits)、short (16 bits)、int (32 bits)、long (64 bits)
  • 浮點數型態:float ( 32 bits) 、double (64 bits)
  • 文字型態: char (16 bits)
  • 邏輯型態:boolean 

2015年8月17日 星期一

Java 基本程式撰寫

Java 基本程式寫作方式:(通常第一支程式都是 Hello World !!)

public class HelloWorld {

    public static void main (String[] args) {
        System.out.println("Hello, world!");
    }
}

  • class <類別名稱>: 因為 Java 語言是物件導向語言,所以都是以 class 開頭來撰寫程式!而類別名稱,則是給這個類別命名,方便其他程式的呼叫!
  • public ... : 表示公開的權限!Java 語言權限,是代表其安全機制的由來,權限共分四級!利用適當的權限值,可確保程式被利用時的安全性!
  • public static void main(String[] args){...} : 表示 Java 程式開始執行的進入點。
  • System.out.println(...) :表示在文字介面視窗中,印出所需要的文字!
  • ; 分號表示程式表示式結束的描述,除了使用 {} 之外,每行 Java 程式描述句結束後,都應加上此符號!

接下來,將檔案存成與<類別名稱>相同的 <類別名稱.java> 檔案,例如本程式應存成 HelloWorld.java 檔案!
之後,將該檔案進行編譯,變成可執行的 Class 檔案!例如:
C:\workspace\test1> javac HelloWorld.java

最後執行該 Java 程式,應使用 Java 執行該 class 檔案!例如:
C:\workspace\test1> java HelloWorld

JDK 安裝與設定

Java 語言的撰寫,最重要的一件事,就是準備好工具!而最重要的工具,就是 JDK(Java Development Kits) !JDK 安裝與設定如下:
  1. Oracle 官方下載 JDK!
  2. 進行「下一步」到底的安裝!
  3. 在電腦的環境設定中,設定 Path 環境項目,加入 Java bin 的目錄!
  4. 利用下列指令測試:
  • javac  -version
  • java  -version

另一種重要的工具,就寫程式的軟體了!通常,利用文字編輯器即可!以下提供常見工具:
  1. Notepad++ : 純文字編輯器!台灣人寫的,給正要入門的新手,最好的寫作平台!
  2. NetBean : Oracle 官方的 IDE (Integrated Development Environment),簡單、易用!
  3. Eclipse : 業界常用!有多方開發的整合套件,方便開發任何 Java 程式語言!

最後一種常用工具: Java API Documents !! (Java SE 8)因為,沒人可以記得住 Java 所有可用 API ,所以,查文件也是一種功力的表現!