SOGO論壇

標題: 求救 使用JAVE 寫出FOR迴圈 [列印本頁]

作者: lonrsfly    時間: 2013-4-19 19:59:36     標題: 求救 使用JAVE 寫出FOR迴圈

我想用JAVE  使用FOR迴圈
產生下圖程式

1
12
123
1234
12345
123456
1234567

請求教學


作者: 包包頭    時間: 2013-4-19 21:33:30

本帖最後由 包包頭 於 2013-4-19 21:48 編輯

我怎麼感覺那個好像是JAVA  (有點不懂你意思)  
(由於我很懶惰 直接轉了成品 大哥您琢磨琢磨吧



public class Test {

public static void main(String[] args) {

  int max = 7; //最大長度
  
  for(int i = 1; i <= max; i++){
   
   StringBuffer sb = new StringBuffer();
   for(int j = 1; j <= i; j++){
    sb.append(j);
   }
   
   System.out.print(sb.toString() + "  ");
  }

}
}


-------------------------------------我是很可愛的分隔線
(看看下面這段是否符合?! )

public class Formatter {
        public static void main(String[] args) {
                String[] tests = new String[] {
                        "1",
                        "12",
                        "123",
                        "1234",
                        "12345",
                        "123456",
                        "1234567",
                        "12345678",
                        "123456789",
                        "1.1",
                        "12.12",
                        "123.123",
                        "1234.1234",
                        "12345.12345",
                        "123456.123456",
                        "1234567.1234567",
                        "12345678.12345678",
                        "123456789.123456789"
                };
                for (String test : tests) {
                        System.out.printf("%s\t[%s]%n", test, format(test));
                }
        }
        public static String format(String numStr) {
                if (numStr == null) {
                        return "";
                }
                char[] chars = numStr.toCharArray();
                int decimal = 0;
                for (; decimal < chars.length; decimal++) {
                        if (chars[decimal] == '.') {
                                break;
                        }
                }
                char[] formatted = new char[chars.length + Math.max((decimal - 1) / 3, 0)];
                for (int i = 0, pos = 0; i < chars.length; i++) {
                        if (i != 0 && i % 3 == decimal % 3 && i < decimal) {
                                formatted[pos++] = ',';
                        }
                        formatted[pos++] = chars;
                }
                return new String(formatted);
        }
}




歡迎光臨 SOGO論壇 (https://oursogo.com/) Powered by OURSOGO.COM