What do you think?

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 10*N 100*N 1000*N
" );
System.out.print( "1 10 100 1000
" );
System.out.print( "2 20 200 2000
" );
System.out.print( "3 30 300 3000
" );
System.out.print( "4 40 400 4000
" );
System.out.print( "5 50 500 5000" );

// increment counter
counter += 1;

} // end while

} // end main

} // end class ValuesTable

Thanks again for any insight that you can provide.

Comments

  • Hey there.

    That works...but I don't think that that is what they are really wanting.

    How bout this? I dunno.

    ie:

    [code]

    for(int i = 1; i < 6 ; i++){
    System.out.print(i + " ");
    System.out.print(i*10 + " ");
    System.out.print(i*100 + " ");
    System.out.println(i*1000);
    }

    [/code]
  • : 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 10*N 100*N 1000*N
    " );
    : System.out.print( "1 10 100 1000
    " );
    : System.out.print( "2 20 200 2000
    " );
    : System.out.print( "3 30 300 3000
    " );
    : System.out.print( "4 40 400 4000
    " );
    : System.out.print( "5 50 500 5000" );
    :
    : // 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.

    [code]
    ...
    System.out.println( "N 10*N 100*N 1000*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 " ")
    */
    }
    ...


    [/code]
  • Thanks you two. That made sense and I don't know WHY I didn't think of that method of doing it...I had read that part in the book.

    Thanks again!

    : Hey there.
    :
    : That works...but I don't think that that is what they are really wanting.
    :
    : How bout this? I dunno.
    :
    : ie:
    :
    : [code]
    :
    : for(int i = 1; i < 6 ; i++){
    : System.out.print(i + " ");
    : System.out.print(i*10 + " ");
    : System.out.print(i*100 + " ");
    : System.out.println(i*1000);
    : }
    :
    : [/code]
    :

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories