Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>a for loop will loop through a collection of data, such as an array. The classic for loop looks like this:</p> <pre><code> for(counter=0;counter &lt;= iterations;counter++){ } </code></pre> <p>the first param is a counter variable. the second param expresses how long the loop should last, and the 3rd param expresses how much the counter should be incremented by after each pass.</p> <p>if we want to loop from 1 - 10, we do the following:</p> <pre><code>for(counter=1;counter&lt;=10;counter++){ System.out.println(counter); } </code></pre> <p>if we want to loop from 10 - 1, we do the following:</p> <pre><code>for(counter=10;counter&gt;=1;counter--){ System.out.println(counter); } </code></pre> <p>if we want to loop through a 2 dimensional collection, like...</p> <pre><code>1 2 3 4 5 6 7 8 9 int[][] grid = new int[][] {{1,2,3},{4,5,6},{7,8,9}}; </code></pre> <p>we need 2 loops. The outer loop will run through all the rows, and the inner loop will run through all the columns.</p> <p>you are going to need 2 loops, one to iterate through the rows, one to iterate through the columns.</p> <pre><code> for(i=0;i&lt;grid.length;i++){ //this will loop through all rows... for(j=0;j&lt;grid[i].length;j++){ //will go through all the columns in the first row, then all the cols in the 2nd row,etc System.out.println('row ' + i + '-' + 'column' + j + ':' + grid[i][j]); } } </code></pre> <p>In the outer loop, we set a counter to 0 for the first parameter. for the second, to calculate how many times we will loop, we use the length of the array, which will be 3, and for the third param, we increment by one. we can use the counter, i, to reference where we are inside the loop. </p> <p>We then determine the length of the specific row by using grid[i].length. This will calculate the length of each row as they are being looped through.</p> <p>Please feel free to ask any questions you may have regarding for loops!</p> <p>EDIT: understanding the question.....</p> <p>You are going to have to do several things with your code. Here we will store the number of lines in a variable, speak up if you need to pass in this value to a method.</p> <pre><code> int lines = 10; //the number of lines String carat = "&gt;"; for(i=1;i&lt;=lines;i++){ System.out.println(carat + "\n"); // last part for a newline carat = carat + "&gt;&gt;"; } </code></pre> <p>The above will print out carats going all the way up. We print out the carat variable then we make the carat variable 2 carats longer.</p> <p>.... the next thing to do is to implement something that will decide when to decrease the carats, or we can go up half of them and down the other half.</p> <p>Edit 3:</p> <pre><code>Class Test { public static void main(String[] args) { int lines = 7; int half = lines/2; boolean even = false; String carat = "&gt;"; int i; if(lines%2==0){even = true;} //if it is an even number, remainder will be 0 for(i=1;i&lt;=lines;i++){ System.out.println(carat + "\n"); if(i==half &amp;&amp; even){System.out.println(carat+"\n");} // print the line again if this is the middle number and the number of lines is even if(((i&gt;=half &amp;&amp; even) || (i&gt;=half+1)) &amp;&amp; i!=lines){ // in english : if the number is even and equal to or over halfway, or if it is one more than halfway (for odd lined output), and this is not the last time through the loop, then lop 2 characters off the end of the string carat = carat.substring(0,carat.length()-2); }else{ carat = carat + "&gt;&gt;"; //otherwise, going up } } } } </code></pre> <p>Explanation and commentary along shortly. Apologies if this is over complicated (i'm pretty sure this is not even close to the best way to solve this problem).</p> <p>Thinking about the problem, we have a hump that appears halfway for even numbers, and halfway rounded up for the odd numbers. </p> <p>At the hump, if it is even, we have to repeat the string. </p> <p>We have to then start taking off "&lt;&lt;" each time, since we are going down.</p> <p>Please ask if you have questions.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload