Download presentation
Presentation is loading. Please wait.
1
Java class 2010.10.29
2
Outline Defining a method Calling Method Passing parameters [Sample code] TestMethod.java 、 TestMethod2.java 、 GCD.java 、 prime.java
3
程式 : A method that return a max value of two values. -> TestMethod.java
4
Defining a method public static int max( int num1, int num2 ) { int result; if (num1 > num2) result = num1; else result = num2; return result;// return value } Modifier 回傳值型態 Method name 參數 2 Method header Method body Method signature ( 方法署名 ) Parameter list ( 參數列 ) 參數 1
5
Calling Method EX: int num3 = max( 10, 20 ); System.out.println( max(10, 20) );
6
main() {. num3 = max( num1,num2 );. } main() {. num3 = max( num1,num2 );. } max( int a, int b ) {. return result;. } max( int a, int b ) {. return result;. } num1 值傳給 a num2 值傳給 b result 值傳給 max(num1,num2)
7
Passing parameters Method with a return value Method without a return value
8
程式 : Two types about passing parameters. -> TestMethod2.java
9
程式 : Find the Greatest Common Divisor number. -> GCD.java
10
程式 : Find the Prime numbers between 1 to num. -> prime.java
11
程式練習 : 1. 輸入一個十進位的整數 2. 寫一個 ToDecimal method, 用來將 十進位數字轉成二進制 Ex: 10 10 = 1010 2
12
回家看 : 課本 p.180 ~ p.182
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.