Results 1 to 7 of 7
  1. #1
    McXxT is offline Member
    Join Date
    Apr 2009
    Posts
    3
    Rep Power
    0

    Smile Pleas help on finding errors

    Hi guys, im doing this exersize and couldn't find the bug.

    Try to print
    " Sum = 15
    Product = 120

    Here is the code.

    public static void main(String[] args)
    {
    int count = 0;
    int sum = 0;
    int product = 0;
    do
    {
    count++;
    sum += count;
    product *= count;
    if (count == 5)
    System.out.println("Sum = " + sum);
    System.out.println("Product = " + product);
    } while (count < 5);
    }

    and it will print like this
    "
    Product = 0
    Product = 0
    Product = 0
    Product = 0
    Sum = 15
    Product = 0

  2. #2
    blurred83 is offline Member
    Join Date
    Apr 2009
    Posts
    3
    Rep Power
    0

    Default

    Ok, first consider the statement:
    product *= count;

    The variable 'product' is instantiated with the value 0, so every time you multiply it against something, it'll return 0, right? Since your goal is to show product equal to 120, there must be something wrong with that line, or you'll never get a value other than 0 when you multiply that variable's value against another integer...

    Otherwise, look into 'If/Else' statements - particularly look into syntax. Can you include multiple lines under an if/else statement without including brackets? What kind of brackets do you need?

    Hope this helps.

  3. #3
    kbindumadhavi is offline Member
    Join Date
    Apr 2009
    Posts
    20
    Rep Power
    0

    Default

    Java Code:
    public class Test2{
    public static void main(String[] args)
    {
    int count = 0;
    int sum = 0;
    int product = 1;//Changed value to 1 as you were trying to multipy with 0 everytime
    do
    {
    count++;
    sum += count;
    product *= count;
    if (count == 5){//Start of if condition
    System.out.println("Sum = " + sum);
    System.out.println("Product = " + product);
    }//Closed if condition
    } while (count < 5);
    }
    }
    Try this.

  4. #4
    blurred83 is offline Member
    Join Date
    Apr 2009
    Posts
    3
    Rep Power
    0

    Default

    kbindumadhavi - nice work... but if people are going to try to learn here, i assume the goal is to give them resources to research solutions rather than giving away solutions entirely.

  5. #5
    McXxT is offline Member
    Join Date
    Apr 2009
    Posts
    3
    Rep Power
    0

    Default

    Ahh~~ I see.
    Thanks a lot~
    It is very helpful~

  6. #6
    McXxT is offline Member
    Join Date
    Apr 2009
    Posts
    3
    Rep Power
    0

    Default

    Thank you blurred83 very much.
    I got what I did wrong and learned from it.

  7. #7
    blurred83 is offline Member
    Join Date
    Apr 2009
    Posts
    3
    Rep Power
    0

Similar Threads

  1. Replies: 3
    Last Post: 03-08-2009, 01:51 AM
  2. Finding elements in a vector
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-14-2008, 08:37 PM
  3. Finding arguments of Servlet
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-25-2008, 07:04 PM
  4. Finding largest no
    By bugger in forum New To Java
    Replies: 11
    Last Post: 11-29-2007, 12:49 PM
  5. Finding GCF in java
    By lenny in forum Advanced Java
    Replies: 1
    Last Post: 07-31-2007, 05:41 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •