• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Product problem

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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);

}
}

 
Bartender
Posts: 3648
16
Android Mac OS X Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What are you getting?
 
Carmenia Bona
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 172
Python MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Let's rewrite the code again

[Moderator action: complete code solution removed]
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Python MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Live a little! The night is young! And we have umbrellas in our drinks! This umbrella has a tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic