第 五 章

決定

5.1 if 敘述

if (amount <= balance)
balance = balance - amount;

if/else 敘述

if (amount <= balance)
balance = balance - amount;
else
balance = balance - OVERDRAFT_PENALTY;


Block 敘述

if (amount <= balance)
{
double newBalance = balance - amount;
balance = newBalance;
}

Syntax 5.1. if 敘述

  if(condition)
   statement

if
(condition)
   statement

else
   statement

例題:

 
if (amount <= balance)
balance = balance - amount;

if (amount <= balance)
balance = balance - amount;
else
balance = balance - OVERDRAFT_PENALTY;

用途:

To execute a statement when a condition is true or false

Syntax 5.2. Block 敘述

  {
   statement
   statement
   . . .

}


例題:

 
{

   double newBalance = balance - amount;
   balance = newBalance;

}

用途:

To group several statements together to form a single statement

敘述型式

5.2 數值的比較

關係運算子

Java
Math Notation
Description
>
>
Greater than
>=
≥
Greater than or equal
<
<
Less than
<=
≤
Less than or equal
==
=
Equal
!=
≠
Not equal


相等和指定


浮點數比較

浮點數比較

字串比較

依字典的次序比較(Lexicographic Comparison)

Java 所用的次序,數字在前,接著大寫字母,再接著小寫字母。

依字典的次序比較


物件比較

物件比較


The null Reference

5.3 多重選擇(Multiple Alternatives)

File Earthquake.java


/**
   A class that describes the effects of an earthquake.
*/
public class Earthquake
{
   /**
      Constructs an Earthquake object.
      @param magnitude the magnitude on the Richter scale
   */
   public Earthquake(double magnitude)
   {
      richter = magnitude;
   }

   /**
      Gets a description of the effect of the earthquake.
      @return the description of the effect
   */
   public String getDescription()
   {
      String r;
      if (richter >= 8.0)
         r = "Most structures fall";
      else if (richter >= 7.0)
         r = "Many buildings destroyed";
      else if (richter >= 6.0)
         r = "Many buildings considerably damaged, some collapse";
      else if (richter >= 4.5)
         r = "Damage to poorly constructed buildings";
      else if (richter >= 3.5)
         r = "Felt by many people, no destruction";
      else if (richter >= 0)
         r = "Generally not felt by people";
      else
         r = "Negative numbers are not valid";
      return r;
   }

   private double richter;
}


File EarthquakeTest.java


import javax.swing.JOptionPane;

/**
   A class to test the Earthquake class.
*/
public class EarthquakeTest
{
   public static void main(String[] args)
   {
      String input = JOptionPane.showInputDialog(
         "Enter a magnitude on the Richter scale:");
      double magnitude = Double.parseDouble(input);
      Earthquake quake = new Earthquake(magnitude);
      System.out.println(quake.getDescription());
      System.exit(0);
   }
}


多重選擇

多層分支(Nested Branches)

所得稅申報

假如婚姻狀況是單身

所得總額超過
但未超過
稅額為
總額超過
$0
$21,450
15%
$0
$21,450
$51,900
$3,217.50 + 28%
$21,450
$51,900

$11,743.50 + 31%
$51,900

假如婚姻狀況是已婚

所得總額超過
但未超過
稅額為
總額超過
$0
$35,800
15%
$0
$35,800
$86,500
$5,370.00 + 28%
$35,800
$86,500

$19,566.00 + 31%
$86,500

所得稅申報流程圖


File TaxReturn.java


/**
   A tax return of a taxpayer in 1992.
*/
class TaxReturn
{
   /**
      Constructs a TaxReturn object for a given income and
      marital status.
      @param anIncome the taxpayer income
      @param aStatus either SINGLE or MARRIED
   */
   public TaxReturn(double anIncome, int aStatus)
   {
      income = anIncome;
      status = aStatus;
   }

