【正文】
( 5) Java應(yīng)用程序中 , 程序的執(zhí)行從 main方法開始 , 調(diào)用其他方法后又回到 main方法 , 在 main方法中結(jié)束整個程序的運(yùn)行 。 方法體通常起到兩種作用 , 一是圍繞類的屬性進(jìn)行各種操作;二是與其他的類與對象進(jìn)行數(shù)據(jù)交流 、 消息傳遞等操作 。 14 } 15 } 16 17 public class app7_3 18 { 19 public static void main(String args[]) 20 { 21 CRectangle rect1。 ? int k = max(i, j)。 i++) (message)。 ? } ? 例 // The method for swapping two variables ? static void swap(int n1, int n2) ? { ? ( Inside the swap method)。 else return num2。 ? } ? ? ?// Find the max among three double values ? static double max(double num1, double num2, double num3) ? { ? return max(max(num1, num2), num3)。 i++) ? { ? // Generate a new random number ? number = (int)(()*1000)。 ? (The standard deviation is + deviation)。 ? ? // Print headings ? printMonthTitle(year, month)。 i++) ? if (isLeapYear(i)) ? total = total + 366。 ? ? if (month == 2) ? if (isLeapYear(year)) ? return 29。 ? for (i = 0。 ? } ? ()。 break。 break。 break。 int n = ()。 ? // Find and display the Fibonacci number ? (Fibonacci number at index + n + is +fib(n))。 當(dāng)調(diào)用遞歸函數(shù)的層數(shù)很大時,就必須要有較大的堆??臻g,容易會有內(nèi)存不足的情形,這也是使用遞歸函數(shù)要注意的地方。 例 Fibonacci數(shù)列 ? public class ComputeFibonacci ? { ? // Main method ? public static void main(String args[]) ? { ? // Read the index ? (Enter an index for the Fibonacci number)。 例如 階乘函數(shù) (factorial function),便可利用遞歸的方式來完成: 遞歸可讓程序代碼簡潔,也可提高運(yùn)行的效率,但使用時須注意到遞歸函數(shù)一定要有可以結(jié)束運(yùn)行的終止條件,使函數(shù)得以返回上層調(diào)用的地方,否則容易造成無窮循環(huán),最后因內(nèi)存空間不足而當(dāng)?shù)簟?break。 break。 break。 ? else ? ( + i)。 ? ? return false。 ? } ? 例 ? // Get the number of days in a month ? static int getNumOfDaysInMonth(int year, int month) ? { ? if (month == 1 || month==3 || month == 5 || month == 7 || ? month == 8 || month == 10 || month == 12) ? return 31。 ? ? // Get the total days from 1800 to year 1 ? for (int i = 1800。 ? } 例 ? // Print the calendar for a month in a year ? static void printMonth(int year, int month) ? { ? // Get start day of the week for the first date in the month ? int startDay = getStartDay(year, month)。 ? // Find standard deviation ? double deviation = ((squareSum mean)/(10 1))。 // Store the sum of the squares ? 計算 ? 輸出 ? } ? } 例 ? 計算 ? for (int i=0。 ? } ? ? // Find the max between two double