存取敘述 static 傳回值型態 方法名稱(參數列){ ... 程式敘述; ... return 運算式; } |
程式 | 輸出 |
public class example{ public static void void_example(){ System.out.println("Hello, World!"); } public static void main(String[] args){ void_example(); } } |
Hello, World! |
程式 | 輸出 |
class class_example{ public static void void_example(){ System.out.println("Hello, World!"); } } public class example{ public static void main(String[] args){ class_example.void_example(); } } |
Hello, World! |
程式 | 輸出 |
public class example{ public static void void_example(){ System.out.println("Hello, World!"); } public static void main(String[] args){ void_example(); } } |
Hello, World! |
程式 | 輸出 |
public class example{ public static void power(int x, double y){ double z = Math.pow((double)x, y); System.out.println(z); } public static void main(String[] args){ int a = 5; double b = 3; power(a, b); } } |
125 |
程式 | 輸出 |
public class example{ public static int summary(int x, int y){ return x + y; } public static void main(String[] args){ int a = 3; int b = 6; int c = summary(a, b); System.out.println(c); } } |
9 |
程式 | 輸出 |
class example1{ public static void first_code(){ System.out.println("Hello, World!"); } public static int summary(int x, int y){ return x + y; } } public class example{ public static float average(int x, int y, int z){ float a; a = ((float)x + (float)y + (float)z)/3; return a; } public static void main(String[] args){ int a = 6; int b = 13; int c = 10; int d = 8; example1.first_code(); float number1 = average(a, b, c); System.out.println(number1); int number2 = example1.summary((int)number1, d); System.out.println(number2); } } |
Hello, World! 9.666667 17 |