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 replies to this topic
#1
Posted 16 November 2011 - 06:05 PM
|
|
|
#2
Posted 16 November 2011 - 06:47 PM
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
Posted 16 November 2011 - 10:50 PM
You can use b = 2 + a to initialise b.
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account

Back to top









