Discuss / Java / for循环

for循环

Topic source

I WILL.

#1 Created at ... [Delete] [Delete and Lock User]

练习一:

int[] ns = { 1, 4, 9, 16, 25 };for (int i=ns.length-1; i>=0; i--) {    System.out.println(ns[i]);}

练习二:

int[] ns = { 1, 4, 9, 16, 25 };
int sum = 0;
for (int i : ns) {
    sum+=i;
    // TODO}
System.out.println(sum); // 55
练习三:
double pi = 1;
for (double i =3; i<=500000000; i+=4) {
    pi = pi- 1/i+1/(i+2);
    // TODO}
System.out.println(pi*4);

I WILL.

#2 Created at ... [Delete] [Delete and Lock User]

练习3不能使用

pi -= 1/i+1/(i+2)

因为-= 运算符的优先级较低,而 +- 运算符的优先级较高


  • 1

Reply