Results 1 to 6 of 6
Thread: Eliminating a line
- 11-03-2010, 08:45 PM #1
Member
- Join Date
- Nov 2010
- Posts
- 18
- Rep Power
- 0
Eliminating a line
I'm a little bit stuck with how to work with this piece of code:
All variables are integers.Java 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(""); } }
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?
- 11-03-2010, 09:35 PM #2
Member
- Join Date
- Sep 2010
- Location
- Oregon, usa
- Posts
- 69
- Rep Power
- 0
You might try nesting your for-loops and Only printing x after you have tested y ... maybe something like this:
BTW: I didn't check for correctness or syntaxJava 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); } } }:cool: It's all here: http://download.oracle.com/javase/6/docs/api/
- 11-03-2010, 09:55 PM #3
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?
- 11-03-2010, 11:44 PM #4
Member
- Join Date
- Nov 2010
- Posts
- 18
- Rep Power
- 0
Thanks for the idea tashimoto.
Last edited by wizar; 11-04-2010 at 01:23 AM. Reason: to avoid confusion
- 11-04-2010, 12:30 AM #5
Member
- Join Date
- Oct 2010
- Posts
- 94
- Rep Power
- 0
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:
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.Java Code:for(x = a, y = b; x <= p && y >= 1; x *= 3, y /= 3) { }
Succes,
ErikI'm new to Java but I like to help where ever I can. :)
- 11-04-2010, 01:01 AM #6
Member
- Join Date
- Nov 2010
- Posts
- 18
- Rep Power
- 0
Similar Threads
-
tracing java application line by line using netbeans
By chandrasekhar123 in forum NetBeansReplies: 1Last Post: 08-03-2010, 02:46 PM -
Formatting java command line output - Multi line string
By dricco in forum New To JavaReplies: 2Last Post: 07-02-2010, 02:20 PM -
Need to read an .ini and .abook file line by line (both files contain texts)
By ollyworks in forum Java AppletsReplies: 4Last Post: 09-10-2009, 10:18 AM -
Reading in a line of data and splitting the line up into variables
By guru32 in forum New To JavaReplies: 9Last Post: 04-07-2009, 03:51 AM -
Reading in data from file line by line
By bluekswing in forum New To JavaReplies: 1Last Post: 10-02-2007, 12:19 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks