2011年7月19日 星期二

2011-07-19 JAVA

// 求出三個人的身高、體重、年齡的最大值並顯示(使用方法)

import java.util.Scanner;


class MyMethod {
     static int max(int a, int b, int c) {
        int max = a;
        if (b > max) max = b;
        if (c > max) max = c;
        return max;
    }

}

class MaxHwaMethod_techer {

    //--- 傳回a、b、c的最大值 ---//
   

    public static void main(String[] args) {
        Scanner stdIn = new Scanner(System.in);
        int[] height = new int[3];        // 身高
        int[] weight = new int[3];        // 體重
        int[] age    = new int[3];        // 年齡

        for (int i = 0; i < 3; i++) {    // 讀入
            System.out.print("[" + (i + 1) + "] ");
            System.out.print("身高:");        height[i] = stdIn.nextInt();
            System.out.print("    體重:");    weight[i] = stdIn.nextInt();
            System.out.print("    年齡:");    age[i]    = stdIn.nextInt();
        }

        int maxHeight = MyMethod.max(height[0], height[1], height[2]);    // 身高的最大值
        int maxWeight = MyMethod.max(weight[0], weight[1], weight[2]);    // 體重的最大值
        int maxAge    = MyMethod.max(age[0], age[1], age[2]);        // 年齡的最大值

        System.out.println("身高的最大值為" + maxHeight + "。");
        System.out.println("體重的最大值為" + maxWeight + "。");
        System.out.println("年齡的最大值為" + maxAge    + "。");
    }
}
----------------------------------------------------------------------------------------------------------------------
import java.util.Scanner;

class MyP7_3 {

  
    static int med(int a, int b, int c) {
        int med = 0;
        if (a >=b && a<=c || (a>b && a<c))
            med = a;
        if (b>=a && b<=c || (b>a && b<c))
            med = b;
        if (c>=a && c<=b || (c>a && c<b))
            med = c;
        return med;
      
    }
  
    public static void main(String[] args) {
        Scanner stdIn = new Scanner(System.in);

        int a = 2;
        int b = 4;
        int c = 6;
        System.out.println("中間值" + med(a,b,c));
    }
}
-------------------------------------------------------------------------------------------------
import java.util.Scanner;

class MyP7_3 {

  
    static int med(int a, int b, int c) {
        int med = 0;
        if (a >=b && a<=c || (a>b && a<c))
            med = a;
        if (b>=a && b<=c || (b>a && b<c))
            med = b;
        if (c>=a && c<=b || (c>a && c<b))
            med = c;
        return med;
      
    }
  
    public static void main(String[] args) {
        Scanner stdIn = new Scanner(System.in);

        System.out.print("整數a:");  int a = stdIn.nextInt();
        System.out.print("整數b:");  int b = stdIn.nextInt();
        System.out.print("整數c:");  int c = stdIn.nextInt();
      
        System.out.println(med(a,b,c));
    }
}
-------------------------------------------------------------------------------------------------
class MyP7_66 {
   static void myArr(int[] a) {     //void不傳回值
      for (int y:a)  //加強型for
        System.out.println(y);
        //return 0;
 
   }
   public static void main(String[] args){
   int[] x={1,2,3,4,5};
   myArr(x); //x變數,copy值給a;
   //int x = myArr(x) 錯誤寫法,因為不傳值
   }

}
----------------------------------------------------------------------------------


import java.util.Random;

class MyP7_8 {

    static int random(int a, int b) {
     Random x = new Random();
        if (a>b)
           return a;
        int y = x.nextInt(b-a);
           return y+a;
      
      
    }
public static void main(String[] args) {

}

  
}
---------------------------------------------------------------------------------------------------





class MyP7_67 {

    static int sumInt(int ...x) {
     int sum = 0;
     for (int y:x) {
        sum=sum+y;
     }
        return sum;
      
    }
public static void main(String[] args) {

  System.out.println(sumInt(10,20,30));
  System.out.println(sumInt(1,2,3,4,5,6,7));
}

}


------------------------------------------------------------------------------------------
lass vlocal {
  static void z(){
  int b=0;
  System.out.println(b);
}
}
class MyP8_66 {
  public static void main(String[] args) {
   vlocal.z();
  }

}
---------------------------------------------------------------------------------------
class vlocal {
  static int a=100;             // class field
  static void va(){
     System.out.println(++a);
}
}
class MyP8_67 {
  public static void main(String[] args) {
   System.out.println(vlocal.a);
  }

}
-----------------------------------------------------------------------------------
  

class vinstance {
    int a=100;

    void pa(){
 System.out.println(a);
 }
}
class MyP8_68 {
    public static void main(String[] args) {
    vinstance x = new vinstance();
    System.out.println(x.a);
    x.pa();
  }
}

   

沒有留言:

張貼留言