【问题标题】:easy java code,printing stars简单的java代码,打印星星
【发布时间】:2012-05-19 22:50:17
【问题描述】:

这里是打印一个下一个三角形的java代码, 将它们命名为 A、B、C、D;我的问题是如何将它们打印在*同一级别*

public class ex_5_10 {
public static void main(String args[]){
    // (A)
    System.out.println("(A)") ;

    for(int row = 0 ; row < 10 ; row++){
        for(int column = 0 ; column < 10 ; column++){
            if(column > row) continue ;
            System.out.print("*");
        }
        System.out.println() ;
    }
    //*********************************
    // (B)
    System.out.println("(B)") ;

    for(int row = 0 ; row < 10 ; row++){
        for(int column = 0 ; column < 10 ; column++){
            if(column < row) continue ;
            System.out.print("*");
        }
        System.out.println() ;
    }
    //********************************
    //(C)
    System.out.println("(C)") ;
    for(int row = 0 ; row < 10 ; row++){
        for(int column = 0 ; column < 10 ; column++){
            if( column < row ) System.out.print(" ") ;
            else System.out.print("*");
        }
        System.out.println() ;
    }
    // (D)
    System.out.println("(D)") ;
    for(int row = 0 ; row < 10 ; row++){
        for(int column = 10 ; column >= 0 ; column--){
            if( column > row ){ System.out.print(" ") ; }
            else {System.out.print("*"); }
        }
        System.out.println() ;
    }


}

}

所以上面的java代码会打印这个:

*
**
***

***
**
*

***
 **
  *

  *
 **
***

现在我需要在同一级别上打印相同的数字!

*    *** ***   *   
**   **   **  **
***  *     * ***

请帮助我! 我正在寻找你的答案! 提前致谢

【问题讨论】:

  • 看看这个:[gotoXY Discussion][1]这将有助于[1]:stackoverflow.com/questions/1001335/…
  • 你是否允许使用额外的图书馆或者你必须修改这个作业?
  • 伙计,这个练习告诉我们只使用嵌套的 for 循环!

标签: java loops println


【解决方案1】:

对此没有单行答案。

您需要记住,当您调用 println() 时,字符串末尾会有一个换行符输出,因此该行不会再出现任何内容。

考虑到这一点,您需要拆分逻辑,以便首先组装各种“数字”的输出,然后在处理完它们后,输出结果以一种有意义的方式(所以每个字符的第一行,然后是第二行等)。不要在处理每个图形时调用 print,而是将结果放入适当的数据结构中。

对于奖励分数,当提供足够的数据以跨越显示器的宽度时,您需要自动换行...

