2020.8.14第三十九天
阿新 • • 發佈:2020-08-14
例4.1在主函式中呼叫其他函式
1 public class cjava { 2 public static void main(String[] args) { 3 printstar(); 4 print_message(); 5 printstar(); 6 } 7 static void printstar() { 8 System.out.println("*************************************"); 9 } 10 staticvoid print_message() { 11 System.out.println("Weclome to java!!!"); 12 } 13 }
例4.2呼叫函式時的資料傳遞
1 import java.util.Scanner; 2 public class cjava { 3 public static void main(String[] args) { 4 int a,b,c; 5 System.out.println("please enter two integer number:");6 Scanner s=new Scanner(System.in); 7 a=s.nextInt(); 8 b=s.nextInt(); 9 c=max(a,b); 10 System.out.println("max="+c); 11 } 12 static int max(int x,int y) { 13 int z; 14 z=x>y?x:y; 15 return z; 16 } 17 } 18
例4.3對被呼叫的函式宣告
1 import java.util.Scanner; 2 public class cjava { 3 public static void main(String[] args) { 4 float a,b,c; 5 System.out.println("please enter a,b:"); 6 Scanner s=new Scanner(System.in); 7 a=s.nextFloat(); 8 b=s.nextFloat(); 9 c=add(a,b); 10 System.out.println("max="+c); 11 } 12 static float add(float x,float y) { 13 float z; 14 z=x+y; 15 return z; 16 } 17 }
2.遇到的問題:沒有問題
3.明天繼續學習