返回列表 發帖

自訂函式 (二)

利用函式自訂的技巧, 建立一個專門計算三角形面積的函式.

  1. public class Ch50 {
  2.        
  3.         static float tri(float x, float y)
  4.         {
  5.                 return x*y/2;
  6.         }
  7.        
  8.         public static void main(String[] args)
  9.         {
  10.                 System.out.println("底7公分,高5公分的三角形,面積為"+tri(7,5)+"平方公分。");       
  11.         }
  12. }
複製代碼

  1. import java.util.Scanner;
  2. public class Ch01
  3. {
  4.         static float a(float a,float b)
  5.         {
  6.                 return a*b/2;
  7.         }
  8.         public static void main(String[] args)
  9.         {
  10.                 System.out.println("底7公分,高5公分的三角形,面積為"+a(7,5)+"平方公分");
  11.         }
  12. }
複製代碼

TOP

  1. public class Ch10
  2. {
  3.         static float tri(float a,float b)
  4.         {
  5.                 return a*b/2;
  6.         }
  7.         public static void main(String[] args)
  8.         {
  9.                 System.out.println("底為7,高為5,面積為"+tri(7,5));
  10.         }
  11. }
複製代碼

TOP

  1. public class Ch02 {
  2.         static float tri(float x, float y)
  3.         {
  4.                 return x*y/2;
  5.         }

  6.         public static void main(String[] args) {
  7.                 System.out.println("底7公分,高5公分的三角形面積為"+tri(7,5)+"平方公分");
  8.         }

  9. }
複製代碼

TOP

  1. public class Ch01 {
  2.         static float tri(float a,float b)
  3.         {
  4.                 return a*b/2;
  5.         }
  6.         public static void main(String[]args)
  7.         {
  8.                 System.out.println("底7公分,高5公分的三角形面積為"+tri(7,5)+"平方公分");

  9.         }




  10. }
複製代碼

TOP

  1. public class Ch05
  2. {
  3.         static float tri(float a,float b)
  4.         {
  5.                 return a*b/2;
  6.         }
  7.         public static void main(String[] args)
  8.         {
  9.                 System.out.println("底7公分,高5公分的正方形,面積為"+tri(7,5)+"平方公分。");
  10.         }
  11. }
複製代碼

TOP

  1. import java.util.Scanner;

  2. public class Ch01

  3. {
  4.         static float tri(float x, float y)
  5.         {
  6.                 return x*y/2;
  7.         }

  8.         public static void main(String[] args)
  9.         {
  10.                 System.out.println("底公分,高5公分的三角形,面積為"+tri(7,5)+"平方公分。");     
  11.         }
  12. }       
複製代碼

TOP

  1. public class Ch01{
  2.        
  3.         static float tri(float a, float b)
  4.         {
  5.                return a*b/2               
  6.         }
  7.         
  8.             public static void main(String[] args)
  9.             {
  10.              System.out.println("底7公分,高5公分的三角形,面積為"+tri(7,5)+"平方公分");
  11.             }
  12. }               
複製代碼

TOP

  1. public class Ch02 {
  2.         static float tri(float x, float y)
  3.         {
  4.                 return x*y/2;
  5.         }
  6.         public static void main(String[] args)
  7.         {
  8.                 System.out.println("底7公分,高5公分的三角形,面積為"+tri(7,5)+"平方公分。");
  9.         }
  10. }
複製代碼

TOP

  1. public class Ch03
  2. {
  3.         static void tri(float a,float b)
  4.         {
  5.                 System.out.println("底"+a+"公分,高"+b+"公分的三角形,面積為"+(a*b/2)+"平方公分");
  6.         }       
  7.         public static void main(String[]args)
  8.         {
  9.                 tri(7,5);
  10.         }
  11. }
複製代碼

TOP

  1. import java.util.Scanner;
  2. public class AS02 {
  3.        
  4.         static float tri(float x, float y){
  5.                 return (x*y)/2;
  6.         }

  7.         public static void main(String[] args) {
  8.                 Scanner s=new Scanner(System.in);
  9.                 System.out.print("請輸入底: ");
  10.                 int a=s.nextInt();
  11.                 System.out.print("請輸入高: ");
  12.                 int b=s.nextInt();
  13.                 System.out.println("底"+a+",高"+b+"的三角形,面積為"+tri(a,b));

  14.         }

  15. }
複製代碼

TOP

返回列表