How to draw a diamond using t: loop tapestry component

For my homework for the Tapestry, I have to show the diamond on the table from an array of strings. Here is what I still have:

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 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:

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

I need the correct result:

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

Any ideas would be a big help.

+6
source share
2 answers

Want to 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, do you want to create a tapestry page that correctly draws this diamond asterisk?

One of the options:

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

You just need to establish that String is part of the .java of your page. (See the code above has been updated)

Your output should display as html, you can just use the algorithms that we have provided for you and insert html breaks instead of println ()

+2
source

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 ) || (ij == 4)||(i+j == 12) || (ji == 4)) System.out.print("*"); else System.out.print("-"); System.out.println(); } } } 
0
source

Source: https://habr.com/ru/post/891227/


All Articles