Jump to content

Very short code help

- - - - -

  • Please log in to reply
2 replies to this topic

#1
bchav

bchav

    Newbie

  • Members
  • Pip
  • 4 posts
I'm trying to get this to print exactly like this:

3, 4, 5, 6, 7, 8

4, 5, 6, 7, 8, 9

5, 6, 7, 8, 9, 10

6, 7, 8, 9, 10, 11

here is my code which is close but cant get it to go 1 number up:
for(int a = 1; a < 6; a++){
for(int b = 2; b < 8; b++){
System.out.print(b + "," + " ");}
System.out.println();
System.out.println();
}



:confused:

#2
fread

fread

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 787 posts
Your second loop starts printing b with a value of two. Your data set starts from three. Each run of the inner loop you print the same set of data. You need to set a start point for each run of the inner loop. i.e start at three and increment by 1. after each iteration increment you start point. But this could be done with one loop.

     int i; 
     int start;
     
     for(start = 3, i = 0; start < 7 ; i++)
     {
               if (i < 5) 
               System.out.print(start + i + ",  " );
               if (i == 5)
               {
                     System.out.println(start + i);
                     start++;
                     i = -1;
                  }
       }

Perfection of means and confusion of ends seem to characterize our age. Albert Einstein :confused:

#3
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
You can use b = 2 + a to initialise b.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users