簡體   English   中英

Java兩個外部for循環,兩個內部for循環添加到字符串中,第一個不執行任何操作

[英]Java two for outer for loops with two inner for loops that add to a string and the first one doesn't do anything

public static void diamondOfAsterisks(int numOfRows){
   String diamondStr="";

   for (int row = 1; row == (numOfRows/2+1); row++){
       for (int space = numOfRows; space < row; space--){
           diamondStr += " ";
       }
       for (int stars = 1; stars >= (2 * row) - 1; stars++){
           diamondStr+= "*";
       }
       diamondStr+="\n";
    }

   for (int row = numOfRows/2 ; row >= 1; row--){
       for (int space = numOfRows; space > row; space--){
           diamondStr += " ";
       }
       for (int stars = 1; stars <= (2 * row)-1; stars ++){
           diamondStr += "*";
       }
       diamondStr += "\n";
   }
   System.out.print(diamondStr);

我正在編寫一個Java程序來創建由星號組成的菱形。 上面的代碼基於提供給我的偽代碼。 但是,在具有Java 8u25的NetBeans 8.0.1中(無疑這是IDE的問題,但以防萬一),第一個for循環似乎未向diamondStr添加任何內容。 我誤解或做錯了什么?

在第一個for循環中,執行循環的條件是row ==(numOfRows / 2 + 1),我認為這是一個錯誤,應將其更改為row <=(numOfRows / 2 + 1)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM