Java编辑程序在控制台用*输出一个倒立的三角形并保存在triangle文件中
1个回答
展开全部
代码:
public class TriangleTest {
public static void main(String[] args) throws IOException{
TriangleTest.print(7);
}
public static void print(int layer) throws IOException {
File file = new File("C:\\Triangle.txt");
if(!file.exists()){
file.createNewFile();
}
Writer writer = new FileWriter(file);
for (int i = layer; i >= 1; i--) {
for (int k = 1; k <= layer - i; k++) {
System.out.print(" ");
writer.write(" ");
}
for (int j = 1; j <= 2 * i - 1; j++) {
System.out.print("*");
writer.write("*");
}
System.out.println();
//换行
writer.write(System.getProperty("line.separator"));
}
writer.close();
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询