2 Replies - 342 Views - Last Post: 06 October 2013 - 04:58 PM Rate Topic: -----

#1 michaelphongsak  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 06-October 13

create a multiplication table INVOKING

Posted 06 October 2013 - 09:32 AM

I got most if this done and the program runs, but my professor said that I didn't complete it. I need to use a method which prints each row which is invoked from the method that printsthe overall multiplication table.

heres the assignment:
Multiplication Table
A set of nested for loops can be used to print a multiplication table using System.out.printf statements.

Write a program to print such a multiplication table, using a method to print each row, which is invoked from the method that prints the overall multiplication table.

my code:
public class MultiplicationTable 
{ 
public static void main (String[] args) { 
// print header row 
System.out.print(" "); 
for (int i = 1; i <= 9; i++) 
System.out.print(pad(i) + " "); 
System.out.println(); 
// print separator 
System.out.print(" "); 
for (int i = 1; i <= 9; i++) 
System.out.print(" "); 
System.out.println(); 
// print main table 
for (int i = 1; i <= 9; i++) { 
System.out.print(pad(i) + " "); 
for (int j = 1; j <= 9; j++) { 
System.out.print(pad(i*j) + " "); 
} 
System.out.println(); 
} 
} // end of main 

// pad: add blanks to make it 3 long 
public static String pad(int x) { 
String s = new String(); 
if (x < 10) s = " " + x; 
else if (x < 100) s = " " + x; 
else s = "" + x; 
return s; 
} 
}



Is This A Good Question/Topic? 0
  • +

Replies To: create a multiplication table INVOKING

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10180
  • View blog
  • Posts: 37,585
  • Joined: 27-December 08

Re: create a multiplication table INVOKING

Posted 06 October 2013 - 03:27 PM

1) Please properly indent your code so that others, including you, can easily read it.

2) What have you tried? What specific problems or errors are you encountering?
Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: create a multiplication table INVOKING

Posted 06 October 2013 - 04:58 PM

Indent your code !
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1