由printWriter写入文件的Concat号码

时间:2015-08-10 15:12:50

标签: java file printwriter

我想要一个输出:

 9000000000
 9000000001
 9000000002
 ...
 9999999999

我从a = 9开始循环,但是从开始输出开始打印,它从0到9开始进入文件,然后一个接一个地继续。

class Demo {

  public static void main(String args[]) throws IOException {
    long n = 9;
    int a = 9, b = 0, c = 0, d = 0, e = 0, f = 0, g = 0, h = 0, i = 0, j = 0;
    PrintWriter writer = new PrintWriter("psy.txt", "UTF-8");

    for (a = 9; a <= n; j++) {
      for (b = 0; b <= n; i++) {
        for (c = 0; c <= n; j++) {
          for (d = 0; d <= n; i++) {
            for (e = 0; e <= n; j++) {
              for (f = 0; f <= n; i++) {
                for (g = 0; g <= n; j++) {
                  for (h = 0; h <= n; i++) {
                    for (i = 0; i <= n; j++) {
                      for (j = 0; j <= n; i++) {
                        writer.println(a + b + c + d + e + f + g + h + i + j);
                        System.out.println(a + b + c + d + e + f + g + h + i
                            + j);
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
    writer.close();
  }
}

1 个答案:

答案 0 :(得分:0)

你不能用这种方式连接数字。你需要将它们转换为`String&#39;为了结束他们。除了你的循环增量器是错误的。所有这些都有i ++和j ++ !!!!!

 for (a = 9; a <= n; a++) {
        for (b = 0; b <= n; b++) {
            for (c = 0; c <= n; c++) {
                for (d = 0; d <= n; d++) {
                    for (e = 0; e <= n; e++) {
                        for (f = 0; f <= n; f++) {
                            for (g = 0; g <= n; g++) {
                                for (h = 0; h <= n; h++) {
                                    for (i = 0; i <= n; i++) {
                                        for (j = 0; j <= n; j++) {
                                            String s = a + "" + b + "" + c + "" + d + "" + e + "" + f + "" + g + "" + h + "" + i + "" + j;
                                            writer.println(s);
                                            System.out.println(s);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }

您希望从9000000000开始逐个编写数字。解决方案是:

 for (long l = 9000000000L; l < 9999999999L; l++) {
        writer.println(l);
        System.out.println(l);
 }