永发信息网

求教Java如何打印菱形字母!!!

答案:2  悬赏:50  手机版
解决时间 2021-11-30 05:29
  • 提问者网友:雪舞兮
  • 2021-11-30 01:18
求教Java如何打印菱形字母!!!
最佳答案
  • 五星知识达人网友:北城痞子
  • 2021-11-30 01:53
public class Test {
public static void main(String[] args) {
String str = "ABCDEFGHIJKLMNOPQRST";
int index = 0;
for (int i = 0; i < 8; i++) {
int sum = (int)Math.abs(i-3.5);
printTab(sum);
for (int j=0;j<4-sum;j++) {
System.out.print(str.charAt(index)+" ");
index += 1;
}
System.out.println();
}
}

private static void printTab(int sum) {
for (int i = 0; i < sum; i++) {
System.out.print(" ");
}
}
}
全部回答
  • 1楼网友:野味小生
  • 2021-11-30 02:31
public class RhombusTest {
    static String[] component = new String[]{
            "A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"
    };

    public static void printShape(int length){
        int index = 0;
        int componentLength = component.length;
        for(int i = 0; i < length; i++){
            for(int j = 0; j < (length - i-1); j++){
                System.out.print(" ");
            }
            for(int k = 0; k <= i; k++){
                printLetter(component[index]);
                if(++index >= componentLength){
                    index = index - componentLength;
                }
            }
            System.out.println();
        }
        for(int i = 0; i < length; i++){
            for(int j = 0; j < i; j++){
                System.out.print(" ");
            }
            for(int k = i; k < length; k++){
                printLetter(component[index]);
                if(++index >= componentLength){
                    index = index - componentLength;
                }
            }
            System.out.println();
        }
    }

    public static void printLetter(String letter){
        System.out.print(letter + " ");
    }

    public static void main(String[] args){
        printShape(5);
    }
}
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