实验二

1.编写Java Application程序,输出1900年到2000年之间的所有润年。(闰年的判断条件:能被4整除且不能被100整除,或能被400整除);

public class RunNian {
    public static void main(String[] args){
        for(int i = 1900; i<= 2000; i++){
            if((i % 4 == 0 && i % 100 !=0)||i % 400 ==0){
                System.out.println("i = " + i);
            }
        }
    }
}

 

2. 金字塔:Pyramid.java

在屏幕上显示一个由星型符号“*”组成的金字塔图案,示例如下:

         * 

        ***

       *****

   *******

要求:金字塔高度h,可以由用户设置。

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Ta {
    
    public static void main(String[] args) throws IOException{
        BufferedReader buf=new BufferedReader(new InputStreamReader(System.in));
        System.out.println("输入想要的高度:");
        String str=buf.readLine();
        int maxLength=Integer.parseInt(str);
        
        for(int m = 1; m <= maxLength; m++){
            printBlanks(maxLength - m);
            printStar(2 * m - 1);
            System.out.println();
        }
    }

    public static void printStar(int n){
        for(int i = 0; i < n; i++){
            System.out.print("*");
        }
    }
    
    public static void printBlanks(int  n){
        for(int i = 0; i < n; i++){
            System.out.print(" ");
        }
    }
}

 

posted @ 2014-10-03 19:00  肉球  阅读(244)  评论(0编辑  收藏  举报