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 !");
  }
  
 }
}