Discuss / Java / 打卡打卡

打卡打卡

Topic source

xiao伟iii

#1 Created at ... [Delete] [Delete and Lock User]
package com.itranswarp.learnjava;

/**
 * for练习
 */
public class Main {

	public static void main(String[] args) {
		System.out.println("------------first---------------");
		int[] ns = { 1, 4, 9, 16, 25 };
		for (int i=ns.length-1; i>=0; i--) {
			System.out.println(ns[i]);
		}
		
		System.out.println("------------second------------");
		
		int sum = 0;
		for (int i=0; i<ns.length; i++) {
			sum += ns[i];
		}
		System.out.println(sum);
		
		System.out.println("------------third------------");
		
		double pi = 0;
//		for (int i=1; i > 0; i += 4) {
//			pi += (double)1/i;
//		}
//		for (int i=3; i > 0; i += 4) {
//			pi -= (double)1/i;
//		}
		for (int i=1; i > 0; i += 2) {
			if (i == 1) {
				pi += (double)1/i;
			} else if (i == 3) {
				pi -= (double)1/i;
			} else if (i%4 == 1) {
				pi += (double)1/i;
			} else if (i%4 == 3) {
				pi -= (double)1/i;
			}
		}
		pi = pi*4;
		System.out.println(pi);
	}

}


  • 1

Reply