如何用java寫出下列程式
A
BCD
EFGHI
JKLMNOP
QRSTUVWXY
2007-11-24 15:52:35 · 4 個解答 · 發問者 Anonymous in 電腦與網際網路 ➔ 程式設計
用迴圈結構寫
2007-11-25 06:20:00 · update #1
public class Test1 {
public Test1() {
}
public static void main(String[] args) {
Test1 test1 = new Test1();
String letters ="ABCDEFGHIJKLMNOPQRSTUVWXY";
char[] letter = letters.toCharArray(); //將字串變成字元陣列
int count=0; //用來記錄每層的起始位置
for (int i = 1; i <= 5; i++) { //有五層
for (int j = 0; j < 2*i-1; j++) {
System.out.print(letter[count]); //印出每一層的字母
count++;
}
System.out.print("\n"); //換行
}
}
}
2007-11-25 14:43:14 · answer #1 · answered by ? 2 · 0⤊ 0⤋
import java.io.*;
//import java.util.*;
public class TEST
//檔名:TEST.java
{
public static void main(String[] args)
{
PrintStream o=new PrintStream(System.out);
//Scanner in=new Scanner(System.in);
char ch='A';
for(int i=0;i<(int)Math.sqrt(26);i++){
for(int j=0;j<(2*i+1);j++){
o.printf("%c",ch);
ch++;
}
o.printf("\n");
}
}
}
2007-11-25 14:23:49 · answer #2 · answered by Big_John-tw 7 · 0⤊ 0⤋
可以敎我用迴圈結構寫嗎謝謝
2007-11-25 06:21:11 · answer #3 · answered by 呈 1 · 0⤊ 0⤋
你好唷!
你可以參考以下的執行看看
public class ABC {
public static void main(String[] args)
{
System.out.print("A\n BCD\n EFGHI\n JKLMNOP\n QRSTUVWXY\n");
}
}
2007-11-24 21:51:11 · answer #4 · answered by ? 3 · 0⤊ 0⤋