Java实现输入任意数字平行4行
下面展示一些 内联代码片
。
通过Scanner 进行程序交互,for循环进行循环输出
// An highlighted block
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
System.out.println("请输入一个数字");
int n=sc.nextInt();
for (int i =1; i <= n; i++) {
for (int j = 1;j <= n-i; j++) {
System.out.print(" ");
}
for (int j = 1;j <=i; j++) {
System.out.print(j);
}
for (int j = i-1;j >=1; j--) {
System.out.print(j);
}
System.out.println();//控制换行
}
for (int i =n-1; i>0; i--) {
for (int j = 1;j <= n-i; j++) {
System.out.print(" ");
}
for (int j = 1;j <=i; j++) {
System.out.print(j);
}
for (int j = i-1;j >=1; j--) {
System.out.print(j);
}
System.out.println();//控制换行
}
}