java 程式問題 急!!!!!!!!!!!!!!!

1. 印出

1

2 3 4

5 6 7 8 9

........................

............................

............................

印到第十列

2.印出2001年~2100年 的所有leap year ( ten per line)

2 Answers

Rating
  • ?
    Lv 7
    1 decade ago
    Favorite Answer

    請參考我的做法

    public class Y0945 {

    public static void main(String[] args) {

    // Q1

    int counter = 0;

    int flag = 1;

    while (counter++ < 100) {

    System.out.print(counter + " ");

    if (flag * flag == counter) {

    flag++;

    System.out.println();

    }

    }

    // Q2

    counter = 0;

    for (int i = 2001; i <= 2100; i++) {

    if (i % 4 == 0) {

    System.out.print(i + " ");

    counter++;

    }

    if (counter == 10) {

    counter = 0;

    System.out.println();

    }

    }

    }

    }

    2009-10-16 14:41:58 補充:

    1

    234

    56789

    ...................

    ............................

    左右要留空白

    Q1 請改成

    2009-10-16 14:42:09 補充:

    int counter = 0;

    int flag = 1;

    final int total = 100;

    int col_count = 0;

    int cols = (int)(Math.sqrt(total));

    while (counter++ < total) {

    if (Math.abs(cols - flag - col_count) > 0) {

    System.out.print(" ");

    col_count++;

    counter--;

    continue;

    }

    2009-10-16 14:42:13 補充:

    System.out.print(counter + " ");

    if (flag * flag == counter) {

    flag++;

    col_count = 0;

    System.out.println();

    }

    }

  • 1 decade ago

    public class Test {

     //問題1

     private static void printNum(int size) {

      for (int i = 1; i <= size; i ++) {

       for (int j = (i - 1) * (i - 1) + 1, k = 0; k < 2 * i - 1; j++, k++) {

        System.out.print(j + " ");

       }

       System.out.println();

      }

     }

     //問題2

     private static void printLeapYear(int y_s, int y_e) {

      for (int i = y_s, cnt = 1; i <= y_e; i++) {

       if (isLeapYear(i)) {

        System.out.print(i + (cnt++%10==0?"\n":" "));

       }

      }

     }

     private static boolean isLeapYear(int yyyy) {

      return ((yyyy % 400 == 0) || ((yyyy % 4 == 0) && (yyyy % 100 != 0)));

     }

     public static void main(String[] arg) {

      printNum(10);

    System.out.println();

    printLeapYear(2001, 2100);

     }

    }

Still have questions? Get your answers by asking now.