– Loop Solutions

1:  package nnloops;
  2:
  3:  import java.util.Scanner;
  4:
  5:  public class NNLoops {
  6:
  7:      NNLoops()
  8:      {
  9:          //oneToTen();
 10:          //oddNumbers();
 11:          //squareNumbers();
 12:          //random4();
 13:          //even(20);
 14:          //powers(8);
 15:          //areWeThereYet();
 16:          //triangle();
 17:          //tableSquare();
 18:          tableSquares(6);
 19:
 20:      }
 21:
 22:      void oneToTen()
 23:      {
 24:          for(int i=1; i <11; i++)
 25:          {
 26:              System.out.println(i);
 27:          }
 28:      }
 29:
 30:      void oddNumbers()
 31:      {
 32:          for (int i=1; i<20; i+=2)
 33:          {
 34:              System.out.println(i);
 35:          }
 36:      }
 37:
 38:      void squareNumbers()
 39:      {
 40:          for (int i=1; i<11; i++)
 41:          {
 42:              System.out.println(i*i);
 43:          }
 44:      }
 45:
 46:      void random4()
 47:      {
 48:          for (int i=0; i<4; i++)
 49:          {
 50:              System.out.println((int)(Math.random()*10)+1);
 51:          }
 52:      }
 53:
 54:      void even(int n)
 55:      {
 56:          for (int i=1; i<n; i+=2)
 57:          {
 58:              System.out.println(i);
 59:          }
 60:      }
 61:
 62:      void powers (int n)
 63:      {
 64:          for (int i =1; i<=n; i++)
 65:          {
 66:              System.out.println(Math.pow(2, i));
 67:          }
 68:      }
 69:
 70:      void areWeThereYet()
 71:      {
 72:          Scanner scan = new Scanner(System.in);
 73:          do
 74:          {
 75:              System.out.println("Are we there yet?");
 76:          } while(!scan.nextLine().equals("Yes"));
 77:          System.out.println("Good!");
 78:      }
 79:
 80:      void triangle()
 81:      {
 82:          for(int i = 0; i<6; i++)
 83:          {
 84:              for (int j= 0; j<i; j++)
 85:              {
 86:                  System.out.print("*");
 87:              }
 88:              System.out.println("");
 89:          }
 90:      }
 91:
 92:      void tableSquare()
 93:      {
 94:          for(int i = 1; i<=4; i++)
 95:          {
 96:              for (int j=1; j<=4; j++)
 97:              {
 98:                  System.out.print("|"+ i*j +"\t");
 99:              }
100:              System.out.println("|");
101:
102:          }
103:      }
104:
105:      void tableSquares(int n)
106:      {
107:          for(int i = 1; i<=n; i++)
108:          {
109:              for (int j=1; j<=n; j++)
110:              {
111:                  System.out.print("|"+ i*j +"\t");
112:              }
113:              System.out.println("|");
114:
115:          }
116:      }
117:
118:
119:      public static void main(String[] args) {
120:         new NNLoops();
121:      }
122:  }

4 thoughts on “– Loop Solutions”

  1. I could be wrong but tableSquares would not output the exact output from the previous page. You would want:
    void tableSquares(int n)
    106: {
    107: for(int i = 0; i<=n; i++)
    108: {
    109: for (int j=1; j<=n; j++)
    110: {
    111: System.out.print("|"+ (i == 0 ? 1 : i )*j +"\t");
    112: }
    113: System.out.println("|");
    114:
    115: }
    116: }

    1. Oops, you’re right, but it’s my example that’s wrong. I’ll change that. Ternary operators come later…
      Thanks, Colin!

  2. the desired output from the previous page for tableSquares(int n) shows whitespaces and right-aligned numbers. The implementation above doesn’t match that. It would be good to point out in the example that alignment and whitespace matching is not part of the exercise or to adapt the solution.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.