標題:
407-尾端遞迴計算總合
[打印本頁]
作者:
許承鴻
時間:
2018-8-21 18:18
標題:
407-尾端遞迴計算總合
import java.util.Scanner;
public class JPD04 {
static Scanner keyboard = new Scanner(System.in);
public static void main(String args[]) {
String s;
System.out.print("Input a string of numbers: ");
s = keyboard.nextLine();
System.out.printf("尾端遞迴:%d\n", sumTail(s, 0));
System.out.printf("迴圈:%d\n", sumLoop(s, 0));
System.out.print("Input a string of numbers: ");
s = keyboard.nextLine();
System.out.printf("尾端遞迴:%d\n", sumTail(s, 0));
System.out.printf("迴圈:%d\n", sumLoop(s, 0));
}
...
}
複製代碼
作者:
巫沛庭
時間:
2018-12-29 00:35
import java.util.Scanner;
public class JPA04 {
static Scanner keyboard = new Scanner(System.in);
public static void main(String args[]) {
String s;
System.out.print("Input a string of numbers: ");
s = keyboard.nextLine();
System.out.printf("尾端遞迴:%d\n", sumTail(s, 0));
System.out.print("Input a string of numbers: ");
s = keyboard.nextLine();
System.out.printf("尾端遞迴:%d\n", sumTail(s, 0));
}
static int sumTail(String s,int r)
{
if(s.equals(""))
return r;
else
return sumTail(s.substring(1),r+Integer.parseInt(s.substring(0,1)));
}
}
複製代碼
歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://istak.org.tw/seed/)
Powered by Discuz! 7.2