Discuss / Java / 练习题

练习题

Topic source

唯情恋昉

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

练习作答

public class Main {
    public static void main(String[] args) {
        int[] ns = { 1, 4, 9, 16, 25 };
        for (int i = ns.length - 1; i >= 0; i --) {
            System.out.println(ns[i]); // 25 16 9 4 1
        }
    }
}
public class Main {
    public static void main(String[] args) {
        int[] ns = { 1, 4, 9, 16, 25 };
        int sum = 0;
        for (int n : ns) {
            sum += n;
        }
        System.out.println(sum); // 55
    }
}
public class Main {
    public static void main(String[] args) {
        double pi = 0;        
        for (int i = 1; ; i += 4) {
            pi += (1.0 / i);            
            if (i < 0) {
                break;            
            }
        }
        for (int i = 3; ; i += 4) {
            pi -= (1.0 / i);            
            if (i < 0) {
                break;            
            }
        }
        pi *= 4;        
        System.out.print(pi); // 3.141592652649746
    }
}

  • 1

Reply