例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 static void 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.明天继续学习