1.1.1 编写Java程序,求13-23+33-43+…+973-983+993-1003的值
//编写Java程序,求13-23+33-43+…+973-983+993-1003的值
class Test05{
public static void main(String[] args) {
int a = 0;
int b = 0;
for (int i = 13; i < 993; i += 20) {
a += i;
}
for (int i = -23; i < -1003; i -= 20) {
b += i;
}
System.out.println(a+b);
}
}
1.1.2 编程求1!+2!+3!+…+20!
//编程求1!+2!+3!+…+20!
class Test06{
public static void main(String[] args) {
int sum = 0;
for (int i = 1; i <= 20; i++) {
int num = 1;
for (int j = 1; j <= i; j++) {
num = num * j;
}
sum += num;
}
System.out.println(sum);
}
}
1.1.3 使用for循环打印三角形
//使用for循环打印三角形
class Test07{
public static void main(String[] args) {
for (int i = 1; i <= 5; i++) {
for (int j = 5; j > i; j--) {
System.out.print(" ");
}
for (int k = 1; k <= i; k++) {
System.out.print("* ");
}
System.out.println();
}
}
}