-
Eliminating a line
I'm a little bit stuck with how to work with this piece of code:
Code:
for (int x = a; x <= p; x*=3)
{
System.out.println(x);
}
for (y = b; y >= 1; y/=3)
{
if (y % 3 !=0)
{
System.out.println(y);
}
else
{
System.out.print("");
}
}
All variables are integers.
For this code, y will not be outputted if it is a multiple of 3. However, I want x not to be outputted as well if y is a multiple of 3. I'm having trouble because I want x to be outputted before y.
I would also like to align the output with x in a column, and y in a column beside that. I don't think I can use "/t", so I was wondering how to use another for loop correctly (I can get the rows right, but I can't limit the columns correctly)? Or is there a better way of aligning the two outputs?
-
You might try nesting your for-loops and Only printing x after you have tested y ... maybe something like this:
Code:
for (int x=a; x<=p; x*=3) {
for (y=b; y>1; y/=3) {
if (y%3 != 0) {
System.out.print(x + "\t");
System.out.println(y);
}
}
}
BTW: I didn't check for correctness or syntax
-
In your code, Y is local to the second loop. It has no value while X is bring printed out.
Can you provide some sample output that you want to achieve?
-
Thanks for the idea tashimoto.
-
Hi Wizar,
I am not quite sure what problem you try to solve and how the output should look but you could consider to create a for loop with the initialisation, test and increment combined:
Code:
for(x = a, y = b; x <= p && y >= 1; x *= 3, y /= 3) {
}
Are you sure you want all variables to be ints? This means that after at most 2 iterations nothing will be displayed because after 2 iterations y % 3 will always result in 0.
Succes,
Erik
-
I didn't realize you could do that! Thanks! :)
And that code is all part of a larger program, no worries.