Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Code help, timestable

  1. #1
    Junior Member
    Join Date
    Aug 2011
    Posts
    18
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Exclamation Code help, timestable

    Hi People,

    This is my question:


    Write an application to:
    Print the 5 times table (5 * 1 = 5, 5* 2 =10, .. 5 * 12 = 60)
    Format of output
    5 * 1 = 5
    5 * 2 = 10
    etc.
    Hint: Write the Java code to print the line for the 4th row, use a variable row of
    type int to hold the value 3 Then using the value held in row print the
    4th line.
    4 * 3 = 20
    Enclose

    And this is my code:

    public void multTable(int n)
    {
    int m = i* n;
    String line=" ";
    if(i<=10)
    {
    line+=" "+i;
    }else{
    line+=i;
    }
    line +=" * " + n + "=";
    if(m<100)
    {
    line+=" ";
    }
    line +=m;
    System.out.println(line);
    }
    }



    But the problem is, I want it to become as 5*1 = 5 etc BUT its coming up as 0*1=0 etc... always starts with 0? how do I put a 5 instead of a 0???

    Thanks!! R x hope u can help!


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,149
    Thanks
    48
    Thanked 2,158 Times in 2,131 Posts

    Default Re: Code help, timestable

    Please show what your program prints out now.
    What is it supposed to print out?
    Do you know how to code the expression to multiply a number by another number?
    theProduct = anumber * anothernumber;
    Your code does not do that any where. All I see are Strings being concatenated to one another.
    String aStr = "5" + "*" + "1"; //==> gives the String: "5*1"

    When you post code, please use the code tags to preserve its formatting. See: BB Code List - Java Programming Forums
    What you have posted is very hard to read.

  3. #3
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,512
    My Mood
    Grumpy
    Thanks
    0
    Thanked 165 Times in 156 Posts

    Default Re: Code help, timestable

    You are making this way harder than it needs to be. to print a times table you need a loop but I don't see one in your code. All you have is several confusing if statements which are not needed.
    loop (12 times) {
        print n * c = r;
    }
    Where n = the timestable you are printing (ie 5), c is a counter (1 - 12) and r is the result of multiplying n by c.
    Improving the world one idiot at a time!

  4. #4
    Junior Member
    Join Date
    Aug 2011
    Posts
    18
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Code help, timestable

    Done it guys, thanks!