Advertisement
JeffGrigg

JeffGriggOneLoop

Apr 23rd, 2017
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 1.01 KB | None | 0 0
  1. public class JeffGriggOneLoop {
  2.     public static void main(final String[] args) {
  3.         printLinesWithValuesUpTo(7);
  4.     }
  5.  
  6.     public static void printLinesWithValuesUpTo(final int largestValue) {
  7.         int largestInThisLine = 1;
  8.         int printValue = 1;
  9.         int increment = 1;
  10.         while (largestInThisLine <= largestValue) {
  11.             System.out.print(printValue + " ");
  12.             if (printValue == largestInThisLine) {  // When you hit the top value, ...
  13.                 increment = -1;                     //  count down (to the end of the line).
  14.             }
  15.             printValue += increment;
  16.             if (printValue == 0) {      // At the end of each line...
  17.                 System.out.println();   //  Go to the next line.
  18.                 printValue = 1;         //  Start again at one.
  19.                 increment = 1;          //  Count up from there.
  20.                 ++largestInThisLine;    //  The next line goes up to a value that's one larger.
  21.             }
  22.         }
  23.     }
  24. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement