阅读背景:

求 1+2+3+..n不能使用乘除法、 for 、 while 、 if 、 else 、 switch 、 case 等关键字以及条件判断语句

来源:互联网 
<pre name="code" class="java">public class NoIfWhile {
	/**
	 * @param args
	 *            求 1+2+3+..n不能使用乘除法、 for 、 while 、 if 、 else 、 switch 、 case
	 *            等关键字以及条件判断语句 find x=1+2+3+....n
	 */
	public static void main(String[] args) {
		int n = 10;
		int re = find(n);
		System.out.print(re);
		System.out.println();
		System.out.println(sum(n));
	}

	public static int find(int n) {
		int re = 0;
		boolean whatever = false;
		int a = -1;
		whatever = (n != 0) && (a == (re = find(n - 1)));
		return re + n;
	}

	public static int sum(int n) {
		return n > 0 ? n + sum(n - 1) : 0;

	}
}
<pre name="code" class="java">public class NoIf



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: