Results 1 to 7 of 7
Thread: Pleas help on finding errors
- 04-02-2009, 07:49 AM #1
Member
- Join Date
- Apr 2009
- Posts
- 3
- Rep Power
- 0
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
- 04-02-2009, 08:08 AM #2
Member
- Join Date
- Apr 2009
- Posts
- 3
- Rep Power
- 0
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.
- 04-02-2009, 08:09 AM #3
Member
- Join Date
- Apr 2009
- Posts
- 20
- Rep Power
- 0
Try this.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); } }
- 04-02-2009, 08:13 AM #4
Member
- Join Date
- Apr 2009
- Posts
- 3
- Rep Power
- 0
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.
- 04-02-2009, 08:18 AM #5
Member
- Join Date
- Apr 2009
- Posts
- 3
- Rep Power
- 0
Ahh~~ I see.
Thanks a lot~
It is very helpful~
- 04-02-2009, 08:19 AM #6
Member
- Join Date
- Apr 2009
- Posts
- 3
- Rep Power
- 0
Thank you blurred83 very much.
I got what I did wrong and learned from it.
- 04-02-2009, 08:42 AM #7
Member
- Join Date
- Apr 2009
- Posts
- 3
- Rep Power
- 0
Similar Threads
-
What is the difference between Semantic Errors and Logical Errors?
By tlau3128 in forum New To JavaReplies: 3Last Post: 03-08-2009, 01:51 AM -
Finding elements in a vector
By Java Tip in forum java.langReplies: 0Last Post: 04-14-2008, 08:37 PM -
Finding arguments of Servlet
By Java Tip in forum Java TipReplies: 0Last Post: 01-25-2008, 07:04 PM -
Finding largest no
By bugger in forum New To JavaReplies: 11Last Post: 11-29-2007, 12:49 PM -
Finding GCF in java
By lenny in forum Advanced JavaReplies: 1Last Post: 07-31-2007, 05:41 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks