• Post Reply Bookmark Topic Watch Topic
  • New Topic

Product problem  RSS feed

 
Carmenia Bona
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi. I'm trying to write a code that will do this: 1*2*3*4...

But, I can't seem to get it. My code keeps giving me the wrong answer so I know there's something wrong with it. Can someone help me?

Here's what I have:

public class prod1 {
public static void main(String[] args) {
//int sum = 0;
int product = 0;
int count = 0;
int lowerbound = 1;
int upperbound = 4;

int number = lowerbound;

do {
int a = number;
//int b = number + 1;
product = a * (a + 1);
++number;
++count;
}

while (number <= upperbound);

System.out.println("The product is " + product);
System.out.println("The count is " + count);

}
}

 
K. Tsang
Bartender
Posts: 3648
16
Firefox Browser Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What are you getting?
 
Carmenia Bona
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
K. Tsang wrote:What are you getting?


This is what I'm getting:


The product is 20
The count is 4
 
Ashish Dutt
Ranch Hand
Posts: 172
Java MySQL Database Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let's rewrite the code again

[Moderator action: complete code solution removed]
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ashish Dutt wrote:Let's rewrite the code again...

Let's not.

Please DontBeACodeMill (←click) Ashish; especially in the 'Beginning Java' forum. I'm sure your motives were great, but we prefer people to find their own solutions.

So: by all means explain what the problem is, and help beginners; but don't spoon-feed.

Thanks.

Winston
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carmenia Bona wrote:This is what I'm getting:...

Carmenia,

A really good idea when you run into problems like this is to StopCoding (←click).

Then, you have two choices (both of which require a pencil and LOTS of paper):
1. Go through your code and write down, for each iteration of the loop, the values of each variable. An alternative is to print them all out with System.out.println() statements.
2. Forget the code altogether and write down in English (or your native language) the exact steps required to get the result you want; and then check those steps against what you've actually coded.

Personally, I prefer number 2; but the best time to do it is before you write any Java code at all.

HIH

Winston
 
Ashish Dutt
Ranch Hand
Posts: 172
Java MySQL Database Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
Ashish Dutt wrote:Let's rewrite the code again...

Let's not.

Please DontBeACodeMill (←click) Ashish; especially in the 'Beginning Java' forum. I'm sure your motives were great, but we prefer people to find their own solutions.

So: by all means explain what the problem is, and help beginners; but don't spoon-feed.

Thanks.

Winston

My sincere apologies, Winston.
I forgot what i normally preach and practice.
Thanks for reminding me. :-)
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ashish Dutt wrote:I forgot what i normally preach and practice.
Thanks for reminding me. :-)

No probs. I know you're usually very helpful.

Winston
 
Carmenia Bona
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
Carmenia Bona wrote:This is what I'm getting:...

Carmenia,

A really good idea when you run into problems like this is to StopCoding (←click).

Then, you have two choices (both of which require a pencil and LOTS of paper):
1. Go through your code and write down, for each iteration of the loop, the values of each variable. An alternative is to print them all out with System.out.println() statements.
2. Forget the code altogether and write down in English (or your native language) the exact steps required to get the result you want; and then check those steps against what you've actually coded.

Personally, I prefer number 2; but the best time to do it is before you write any Java code at all.

HIH

Winston



Thanks, Winston. Yes, I do want to learn on my own. I will try the second method you are suggesting.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!