【问题标题】:Java print a pattern with nested for loopJava 使用嵌套的 for 循环打印模式
【发布时间】:2018-05-09 08:43:35
【问题描述】:

我正在努力构建一种算法来打印急需的模式。 代码如下:

public static void printPatternH(int size)
{
    for (int row = 1; row <= size; row++)
    {
        for (int col = 1; col <= 2*size; col++)
        {
            if (col > size + row - 1) {
                continue;
            }
            if (col <= size) {
                System.out.print((row + col >= size + 1 ? (row + col)%size : " ") + " ");
            }
            else {
                System.out.print((row + col >= size + 1 ? (row + size)%col : " ") + " ");
            }                
        }
        System.out.println();
    }
}

结果是:

我知道如果size 是 9,那么中间的最后一个数字将是 0,就像 (row + size)%col = 0,但是我想不出在不更改其余值的情况下修改它的方法。

【问题讨论】:

  • 所以你想打印“9”而不是“0”?
  • (row + col)%size -> (row + col - 1) % size + 1
  • @saka1029,这就是答案。小心张贴:)
  • @AgelKoh 是的,没错,来自 saka1029 的解决方案满足了我的查询 :) 谢谢

标签: java for-loop nested


【解决方案1】:

改变

(row + col)%size

(row + col - 1) % size + 1

【讨论】:

    【解决方案2】:

    您可以在打印出来之前检查“0”并替换它:

     if (col <= size) {
         //print left hand side
         int remainder =  (row + col) % size;
         if (remainder == 0) remainder = size; //replace the "0" with size here.
         System.out.print((row + col >= size + 1 ? remainder : " ") + " ");
    } else {
         //print right hand side
         System.out.print((row + col >= size + 1 ? (row + size) % col : " ") + " ");
    }
    

    它会给出这个输出:

                    1 
                  1 2 1 
                1 2 3 2 1 
              1 2 3 4 3 2 1 
            1 2 3 4 5 4 3 2 1 
          1 2 3 4 5 6 5 4 3 2 1 
        1 2 3 4 5 6 7 6 5 4 3 2 1 
      1 2 3 4 5 6 7 8 7 6 5 4 3 2 1 
    1 2 3 4 5 6 7 8 9 8 7 6 5 4 3 2 1 
    

    【讨论】:

      【解决方案3】:
      public class Testb 
      {
          int item = 9;
          int limit = 5;
          int half = item/2;
          public static void main(String[] args) 
          {
              //System.out.println("Hello World!");
              for(int i=0;i<limit;i++){
                
                 for(int j=0;j<half;j++){
                    
                    if(j<(half-i)){
                     
                       print(" ");
                     
                    }else{
                    
                       print(j-(half-i))
                    
                    }          
                 }
                 
                 print(i);
      
                 for(int k=half;k<(half+i);k++){
                 
                     print((half+i)-(k+1));
                 
                 }
      
                 println();
              }
          }
      }
      

      输出:

                0
              0 1 0
            0 1 2 1 0
          0 1 2 3 2 1 0
        0 1 2 3 4 3 2 1 0
      

      【讨论】:

        【解决方案4】:
         int rowCount = 1;
        
            System.out.println("Here Is Your Pyramid");
        
            //Implementing the logic
        
            for (int i = noOfRows; i > 0; i--)
            {
                //Printing i*2 spaces at the beginning of each row
        
                for (int j = 1; j <= i*2; j++)
                {
                    System.out.print(" ");
                }
        
                //Printing j value where j value will be from 1 to rowCount
        
                for (int j = 1; j <= rowCount; j++)             
                {
                    System.out.print(j+" ");
                }
        
                //Printing j value where j value will be from rowCount-1 to 1
                 
                for (int j = rowCount-1; j >= 1; j--)
                {                 
                    System.out.print(j+" ");             
                }                          
                 
                System.out.println();
        
                //Incrementing the rowCount
        
                rowCount++;
            }
        

        【讨论】:

        • 他并没有要求完全不同于他的方法,但要修复他的方法,他不会学习它,他只是使用另一种方法。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-02-27
        • 1970-01-01
        • 2012-09-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多