钻石用于循环

时间:2016-06-06 19:12:35

标签: java for-loop

我正在尝试使用for循环基于用户输入打印出菱形。我已经把它打印出钻石的右侧但是不能打印出左侧的钻石。我试过逆转我的代码无济于事。我无法弄清楚我的代码打印出整个钻石的逻辑。提前谢谢!

如果num = 2,则预期的输出是(当然没有行):

 *
*$*
 *

到目前为止,这是我的代码:

//Print out a diamond shape based on user input
for (int i=num; i>0; --i){
    System.out.print("*");
    for (int n=i; n<num; ++n){
        System.out.print("$*");
    }//Ending bracket of nested for loop
    System.out.println();
}//Ending bracket of for loop
//Print out a diamond shape based on user input
for (int i=0; i<num; ++i){
    System.out.print("*");
    for (int n=i; n<num; ++n){
        System.out.print("$*");
    }//Ending bracket of nested for loop
    System.out.println();
}//Ending bracket of for loop

2 个答案:

答案 0 :(得分:0)

你基本上只需要添加另一个循环(一个用于上半部分,一个用于下半部分),在每个循环中添加足够的空格以使钻石居中:

num

我不确定num = 1与示例中钻石形状之间的相关性逻辑。我的代码会打印出num = 2而不是num所需的钻石。因此,在此代码中$确定钻石的最大宽度(num = 2符号的数量)。所以 * *$* *$*$* *$* * 打印:

 *
*$*
 *

如果你想要

num=1

对于num等等,您只需在上面的代码中用num-1替换num--; 的每个出现。或者,您也可以添加行

store
在代码之前

并保持原样。

答案 1 :(得分:0)

我很晚才得到答案,Keiwan打败了我。但这是一个从头开始的循环。也许它对你或其他人仍然有用。我也无法弄清楚使用了什么数字,所以我用它作为中心的层数。

// The number variable.
int num = 2;    // Num is used to specify the amount of layers.
// used variables
int size = num * 2 + 1; // total size of the diamond.
int i;  // row
int j;  // col
int a;  // amount to draw
int s;  // start to draw
if (num == 0) {
    // The diamond has a thickness of 0, only print the center
    System.out.print("$");
} else if (num >= 1) {
    // the diamond has a thickness of at least 1.
    for (i = 1; i <= size; i++) {
        for (j = 1; j <= size; j++) {
            if (i == num + 1 && j == num + 1) {
                // We are in the center, print the $ and continue
                System.out.print("$");
                continue;
            } else {
                if (i <= num + 1) {
                    // amount is row times 2 minus one
                    a = i * 2 - 1;
                } else {
                    // Inverted: amount is (size minus row plus 1) times two minus one
                    a = (size - i + 1 ) * 2 - 1;
                }
                // starting point is (thickness plus one) minus (amount divided by two)
                s = (int)((num + 1) - (a / 2));
                // if col is bigger or equals start and col is less or equals start plus amount minus one
                if (j >= s && j <= s + a - 1) {
                    // print a "#";
                    System.out.print("#");
                } else {
                    // we are not suppose to print, print a space.
                    System.out.print(" ");
                }
            }
        }
        // reached the end of the line, begin on a new lie
        System.out.print("\n");
    }
}

输出:num = 1;

 # 
#$#
 # 

输出:num = 2;

  #  
 ### 
##$##
 ### 
  #  

输出:num = 15;

               #               
              ###              
             #####             
            #######            
           #########           
          ###########          
         #############         
        ###############        
       #################       
      ###################      
     #####################     
    #######################    
   #########################   
  ###########################  
 ############################# 
###############$###############
 ############################# 
  ###########################  
   #########################   
    #######################    
     #####################     
      ###################      
       #################       
        ###############        
         #############         
          ###########          
           #########           
            #######            
             #####             
              ###              
               # 

编辑:1来源(拼写错误)的评论中的小编辑