【讨论】:

    【解决方案2】:

    你为什么不想这样打印?

    System.out.println("*    *** ***   *") ;
    System.out.println("**   **   **  **") ;
    System.out.println("***  *     * ***") ;
    

    更新:好的。

    如果这是循环的作业。只需创建矩阵(数组的数组)并通过分配的矩阵元素打印。

    然后逐行打印出矩阵。

    您应该为每个打印周期使用偏移量。 Fox 示例,伪代码中的第二个(不是 java 代码!):

    //*********************************
    // (B)
    matrix[0][offset] = "B";
    
    for(int row = 0 ; row < height ; row++){
        for(int column = 0 ; column < width ; column++){
            if(column < row) continue ;
            matrix[row][column+offset] = "*";
        }
    }
    offset += width + space_length;
    

    【讨论】:

    • 是的,但他需要向讲师展示代码!他需要循环
    【解决方案3】:

    您可以将所有不同图形的代码放在同一个循环中,然后在每个图形之间放置计算的空格。找出图形之间的空间模式。

    【讨论】:

      【解决方案4】:

      您可以将列的内部 for 循环移动到行的一个循环中,并为列添加偏移量。内部 if 的逻辑都是列 - 偏移量。

      【讨论】:

        【解决方案5】:

        如果你想玩结果,你可以做以下事情:(这是一个家庭作业)

        您可以注意到,在循环中存在以下条件:

        1. if(column &lt; row) continue ; System.out.print("*");
        2. if(column &lt; row) continue ; System.out.print("*");
        3. if( column &lt; row ) System.out.print(" ") ; else System.out.print("*");

        4. if( column &gt; row ){ System.out.print(" ") ; } else {System.out.print("*"); }

        只需将它们混合并重新排列,然后查看结果。 并且还移动行的一个循环内部的列的循环内部,并为列添加偏移量。
        这次你需要使用一个循环块而不是 4 个。

        玩得开心。

        【讨论】:

          【解决方案6】:

          你也可以试试这样的:

          for(int row = 0 ; row < 10 ; row++){    
          
              // A
              for(int column = 0 ; column < 10 ; column++){
                  if(column > row) continue ;
                  System.out.print("*");
              }
              System.out.print("   ");
          
              // B
              for(int column = 0 ; column < 10 ; column++){
                  if(column < row) continue ;
                  System.out.print("*");
              }
              System.out.print("   ");
          
          
              // C
              for(int column = 0 ; column < 10 ; column++){
                  if( column < row ) System.out.print(" ") ;
                  else System.out.print("*");
              }
              System.out.print("   ");
          
              //D
              for(int column = 0 ; column < 10 ; column++){
                  if(column > row) continue ;
                  System.out.print("*");
              }
          
              System.out.println() ;
          }
          

          也许您可以通过将 4 个内部循环更改为一个循环来进一步简化它。但这是你的功课......

          【讨论】:

            【解决方案7】:
            public class ex_5_10 {
            public static void main(String args[]){
                // (A)
                System.out.println("(A)") ;
            
                for(int row = 0 ; row < 10 ; row++){
                    for(int column = 0 ; column < 10 ; column++){
                        if(column > row) continue ;
                        System.out.print("*");
                    }
                    System.out.print() ;
                }
               //*********************************
               // (B)
               System.out.println("(B)") ;
            
               for(int row = 0 ; row < 10 ; row++){
                  for(int column = 0 ; column < 10 ; column++){
                     if(column < row) continue ;
                     System.out.print("*");
                  }
                  System.out.print() ;
               }
               //********************************
               //(C)
               System.out.println("(C)") ;
               for(int row = 0 ; row < 10 ; row++){
                  for(int column = 0 ; column < 10 ; column++){
                      if( column < row ) System.out.print(" ") ;
                      else System.out.print("*");
                  }
                  System.out.print() ;
               }
               // (D)
               System.out.println("(D)") ;
               for(int row = 0 ; row < 10 ; row++){
                   for(int column = 10 ; column >= 0 ; column--){
                     if( column > row ){ System.out.print(" ") ; }
                     else {System.out.print("*"); }
                   }
                   System.out.print() ;
               }
            
            
              }
            

            【讨论】:

              【解决方案8】:
              enter code hereclass y{
                      public static void main(String args[])
                      {
                          for(int x=1;x<=3;x++)
                          {
                              for(int y=1;y<=x;y++)
                              {
                                  System.out.print("*");
                              }
                              for(int sp1=1;sp1<=(4-x);sp1++)
                              {
                                  System.out.print(" ");
                              }
                              for(int se=1;se<=(4-x);se++)
                              {
                                  System.out.print("*");
                              }
                              for(int sp2=1;sp2<(2*x);sp2++)
                              {
                              System.out.print(" ");
                              }
                              for(int p=1;p<x;p++)
                              {
                              System.out.print(" ");
              
                              }
                              for(int stt=1;stt<=(4-x);stt++)
                              {
                              System.out.print("*");
                              }
                              for(int spth=1;spth<=(4-x);spth++)
                              {
                              System.out.print(" ");
                              }
                              for(int stfr=1;stfr<=x;stfr++)
                              {
                              System.out.print("*");
                              }
                              System.out.print("\n");
                          }
              
                      }
              
                  }
              

              【讨论】:

              • 在上面的 x 用于保持对行的计数,
              【解决方案9】:

              int 行 = 10;

                  for(int i=0; i<rows ; i++)
                  {
                      for (int j = 0; j <i; j++) {
                          System.out.print("*");
                      }
                      System.out.println();
                  }
              

              【讨论】:

                【解决方案10】:

                代码

                public class Main {
                
                    public static void main(String[] args) {
                
                    for(int line = 1 ; line<=3; line++){
                        for(int patternOne=1; patternOne<=line; patternOne++ ){
                            System.out.print("*");
                        }
                
                        for(int space=3; space>=line; space--){
                            System.out.print(" ");
                        }
                
                        for(int patternTwo=3; patternTwo>=line; patternTwo--){
                            System.out.print("*");
                        }
                
                        for(int space=1; space<=(line*2)-1; space++){
                            System.out.print(" ");
                        }
                        for(int patternThree=3; patternThree>=line; patternThree-- ){
                            System.out.print("*");
                        }
                        for(int space=3; space>=line; space--){
                            System.out.print(" ");
                
                        }
                        for(int patternFour=1; patternFour<=line; patternFour++){
                            System.out.print("*");
                        }
                
                
                        System.out.println();
                    }
                
                
                    }
                }
                

                输出

                *    *** ***   *   
                **   **   **  **
                ***  *     * ***
                

                【讨论】:

                  猜你喜欢
                  • 2023-03-28
                  • 2013-09-04
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 2018-08-26
                  • 1970-01-01
                  相关资源
                  最近更新 更多