package com.org.lxh;import java.util.Scanner;/*** Java流程控制语句,if,if……else,switch,while,do……while,for等等* @author hemmingway <hemmingway@163.com>***/
public class CommCtrl {/*** @param args*/public static void main(String[] args) {// TODO Auto-generated method stub//1.输入一个年份,判断是不是闰年Scanner scan = new Scanner(System.in);System.out.println("输入一个年份:");long year;try{year = scan.nextLong(); //当输入非数字的时候判断异常if(year%4 == 0 && year%100 != 0 || year%400 == 0)System.out.println(year + "是闰年");elseSystem.out.println(year + "不是闰年");}catch(Exception e){e.printStackTrace();}//2.黄蓉难倒瑛姑的数学题System.out.println("今有物不知其数,三三数之剩二,五五数之剩三,七七数之剩二,问几何?");for(int j = 1; j < 1000; j++){if(j%3 == 2 && j%5 == 3 && j%7 == 2){System.out.print(j + " ");}}System.out.println("");//3.李白提壶买酒的问题。//李白街上走,提壶去买酒,遇店加一倍,见花喝一斗,五遇店和花,喝光壶中酒。问壶中原先有多少酒?System.out.println("李白街上走,提壶去买酒,遇店加一倍,见花喝一斗,五遇店和花,喝光壶中酒。问壶中原先有多少酒?");double x = 0.0f; //壶中酒,单位为斗for(int i = 5; i>0; i--){x = x + 1;x = x/2;}System.out.println("壶中原先有酒" + x + "斗");//另外一种方法分析//第一次遇见店和花,2x-1//第二次 2(2x-1)-1=4x-3//第三次,2(4x-3)-1=8x-7//第四次,2(8x-7)-1=16x-15//第五次,2(16x-15)-1=32x-31=0x = 31/32.0;System.out.println("壶中原先有酒31/32斗,即"+ x + "斗"); //4.输出杨辉三角//数字表,两则的数字都是1,其他位置的数字是正上方和左上方的数字之和,下面的代码演示了用for循环打印出包含10行的杨辉三角System.out.println("打印包含10行的杨辉三角");int triangle[][] = new int[10][]; //可以修改行数for(int i = 0; i<triangle.length; i++){triangle[i] = new int[i+1];for(int j=0; j<i; j++){if(i ==0 || j==0 ||j==1){triangle[i][j]=1;}else{triangle[i][j] = triangle[i-1][j] + triangle[i-1][j-1];}System.out.print(triangle[i][j] + "\t");}//for(int j=0; j<=i; j++)System.out.println(); //换行}//for(int i = 0; i<triangle.length; i++)//5.while循环计算一个数列System.out.println("使用循环计算:1-1/2+1/3-1/4+1/5-1/6+1/7-1/8+……");int i =1;double num = 0.0;while(i<=10000){ //演示一个bug while(i<=100);{num = num + Math.pow(-1.0, i+1)*1.0/i;i++;}System.out.println("数列的结果趋向于" + num + ",即ln(2)=" + Math.log(2.0)); //Java中Math类的log函数默认是以e为底的,相当于数学的ln(2)函数//6.计算harmonic sum,即1+1/2+1/3+1/4+……System.out.println("计算harmonic sum,即1+1/2+1/3+1/4+……");int n = 1;double hn = 0.0;while(n<=1000){hn = hn + 1.0/n;n++;}System.out.println("harmonic sum 趋向于ln(N) :" + hn + ", ln(100)=" + Math.log(1000.0));}//end for main}//end for class
输入一个年份:
2012
2012是闰年
今有物不知其数,三三数之剩二,五五数之剩三,七七数之剩二,问几何?
23 128 233 338 443 548 653 758863 968
李白街上走,提壶去买酒,遇店加一倍,见花喝一斗,五遇店和花,喝光壶中酒。问壶中原先有多少酒?
壶中原先有酒0.96875斗
壶中原先有酒31/32斗,即0.96875斗
打印包含10行的杨辉三角
1
1 1
1 1 1
1 1 2 1
1 1 3 3 1
1 1 4 6 4 1
1 1 5 10 10 5 1
1 1 6 15 20 15 6 1
1 1 7 21 35 35 21 7 1
使用循环计算:1-1/2+1/3-1/4+1/5-1/6+1/7-1/8+……
数列的结果趋向于0.6930971830599583,即ln(2)=0.6931471805599453
计算harmonic sum,即1+1/2+1/3+1/4+……
harmonic sum 趋向于ln(N) :7.485470860550343,ln(100)=6.907755278982137