: Hello,
:
: I'm new to JAVA (brand new), and new to these boards. I wanted to run a problem by you to see if YOU (those of you who are vet's in JAVA) think this is what the problem is asking me to do.
:
: Below is the problem, and below that is my solution: (I'm just not sure if the problem wanted something more complicated than this or not...(user entry? JAVA applets, etc). Could someone tell me if what I did 'meets' the problem specifications...and/or if I'm doing it incorrectly, or not the way you believe it 'needs' to be done?
:
: Thanks beforehand!
:
: Okay, to the problem:
:
: Write a Java application that uses looping to print the following table of values:
:
: N 10*N 100*N 1000*N
: 1 10 100 1000
: 2 20 200 2000
: 3 30 300 3000
: 4 40 400 4000
: 5 50 500 5000
:
: Now, below is my code (which works...and prints out the correct table...I'm just not sure if I'm 'meeting' the problem specifications through the way I accomplished the program.
:
: public class ValuesTable {
:
: public static void main ( String args[] ) {
:
: // initialize a counter
: int counter;
:
: // set the counter to 1 so that the program only loops one time
: counter = 1;
:
: // match the condition in the while and run the print lines. When counter > 1, terminate.
: while ( counter <= 1 ) {
: System.out.print( "N\t\t10*N\t\t100*N\t\t1000*N\n" );
: System.out.print( "1\t\t10\t\t100\t\t1000\n" );
: System.out.print( "2\t\t20\t\t200\t\t2000\n" );
: System.out.print( "3\t\t30\t\t300\t\t3000\n" );
: System.out.print( "4\t\t40\t\t400\t\t4000\n" );
: System.out.print( "5\t\t50\t\t500\t\t5000" );
:
: // increment counter
: counter += 1;
:
: } // end while
:
: } // end main
:
: } // end class ValuesTable
:
: Thanks again for any insight that you can provide.
:
No, you aren't meeting the specs. That is not really using looping, that is just printing out the table, and exiting the loop. Print out the header, outside the loop. Then do the rest in a loop, using arthimetic and loops.
It is easier to show it then explain it, it is something like this.
...
System.out.println( "N\t\t10*N\t\t100*N\t\t1000*N" );//use println
//i++ is equivilent to i=i+1
for(int i=1;i<6;i++)//you can use a while looop, but for is better suited to this case
{
/*use a single println statement that uses i(or whatever variable name you want in a manner that outputs
what you need, using * when necessary along with the proper formating(ie "\t\t")
*/
}
...