Re: Nested for loop problem.
You can print your loops indexes since they reset every loop.
You need another variable that will be raised in 1 after you print him in the nested loop.
for ..
for ..
{
print(counter)
counter++
}
btw: change those long names! I'm using "k" and "i" for my indexes.
Re: Nested for loop problem.
Quote:
Originally Posted by
tnrh1
btw: change those long names! I'm using "k" and "i" for my indexes.
Nothing wrong with descriptive names for loop counters if it makes the purpose of the loop more clear.
Re: Nested for loop problem.
Quote:
Originally Posted by
Iron Lion
Nothing wrong with descriptive names for loop counters if it makes the purpose of the loop more clear.
I didn't said it's wrong.
Using short names helps to read the code faster, save time in writing it and the chance to type it wrong is much lower.
But it's not a demand it's an advice.
Code:
for(int LongVariableName=1;LongVariableName<=MaximumNumber;LongVariableName++)
Sytem.out.println(LongVariableName);
Code:
for(int i=0;i<=k;k++)
System.out.println(i);
Work as you like.
Re: Nested for loop problem.
Quote:
int k=1;
for(i=1;...;..){
for(j=1;..;..){
if(k<=x)
System.out.printf("%"+i+"d",k);
k++;
}
System.out.println();
}
this will work as u required.this s jst sample. try in this way.jst try implementing
1 Attachment(s)
Re: Nested for loop problem.
Thanks for helping me out here.
Code:
import java.util.Scanner;
public class Boka {
public static void main (String args[]){
Scanner input = new Scanner(System.in);
int max;
System.out.print("max value: ");
max = input.nextInt();
printNumbersIncreasing(max);
} //Main method end
public static void printNumbersIncreasing(int x){
int counter = 1;
for (int i = 1; i <= x; i++){
for (int j = 1; j <= i; j++){
if (counter <= x){
System.out.printf ("%" + i + "d", counter);
counter++;
} //if statment end
} // Inner for loop end
System.out.println();
} //outer for loop end
} //Method end
} //Class end.
Fair enough, it works well, but the only problem now is this:
Attachment 1923
As you can see on the output pic, i get blank-lines on output. What could be the problem?
Kind Regards Danny.
Re: Nested for loop problem.
That's cause of the first for loop, he has to stop much before he equals to x.
You can fix it by adding a boolean variable, let say flag and equal it to true.
Then add an "else" to your if statement (counter<=x), and inside the else type flag = false;
Now before the system.out.println(); add an if statement that ask if the the flag is true.(if flag).
Why we did all that?when if (counter <= x) give you false you still printing the new lines and your first for loop keep running.
I'm sure that there is a better solution, I will think about it and edit my post.
The answer is definitely in this line: for (int i = 1; i <= x; i++)
Re: Nested for loop problem.
After few days I finally got an answer, thanks to 2 guys josAH and pbrockway2.
You asked if counter is <= to x because your loop runned much more times then it should be.
This one is the correct answer:
Code:
public static void print (int num)
{
int counter = 1;
for(int i=1;i<=Math.round(Math.sqrt(num*2));i++)
{
for(int k=1;k<=i;k++)
{
System.out.print(counter+" ");
counter++;
}
System.out.println();
}
}
This loop will run as many times as it needs.