关键词:入门求和公式
题目:
解决方案:
方法一:(小丽丽)
1 import java.util.Scanner; 2 3 public class Main { 4 5 public static void main(String[] args) { 6 Scanner in = new Scanner(System.in); 7 long n = in.nextLong(); 8 long sum = 0; 9 /* 10 for (long i = 1; i <= n; i++) { 11 sum += i; 12 } 13 */ 14 sum = (1 + n) * n / 2; 15 System.out.println(sum); 16 } 17 18 }
方法二(朕):
1 import java.util.Scanner; 2 3 public class Main { 4 public static void main(String[] args){ 5 6 // System.out.println("题目:求1+2+3+...+n的值"); 7 Scanner read =new Scanner(System.in); 8 // System.out.println(" 请输入您要计算的n的值:"); 9 long n=read.nextLong(); 10 long answer=0; 11 answer=(n*(n+1))/2; 12 System.out.println(answer); 13 } 14 15 }