如何使用 Tapestry 组件 t:loop 绘制钻石

发布于 2024-11-16 15:36:41 字数 3228 浏览 3 评论 0原文

对于我的 Tapestry 作业,我必须在桌子上展示一串字符串中的一颗钻石。到目前为止,这是我所拥有的:

code Index.java

  public class Index
    {
        @Property
        private Integer number;

        @Property
        private String [] table; 

        public Index() {
            number = 9;
            int temp = 0;

            String tmp = "-";
            table = new String[number * number];

            if(singleCell == null)
                singleCell="";

            for (int i = 0; i <  number; i++) {
                for (int j = 0; j <  number; j++) {
                    table[temp] = tmp;
                    temp++;
                }               
            }
        }

        @OnEvent(component="diamond")
        Object onDiamondLink() {
            String swapValue = "*";

            int  sum = number / 2 ;

            int x1 = number-1;

            int sumY = number / 2;

            int y1 = number+1;

            int temp = x1 + sumY;

            for (int i = 0; i < table.length; i++) {
                table[i] = "-";
            }

            for (int i = 0; i < table.length; i++) {
                if( i == sum) {
                    table[i] = swapValue;
                    sum = sum + x1;
                }
                if ( i == sumY ) {
                    table[i] = swapValue;
                    sumY = sumY + y1;
                } 
            }   

            System.out.println("link diamond is activate");
            return null;
        }
 public boolean isStartRow(){
         return (myIndex%9 ==0);
     }

     public boolean isEndRow(){
         return (myIndex%9 == 8);
     }

     public String getStartTR(){
         return "<tr >";
     }

     public String getEndTR(){
         return "</tr>";
    }

code of index.tml:

<t:actionlink t:id="diamond" >Diamond  table</t:actionlink>
            <br/>



         <h1>Result:</h1>

        <table border="1" >
            <t:loop t:source="table" t:value="singleCell" index="MyIndex">
                <t:if test="startRow">
                 <t:outputraw  value="startTR"/>
                </t:if>
                <td width="20px">
                    ${singleCell}
                </td>
            <t:if test="endRow">
                   <t:outputraw value="endTR"/>
           </t:if> 
            </t:loop>
        </table>

此代码生成此输出:

-   -   -   -   *   -   -   -   -
-   -   -   *   -   *   -   -   -
-   -   *   -   -   -   *   -   -
-   *   -   -   -   -   -   *   -
*   -   -   -   -   -   -   -   *
-   -   -   -   -   -   -   *   -
*   -   -   -   -   -   *   -   -
-   *   -   -   -   *   -   -   -
-   -   *   -   *   -   -   -   -

我需要的正确输出是这样的:

-   -   -   -   *   -   -   -   -
-   -   -   *   -   *   -   -   -
-   -   *   -   -   -   *   -   -
-   *   -   -   -   -   -   *   -
*   -   -   -   -   -   -   -   *
-   *   -   -   -   -   -   *   -
-   -   *   -   -   -   *   -   -
-   -   -   *   -   *   -   -   -
-   -   -   -   *   -   -   -   -

任何想法都会有很大帮助。

For my homework for Tapestry, I have to show a diamond on table from array of strings. Here's what I have so far:

code Index.java

  public class Index
    {
        @Property
        private Integer number;

        @Property
        private String [] table; 

        public Index() {
            number = 9;
            int temp = 0;

            String tmp = "-";
            table = new String[number * number];

            if(singleCell == null)
                singleCell="";

            for (int i = 0; i <  number; i++) {
                for (int j = 0; j <  number; j++) {
                    table[temp] = tmp;
                    temp++;
                }               
            }
        }

        @OnEvent(component="diamond")
        Object onDiamondLink() {
            String swapValue = "*";

            int  sum = number / 2 ;

            int x1 = number-1;

            int sumY = number / 2;

            int y1 = number+1;

            int temp = x1 + sumY;

            for (int i = 0; i < table.length; i++) {
                table[i] = "-";
            }

            for (int i = 0; i < table.length; i++) {
                if( i == sum) {
                    table[i] = swapValue;
                    sum = sum + x1;
                }
                if ( i == sumY ) {
                    table[i] = swapValue;
                    sumY = sumY + y1;
                } 
            }   

            System.out.println("link diamond is activate");
            return null;
        }
 public boolean isStartRow(){
         return (myIndex%9 ==0);
     }

     public boolean isEndRow(){
         return (myIndex%9 == 8);
     }

     public String getStartTR(){
         return "<tr >";
     }

     public String getEndTR(){
         return "</tr>";
    }

code of index.tml:

<t:actionlink t:id="diamond" >Diamond  table</t:actionlink>
            <br/>



         <h1>Result:</h1>

        <table border="1" >
            <t:loop t:source="table" t:value="singleCell" index="MyIndex">
                <t:if test="startRow">
                 <t:outputraw  value="startTR"/>
                </t:if>
                <td width="20px">
                    ${singleCell}
                </td>
            <t:if test="endRow">
                   <t:outputraw value="endTR"/>
           </t:if> 
            </t:loop>
        </table>

This code generates this output:

-   -   -   -   *   -   -   -   -
-   -   -   *   -   *   -   -   -
-   -   *   -   -   -   *   -   -
-   *   -   -   -   -   -   *   -
*   -   -   -   -   -   -   -   *
-   -   -   -   -   -   -   *   -
*   -   -   -   -   -   *   -   -
-   *   -   -   -   *   -   -   -
-   -   *   -   *   -   -   -   -

The correct output I need is this:

-   -   -   -   *   -   -   -   -
-   -   -   *   -   *   -   -   -
-   -   *   -   -   -   *   -   -
-   *   -   -   -   -   -   *   -
*   -   -   -   -   -   -   -   *
-   *   -   -   -   -   -   *   -
-   -   *   -   -   -   *   -   -
-   -   -   *   -   *   -   -   -
-   -   -   -   *   -   -   -   -

Any ideas will be great help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

羁〃客ぐ 2024-11-23 15:36:41

想画钻石吗?试试这个算法:

public class Diamond {

    @Property
    @Persist
    private String diamond;


    @SetupRender    
    init(){
         int n,i,j,k;

     do {  

      n = (int)(Math.random() * 10 + 3); 

      }while(n % 2 == 0);

      diamond += ""+n+"<br\/>";

      System.out.println();   

     for (i = 1; i <= n; i++){

        for (k = n; k > i; k--)
          diamond += "-";

        for (j =1; j <= i; j++)
             diamond += "*"+"-";


       diamond += "<br\/>";

        }

     for (i = n; i > 0; i--){

        for (k = n; k > i; k--)
          diamond += "-";

        for (j =1; j <= i; j++)
               diamond += "*"+"-";

              diamond += "<br\/>";

        }
}
}

更新

等一下,你想创建一个挂毯页面,绘制星号的菱形,对吗?

一种选择是使用:

<t:outputraw value="${diamond}"/>

您只需将该字符串设置为页面的 .java 部分。(请参阅上面的代码已更新)

您的输出需要呈现为 html,您可以只使用我们为您提供的算法并插入 html中断而不是 println()

Wanna draw a diamond? Try this algorithm:

public class Diamond {

    @Property
    @Persist
    private String diamond;


    @SetupRender    
    init(){
         int n,i,j,k;

     do {  

      n = (int)(Math.random() * 10 + 3); 

      }while(n % 2 == 0);

      diamond += ""+n+"<br\/>";

      System.out.println();   

     for (i = 1; i <= n; i++){

        for (k = n; k > i; k--)
          diamond += "-";

        for (j =1; j <= i; j++)
             diamond += "*"+"-";


       diamond += "<br\/>";

        }

     for (i = n; i > 0; i--){

        for (k = n; k > i; k--)
          diamond += "-";

        for (j =1; j <= i; j++)
               diamond += "*"+"-";

              diamond += "<br\/>";

        }
}
}

UPDATE

Wait a second, you want to create A tapestry page, that draws that diamond of asterisk right?

One option would be using:

<t:outputraw value="${diamond}"/>

You just need to set that String the .java part of your page.(See the above code was updated)

Your output need to be rendered as html, you can just use the algorithms we gave you and insert html breaks instead of println()

总以为 2024-11-23 15:36:41

这应该打印所需的输出:

public class Diamond
{
  public static void main( String []args)
  {
    for(int i=0;i<9;i++){ 
      for(int j=0;j<9;j++)
        if( (i + j == 4 ) || (i-j == 4)||(i+j == 12) || (j-i == 4))
          System.out.print("*");
        else
          System.out.print("-");
      System.out.println();
    }
  }
}

This should print the required output :

public class Diamond
{
  public static void main( String []args)
  {
    for(int i=0;i<9;i++){ 
      for(int j=0;j<9;j++)
        if( (i + j == 4 ) || (i-j == 4)||(i+j == 12) || (j-i == 4))
          System.out.print("*");
        else
          System.out.print("-");
      System.out.println();
    }
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文