   public double getTax()
   {
      double tax = 0;

      if (status == SINGLE)
      {
         if (income <= SINGLE_CUTOFF1)
            tax = RATE1 * income;
         else if (income <= SINGLE_CUTOFF2)
            tax = SINGLE_BASE2
               + RATE2 * (income - SINGLE_CUTOFF1);
         else
            tax = SINGLE_BASE3
               + RATE3 * (income - SINGLE_CUTOFF2);
      }
      else
      {
         if (income <= MARRIED_CUTOFF1)
            tax = RATE1 * income;
         else if (income <= MARRIED_CUTOFF2)
            tax = MARRIED_BASE2
               + RATE2 * (income - MARRIED_CUTOFF1);
         else
            tax = MARRIED_BASE3
               + RATE3 * (income - MARRIED_CUTOFF2);
      }

      return tax;
   }

   public static final int SINGLE = 1;
   public static final int MARRIED = 2;

   private static final double RATE1 = 0.15;
   private static final double RATE2 = 0.28;
   private static final double RATE3 = 0.31;

   private static final double SINGLE_CUTOFF1 = 21450;
   private static final double SINGLE_CUTOFF2 = 51900;

   private static final double SINGLE_BASE2 = 3217.50;
   private static final double SINGLE_BASE3 = 11743.50;
   private static final double MARRIED_CUTOFF1 = 35800;
   private static final double MARRIED_CUTOFF2 = 86500;

   private static final double MARRIED_BASE2 = 5370;
   private static final double MARRIED_BASE3 = 19566;

   private double income;
   private int status;
}


File TaxReturnTest.java


/**
   A tax return of a taxpayer in 1992.
*/
class TaxReturn
{
   /**
      Constructs a TaxReturn object for a given income and
      marital status.
      @param anIncome the taxpayer income
      @param aStatus either SINGLE or MARRIED
   */
   public TaxReturn(double anIncome, int aStatus)
   {
      income = anIncome;
      status = aStatus;
   }

   public double getTax()
   {
      double tax = 0;

      if (status == SINGLE)
      {
         if (income <= SINGLE_CUTOFF1)
            tax = RATE1 * income;
         else if (income <= SINGLE_CUTOFF2)
            tax = SINGLE_BASE2
               + RATE2 * (income - SINGLE_CUTOFF1);
         else
            tax = SINGLE_BASE3
               + RATE3 * (income - SINGLE_CUTOFF2);
      }
      else
      {
         if (income <= MARRIED_CUTOFF1)
            tax = RATE1 * income;
         else if (income <= MARRIED_CUTOFF2)
            tax = MARRIED_BASE2
               + RATE2 * (income - MARRIED_CUTOFF1);
         else
            tax = MARRIED_BASE3
               + RATE3 * (income - MARRIED_CUTOFF2);
      }

      return tax;
   }

   public static final int SINGLE = 1;
   public static final int MARRIED = 2;

   private static final double RATE1 = 0.15;
   private static final double RATE2 = 0.28;
   private static final double RATE3 = 0.31;

   private static final double SINGLE_CUTOFF1 = 21450;
   private static final double SINGLE_CUTOFF2 = 51900;

   private static final double SINGLE_BASE2 = 3217.50;
   private static final double SINGLE_BASE3 = 11743.50;
   private static final double MARRIED_CUTOFF1 = 35800;
   private static final double MARRIED_CUTOFF2 = 86500;

   private static final double MARRIED_BASE2 = 5370;
   private static final double MARRIED_BASE3 = 19566;

   private double income;
   private int status;
}


使用布林(boolean)式

boolean 型式

斷定性方法(Predicate Method)

布林運算子

&& 和 || 運算子


真實表(Truth Tables)

A
B
A && B
true
true
true
true
false
false
false
Any
false
A
B
A || B
true
Any
true
false
true
true
false
false
false
A
! A
true
false
false
true

De Morgan's 定律

布林變數