java 小问题~谢谢大侠

来源:百度知道 编辑:UC知道 时间:2024/06/15 15:33:33
用FOR 编写JAVA程序使得出现下列结果:

*******
*****
***
*
(倒立的正三角形,注意图形。在这里空格打不上)

*
***
*****
*******
(正立的三角形,注意图形 前边空格打不上。一定要注意图形 谢谢

(1) 倒立三角形
int sum = 7,space=0;
for(int j=sum;j>0;j-=2){
for(int n=0;n<space;n++)
System.out.print(" ");
for (int i = 0; i < j; i++) {
System.out.print("*");
}
System.out.print("\n");
space++;
}

(2)正立三角形
int sum = 7,space=sum/2;
for(int j=1;j<=sum;j+=2){
for(int n=space;n>0;n--)
System.out.print(" ");
for (int i = 0; i < j; i++) {
System.out.print("*");
}
System.out.print("\n");
space--;
}

package test;
import java.util.Scanner;
public class Test
{
public static void main(String args[]) {
Scanner scn=new Scanner(System.in);
System.out.println("你想输出几层?");
int n=scn.nextInt();
Test t=new Test();
t.f(n);
}
private void f(int n){
for