![](/img/trans.png)
[英]Java: Having trouble printing out this pyramid-like reversed number pattern
[英]Printing pyramid pattern out of characters
我需要打印金字塔形狀的字符數組。 到目前為止我所擁有的是:
char[] chars = {'F', 'E', 'L', 'I', 'Z', ' ', 'A', 'N','I', 'V', 'E', 'R', 'S', 'A', 'R', 'I', 'O'};
int length = chars.length;
for (int i = 1; i < length; i += 2) {
for (int j = 0; j < 9 - i / 2; j++)
System.out.print(" ");
for (int j = 0; j < i; j++)
System.out.print(chars[j]);
System.out.print("\n");
}
for (int i = length; i > 0; i -= 2) {
for (int j = 0; j < 9 - i / 2; j++)
System.out.print(" ");
for (int j = 0; j < i; j++)
System.out.print(chars[j]);
System.out.print("\n");
並打印出這個:
F
FEL
FELIZ
FELIZ A
FELIZ ANI
FELIZ ANIVE
FELIZ ANIVERS
FELIZ ANIVERSAR
FELIZ ANIVERSARIO
FELIZ ANIVERSAR
FELIZ ANIVERS
FELIZ ANIVE
FELIZ ANI
FELIZ A
FELIZ
FEL
F
但我需要它從數組中間的字符開始打印。 最終結果必須是這樣的:
I
NIV
ANIVE
ANIVER
Z ANIVERS
IZ ANIVERSA
LIZ ANIVERSAR
ELIZ ANIVERSARI
FELIZ ANIVERSARIO
ELIZ ANIVERSARI
LIZ ANIVERSAR
IZ ANIVERSA
Z ANIVERS
ANIVER
ANIVE
NIV
I
任何幫助將不勝感激。
您可以執行與以下代碼中顯示相同的操作。 它將使用較少數量的for循環。 我在代碼中添加了內聯注釋。
char[] chars = { 'F', 'E', 'L', 'I', 'Z', ' ', 'A', 'N', 'I', 'V', 'E', 'R', 'S', 'A', 'R', 'I', 'O' };
int length = chars.length;
for (int line=0;line<=length;line++) {
//This will print upper part of pyramid
if(line < length/2){
String output="";
int middelVal=length/2;
for (int i = middelVal - line; i > 0; i--) {
output=output+" ";
}
for (int i = middelVal - line; i <= middelVal + line; i++) {
output=output+chars[i];
}
System.out.println(output);
}else if(line > length/2){
//This will print lower part of pyramid
String output="";
int middelVal=length/2;
int nwNum = chars.length-line;
for (int i = middelVal - nwNum; i > 0; i--) {
output=output+" ";
}
for (int i = middelVal - nwNum; i <= middelVal + nwNum; i++) {
output=output+chars[i];
}
System.out.println(output);
}
}
獲取中間索引,將其存儲在兩個變量中將標記開始和結束字符打印的位置。
int mid = length / 2; // left end
int midLen = mid; // right end
循環將開始打印空間,直到左端mid
,然后打印字符,直到右端midLen + 1
。 然后將midLen
增加到更右邊,然后減少mid
進一步向左。
for(int i = mid; mid > -1; mid--, midLen++) {
for(int s = 0; s < mid; s++)
System.out.print(" ");
for (int j = mid; j < midLen + 1; j++) {
System.out.print(chars[j]);
}
System.out.print("\n");
}
要打印下金字塔,我們將從0
索引到length
。 這一次,我們增加mid
進一步向右,然后遞減midLen
再往左,截斷每行的字符串直到中間字符。
mid = 0;
midLen = length;
for(int i = mid; midLen != length / 2; mid++, midLen--) {
for(int s = 0; s < mid; s++)
System.out.print(" ");
for(int j = mid; j < midLen; j++) {
System.out.print(chars[j]);
}
System.out.print("\n");
}
獎勵:利用Java 8的新功能 。 同樣的機制,我會留給你檢查。
...
static int mid;
static int midLen;
static String line = "";
public static void main(String[] args) {
char[] chars = {'F', 'E', 'L', 'I', 'Z', ' ', 'A', 'N','I', 'V', 'E', 'R', 'S', 'A', 'R', 'I', 'O'};
mid = chars.length / 2;
midLen = mid;
List<String> tri = new ArrayList<String>();
IntStream.rangeClosed(0, mid)
.forEach(i -> {
Stream.of(chars).forEach( j -> {
IntStream.range(0, mid).forEach(x -> line += " ");
IntStream.rangeClosed(mid, midLen)
.forEach(x -> line += String.valueOf(chars[x]));
mid--;
midLen++;
tri.add(line);
line = "";
});
});
tri.forEach(System.out::println); // print upper pyramid
Collections.reverse(tri);
tri.forEach(System.out::println); // print lower pyramid
}
...
這是使用2d數組的另一種方法
char[] chars = { 'F', 'E', 'L', 'I', 'Z', ' ', 'A', 'N', 'I', 'V', 'E', 'R', 'S', 'A', 'R', 'I', 'O' };
int size = chars.length;
int mid = size / 2 + 1;
char[][] matrix = new char[size][size];
for (char[] cs : matrix) {
Arrays.fill(cs, ' ');
}
for (int i = 0; i < size; i++) {
int l = Math.abs(mid - 1 - i);
int r = size - l;
for (int m = l; m < r; m++) {
matrix[i][m] = chars[m];
}
}
for (char[] cs : matrix) {
System.out.println(cs);
}
產量
I
NIV
ANIVE
ANIVER
Z ANIVERS
IZ ANIVERSA
LIZ ANIVERSAR
ELIZ ANIVERSARI
FELIZ ANIVERSARIO
ELIZ ANIVERSARI
LIZ ANIVERSAR
IZ ANIVERSA
Z ANIVERS
ANIVER
ANIVE
NIV
I
這是我的頭腦,但我嘗試的一種方法是這樣的:
找到數組中中間字符的索引。 稱之為'startIndex'
對於每一行,我會找到長度,除以2(向下舍入),這將是startIndex的起始偏移,它決定了我的第一個字符是什么。
讓我們說我在第5行,你有“Z ANIVERS”。 長度為9.除以2(向下舍入),得到4。
在chars數組中,startIndex計算為8(17/2,向下舍入)。 因此,我應該從char數組中的char 4(即startIndex - 4)開始打印,直到需要很多字符。
您也可以輕松打印整行而不計算循環中的字符,因為如果您知道所需的行長度和起始字符索引,那么如果您將初始字符作為一個字符串,則可以將該行一行子串。
最后,你也可以只完成一半的工作,因為看起來你的上半部分與下半部分相同(在字符串中構建結果然后將它們連接在一起以打印出完整的結果)
編輯:閱讀我的答案,你不需要實際“找到”行長度,因為它只是從1開始,每次增加2,但原始方法仍然是相同的。
編輯2:
由於每個人都在添加他們的編碼版本(並且答案已被接受),我不妨按照我的說法添加我的:
String sourceString = "FELIZ ANIVERSARIO"; // source of characters
String padding = " "; // used to create the padding offset for each line
int middle = sourceString.length() / 2;
// top and bottom halves of the output
String top = "";
String bottom = "";
for(int currentLength = 1; currentLength < sourceString.length(); currentLength += 2){
String linePadding = padding.substring(currentLength / 2, padding.length());
// speical case here, in the given example by the post, the 4th line the offset is one less as the I characters do not line up in the middle
// 7 is used as the 4th line has a length of 7
if(currentLength == 7){
linePadding = linePadding.substring(1, linePadding.length());
}
top += linePadding + sourceString.substring(middle - (currentLength / 2), middle + (currentLength / 2) + 1) +"\n";
bottom = linePadding + sourceString.substring(middle - (currentLength / 2), middle + (currentLength / 2) + 1) + "\n" + bottom;
}
System.out.println(top + sourceString +"\n"+ bottom);
不確定這是否是故意的,但我在循環中添加了一個特殊的期望,我在用戶的例子中注意到第4行是1(我不在那里排隊)。 如果我應該排隊並且這是一個拼寫錯誤,可以刪除那個if block。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.