[英]How to print out an X using nested loops
我經過搜索以找到解決此問題的簡單方法。
我有一個方法叫做
printCross(int size,char display)
它接受一個大小,並打印一個帶有char變量的X,該變量接收高度和寬度的大小。
調用方法printShape(int maxSize, char display)
接受形狀的最大大小並循環執行,向printCross方法發送2的倍數,直到達到最大值。
這是我的代碼,但沒有給我期望的結果。
public static void drawShape(char display, int maxSize)
{
int currentSize = 2; //start at 2 and increase in multiples of 2 till maxSize
while(currentSize<=maxSize)
{
printCross(currentSize,display);
currentSize = currentSize + 2;//increment by multiples of 2
}
}
public static void printCross(int size, char display)
{
for (int row = 0; row<size; row++)
{
for (int col=0; col<size; col++)
{
if (row == col)
System.out.print(display);
if (row == 1 && col == 5)
System.out.print(display);
if (row == 2 && col == 4)
System.out.print(display);
if ( row == 4 && col == 2)
System.out.print(display);
if (row == 5 && col == 1)
System.out.print(display);
else
System.out.print(" ");
}
System.out.println();
}
}
是否因為我將數字硬編碼到循環中? 我做了很多數學運算,但是不幸的是,只有這樣,我才剛剛接近達到期望的輸出。
If the printCross() method received a size of 5 for instance, the output should be like this:
x x
x x
x
x x
x x
拜托,我已經花了數周的時間,似乎一無所獲。 謝謝
您要做的第一件事是找到索引之間的關系 。 假設您有一個長度為size
的方陣(示例中為size = 5
):
0 1 2 3 4
0 x x
1 x x
2 x
3 x x
4 x x
您會注意到,在從(0,0)
到(4,4)
的對角線中,索引是相同的(在代碼中這意味着row == col
)。
此外,您還可以注意到,在從(0,4)
到(4,0)
的對角線中,索引總和為4
, size - 1
(在代碼中為row + col == size - 1
)。
因此,在代碼中,您將遍歷行,然后遍歷列(嵌套循環)。 在每次迭代中,您必須檢查是否滿足上述條件。 邏輯OR( ||
)運算符用於避免使用兩個if
語句。
碼:
public static void printCross(int size, char display)
{
for (int row = 0; row < size; row++) {
for (int col = 0; col < size; col++) {
if (row == col || row + col == size - 1) {
System.out.print(display);
} else {
System.out.print(" ");
}
}
System.out.println();
}
}
輸出:( (size = 5, display = 'x')
x x
x x
x
x x
x x
除了直接給出答案外,我還會給您一些提示。
首先,使用嵌套的for循環是正確的。
但是,正如您所注意到的,對於5情況,您確定何時打印'x'。
僅當row = col或row + col = size-1時檢查是否打印了“ x”
對於您的printCross方法,請嘗試以下操作:
public static void printCross(int size, char display) {
if( size <= 0 ) {
return;
}
for( int row = 0; row < size; row++ ) {
for( int col = 0; col < size; col++ ) {
if( col == row || col == size - row - 1) {
System.out.print(display);
}
else {
System.out.print(" ");
}
}
System.out.println();
}
}
啊,我被毆打了xD
這是一個簡短的丑陋解決方案,它不使用任何空格字符串或嵌套循環。
public static void printCross(int size, char display) {
for (int i = 1, j = size; i <= size && j > 0; i++, j--) {
System.out.printf(
i < j ? "%" + i + "s" + "%" + (j - i) + "s%n"
: i > j ? "%" + j + "s" + "%" + (i - j) + "s%n"
: "%" + i + "s%n", //intersection
display, display
);
}
}
Lte嘗試使用此簡單代碼來打印十字圖案。
class CrossPattern { public static void main(String[] args) { Scanner s = new Scanner(System.in); System.out.println("enter the number of rows=column"); int n = s.nextInt(); int i, j; s.close(); for (i = 1; i <= n; i++) { for (j = 1; j <= n; j++) { if (j == i) { System.out.print("*"); } else if (j == n - (i - 1)) { System.out.print("*"); } else { System.out.print(" "); } } System.out.println(); } } }
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.