Jgat5
Jgat5

Reputation: 1

Nested for loop not incrementing by 1

In the below code block:

public static void main(String[] args) {
    int i;
    int x = 0;
    int count;
    int sum = 0;
    Scanner scan = new Scanner(System.in);

    System.out.println("Enter The Table Operator (+, -, *, /, %, or R)");
    String operator = scan.nextLine();

    System.out.println("Enter The Smallest Operand For the Table:");
    int small = scan.nextInt();

    System.out.println("Enter The Largest Operand For the Table");
    int large = scan.nextInt();

    for(i = 0; i < 12; i++) {
      for(x = 0; x <= large; x ++)
        System.out.printf("%4d", x + small);
      System.out.printf("\n");
      x++;
      large++;
    }
}

The code outputs:

   1   2   3   4   5   6   7   8   9  10  11  12  13
   1   2   3   4   5   6   7   8   9  10  11  12  13
   1   2   3   4   5   6   7   8   9  10  11  12  13
   1   2   3   4   5   6   7   8   9  10  11  12  13
   1   2   3   4   5   6   7   8   9  10  11  12  13
   1   2   3   4   5   6   7   8   9  10  11  12  13
   1   2   3   4   5   6   7   8   9  10  11  12  13
   1   2   3   4   5   6   7   8   9  10  11  12  13
   1   2   3   4   5   6   7   8   9  10  11  12  13
   1   2   3   4   5   6   7   8   9  10  11  12  13
   1   2   3   4   5   6   7   8   9  10  11  12  13
   1   2   3   4   5   6   7   8   9  10  11  12  13

but I want it to look like this:

1 2 3 4 5 6 7 8 9 10 11 12 13
2 3 4 5 6 7 8 9 10 11 12 13 14
3 4 5 6 7 8 9 10 11 12 13 14 15
4 5 6 7 8 9 10 11 12 13 14 15 16
5 6 7 8 9 10 11 12 13 14 15 16 17
6 7 8 9 10 11 12 13 14 15 16 17 18
7 8 9 10 11 12 13 14 15 16 17 18 19
8 9 10 11 12 13 14 15 16 17 18 19 20
9 10 11 12 13 14 15 16 17 18 19 20 21
10 11 12 13 14 15 16 17 18 19 20 21 22
11 12 13 14 15 16 17 18 19 20 21 22 23
12 13 14 15 16 17 18 19 20 21 22 23 24

It's basically an addition table. I'm trying to get the x value and the value of "large" to implement by one on each loop. I'm pretty sure I'm using the for loop incorrectly but I haven't been able to find out how.

Upvotes: 0

Views: 878

Answers (4)

Amutheezan
Amutheezan

Reputation: 355

    int small = 1;
    int large = 12;
    for(int i = 0; i < 12; i++) {
        for(int x = i; x <= i + large; x ++)
        {
            System.out.printf("%4d", x + small);
        }
        System.out.printf("\n");

    }

checkout this simple change by changing the x ranges from i -> i + large which will avoid increment large ( because it is not need as in every iteration i also increase)

what you are trying to is in every iteration of i and x you are increment it at the end of the print operation that wont effect exactly as you want because for every iteration the value you print should iterate and print so change the x value to i, which will get incremented in every iteration and use it till i + large (where large is the number you obtain as maximum limit)

Note this can be achieved since small, and large are constant through out the loop

Upvotes: 0

Oleg Cherednik
Oleg Cherednik

Reputation: 18245

for (int row = 1; row <= 12; row++) {
    for (int col = row; col < row + 13; col++)
        System.out.print(col != row ? " " + col : col);

    System.out.println();
}

This pretty simple trianlge. Just inline all temporary variables like row and 'col' into the loops. Be simple.

Upvotes: 3

Rakesh
Rakesh

Reputation: 39

Your loop looks right. You are incrementing the wrong variables.

for(i = 0; i < large; i++) {
    for(x = 0; x <= large; x ++){
        System.out.printf("%4d", x + small);
    }
    System.out.printf("\n");
    small++;
}

output:

   1   2   3   4   5   6   7   8   9  10  11  12  13
   2   3   4   5   6   7   8   9  10  11  12  13  14
   3   4   5   6   7   8   9  10  11  12  13  14  15
   4   5   6   7   8   9  10  11  12  13  14  15  16
   5   6   7   8   9  10  11  12  13  14  15  16  17
   6   7   8   9  10  11  12  13  14  15  16  17  18
   7   8   9  10  11  12  13  14  15  16  17  18  19
   8   9  10  11  12  13  14  15  16  17  18  19  20
   9  10  11  12  13  14  15  16  17  18  19  20  21
  10  11  12  13  14  15  16  17  18  19  20  21  22
  11  12  13  14  15  16  17  18  19  20  21  22  23
  12  13  14  15  16  17  18  19  20  21  22  23  24

Upvotes: 2

faris
faris

Reputation: 702

int large = 10;
int small = 0;
int x,i;
for( i = 0; i < large; i++) {
    for( x = 1; x <= large; x ++)
        System.out.printf("%d ", x + small);
    System.out.printf("\n");
    x++;
    small++;
}
  1. Change "%4d" to "%d ". %4d makes every number 4 spaces long no matter what but you wanted the number and one space.

  2. The last line in your outer for-loop should be small++ instead of large++ to offset the start value each time.

Upvotes: 2

Related Questions