用java编写一个程序利用for写从键盘输入n求s=1+1+2+1+2+3++1+2+3++n以梯形打印出来
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入n:");
int n = scanner.nextInt();
int s = 0;
for (int i = 1; i <= n; i++) {
int sum = 0;
for (int j = 1; j <= i; j++) {
sum += j;
}
s += sum;
}
System.out.println("s=" + s);
// 打印梯形
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(j);
}
System.out.println();
}
}
}
输入示例:
请输入n:5
输出示例:
s=35
1
12
123
1234
12345
``
原文地址: https://www.cveoy.top/t/topic/cJSq 著作权归作者所有。请勿转载和采集!