2015年8月31日 星期一

Static 的用法

Java 中的 Static 用法:

import java.util.Scanner;

public class blenderDemo2{
 static int juice = 0;
 
 public static void makeJuice(int fruit){
  
  switch(fruit){
   case 1:
    juice += 150;
    break;
   case 2:
    juice += 300;
    break;
   case 3:
    juice += 400;
    break;
  }   
 }

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