Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class JeffGriggOneLoop {
- public static void main(final String[] args) {
- printLinesWithValuesUpTo(7);
- }
- public static void printLinesWithValuesUpTo(final int largestValue) {
- int largestInThisLine = 1;
- int printValue = 1;
- int increment = 1;
- while (largestInThisLine <= largestValue) {
- System.out.print(printValue + " ");
- if (printValue == largestInThisLine) { // When you hit the top value, ...
- increment = -1; // count down (to the end of the line).
- }
- printValue += increment;
- if (printValue == 0) { // At the end of each line...
- System.out.println(); // Go to the next line.
- printValue = 1; // Start again at one.
- increment = 1; // Count up from there.
- ++largestInThisLine; // The next line goes up to a value that's one larger.
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement