1.例4.1在其他函数中调用主函数
package bgio; public class cjava { public static void main(String[] args) { printstar(); print_message(); printstar(); } static void printstar() { System.out.println("*******************"); } static void print_message() { System.out.println("Weclome to java!"); } }
例4.2调用函数时的数据传递
import java.util.Scanner; public class cjava { public static void main(String[] args) { int a,b,c; System.out.println("please enter two integer number:"); Scanner s=new Scanner(System.in); a=s.nextInt(); b=s.nextInt(); c=max(a,b); System.out.println("max="+c); } static int max(int x,int y) { int z; z=x>y?x:y; return z; } }
例4.3对被调用的函数声明
import java.util.Scanner; public class cjava { public static void main(String[] args) { float a,b,c; System.out.println("please enter a,b:"); Scanner s=new Scanner(System.in); a=s.nextInt(); b=s.nextInt(); c=add(a,b); System.out.println("和为:"+c); } static float add(float x,float y) { float z; z=x+y; return z; } }
2.没问题
3.明天继续写例题。