其实算法和C语言一样——
package test;
import java.util.Scanner;
public class Fruit {
public static void main(String[] args) {
Scanner s=new Scanner(System.in);
int n,i;
n=s.nextInt();
for ( i = 1; i <= n; i++) {
for (int k = 0; k <n-i ; k++) {
System.out.print(" ");
}
for (int j = 1; j <= 2*i-1; j++) {
System.out.print("*");
}
System.out.println();
}
for ( i = 1; i <= n-1; i++)
{
for (int j = 0; j <i; j++) {
System.out.print(" ");
}
if(i==n-1)
{
System.out.print("*");
}
else
{
for (int k = 0; k <2*(n-i)-1; k++) {
System.out.print("*");
}
}
System.out.println();
}
}
}
|