用java编写程序,从键盘输入一个n,输出一个n行的等腰三角形,不用数组

不用数组,用循环语句,最好是for循环。java语句
2024-11-15 10:33:54
推荐回答(4个)
回答1:

这个是作业吧哈哈!!
用双循环做的!
public static void main( String[] arg ){
int n = 4;
for(int i = 0; i < n; i++){
int m = i + 1;
for(int j=n-i-1;j>0;j--){
System.out.print(" ");
}
while(m > 0){
System.out.print("* ");
m--;
}
System.out.print("\n");
}
}

回答2:

import java.util.Scanner;


public class Test050 {

    /**
     * @param args
     */
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        int n = s.nextInt();
        for(int i = 0; i < n; i++){
            int count = i + 1;
            for(int j = 0; j < n - i - 1; j++){
                System.out.print(" ");
            }
            for(; count > 0; count--){
                System.out.print("* ");
            }
            System.out.println();
        }
    }

}

回答3:

public class Test {

/**
* @param args
*/
public static void main(String[] args) {
printS(5);
}

public static void printS(int n) {
int a = n + n - 1;
for (int i=1; i<=n; i++) {
int m = i + i -1;
for (int j=1; j<=(a-m)/2; j++) {
System.out.print(" ");
}
for (int k=1; k<=m; k++) {
System.out.print("*");
}
System.out.println();
}
}

}

回答4:

import java.util.Scanner;

public class Play {
public static void main(String[] args){
System.out.println("请输入数字:");
Scanner s = new Scanner(System.in);
int line = s.nextInt();
for(int i=0;i int star=i;
int empty = line-1-i;
while(empty>0){
System.out.print(" ");
empty--;
}
while(star>=0){
System.out.print("* ");
star--;
}
System.out.println("");
}
}
}