日期:2014-05-20 浏览次数:20763 次
public static void main(String[] args) throws Exception { int[] arg = { 0, 0 }; for (int i = 0; i < 2; i++) { System.out.print("输入第" + (i + 1) + "个数:"); DataInputStream di = new DataInputStream(System.in); String num = di.readLine(); arg[i] = Integer.parseInt(num); } System.out.println(firstMethod(arg[0], arg[1])); System.out.println(secondMethod(arg[0], arg[1])); } static int firstMethod(int start, int end) { int sum = 0; for (; start <= end; start++) { sum += start; } return sum; } static int oneToAnum(int num) { int k = ((1 + num) * num) / 2; return k; } static int secondMethod(int first, int second) { return oneToAnum(second) - oneToAnum(first - 1); }