Results 1 to 8 of 8
- 07-12-2010, 08:40 AM #1
Member
- Join Date
- Jul 2010
- Posts
- 3
- Rep Power
- 0
need help with this: multiplication table
Hello everyone! I am new to java programming. I'm trying to do the exercises in the book that I am reading.
I would to ask for a help. I am supposed to make a multiplication table. It asks a user to enter the size of the multiplication table then the output should be something like this:
0 1 2
1 1
2 2 4
3 3 6
4 4 8
5 5 10
(with "1 tab" space between columns)
Now with these codes I wrote, I am only getting 1 column. What must I do? Please help me. Thank you very much!
Java Code:import java.io.*; public class MultiplicationTable { public static void main(String args[]) throws IOException { BufferedReader in=new BufferedReader (new InputStreamReader(System.in)); int num; System.out.println("Enter a number: "); num=Integer.parseInt(in.readLine()); for (int x=0;x<num;x++) { for (int y=0;y<num;y++) { System.out.println(x*y); } } } }
- 07-12-2010, 08:45 AM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,400
- Blog Entries
- 7
- Rep Power
- 17
Look at the System.out.print( ... ) method versus the System.out.println( ... ) method.
kind regards,
Jos
- 07-12-2010, 04:28 PM #3
That's what you code outputs: System.out.println(x*y)I am only getting 1 column
You need to add more stuff in between the (...)s to get more columns
- 07-13-2010, 01:45 AM #4
Member
- Join Date
- Jul 2010
- Posts
- 3
- Rep Power
- 0
I've added System.out.print("\t"); before the System.out.print(x*y);
Now I get the columns but only 1 row. lol:confused:
- 07-13-2010, 01:50 AM #5
What is on that row?
Do you know how to tell the output to go onto a new line?
Two ways: use println() or add a "\n" (newline character) in the print() statement.
- 07-13-2010, 09:18 AM #6
Member
- Join Date
- Jul 2010
- Posts
- 3
- Rep Power
- 0
I got it already, the rows and the columns. My problem now is the output is not aligned. How can I aligned them?
OUTPUT:
- 07-13-2010, 01:12 PM #7
You need to pad the data for each column with spaces so that each column is the same width.
I use a method which takes a string and pads it on the left with enough spaces so that the results are all the same width. For example to create columns 5 characters wide if it contained a 1 it would pad with 4 spaces on the left. If the column would contain 121 it would pad with 2 spaces on the left.
- 07-13-2010, 01:31 PM #8
Similar Threads
-
Multiplication Table
By SwEeTAcTioN in forum New To JavaReplies: 4Last Post: 02-24-2010, 04:11 AM -
Multiplication table quiz
By wolfy101 in forum New To JavaReplies: 1Last Post: 11-16-2009, 06:29 AM -
Multiplication table
By rjones215 in forum New To JavaReplies: 3Last Post: 10-19-2009, 04:34 PM -
trouble creating program using loops for multiplication table
By cuse17 in forum New To JavaReplies: 2Last Post: 02-23-2009, 02:18 AM -
Help with multiplication table
By Albert in forum New To JavaReplies: 1Last Post: 07-10-2007, 04:44 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks